C++ C从单独的文件C+;链接类时出现问题+;
我是linux开发的新手,在使用主应用程序中单独文件中的类时遇到问题。在使用cmake创建makefile后,我在生成时遇到的错误是系统没有命名类型,我认为system类中的代码是正确的,就好像我在编译时没有尝试创建system类的对象一样,我没有错误,因此我认为这可能与我编写CMakeLists.txt文件的方式有关 这是我的CMakeLists文件:C++ C从单独的文件C+;链接类时出现问题+;,c++,linux,class,types,cmake,C++,Linux,Class,Types,Cmake,我是linux开发的新手,在使用主应用程序中单独文件中的类时遇到问题。在使用cmake创建makefile后,我在生成时遇到的错误是系统没有命名类型,我认为system类中的代码是正确的,就好像我在编译时没有尝试创建system类的对象一样,我没有错误,因此我认为这可能与我编写CMakeLists.txt文件的方式有关 这是我的CMakeLists文件: cmake_minimum_required (VERSION 2.6) project (GL_PROJECT)
cmake_minimum_required (VERSION 2.6)
project (GL_PROJECT)
add_library(system system.cpp)
include_directories(${GL_PROJECT_SOURCE_DIR})
link_directories(${GL_PROJECT_BINARY_DIR})
find_package(X11)
if(NOT X11_FOUND)
message(FATAL_ERROR "Failed to find X11")
endif(NOT X11_FOUND)
find_package(OpenGL)
if(NOT OPENGL_FOUND)
message(FATAL_ERROR "Failed to find opengl")
endif(NOT OPENGL_FOUND)
set(CORELIBS ${OPENGL_LIBRARY} ${X11_LIBRARY})
add_executable(mainEx main.cpp system.cpp)
target_link_libraries(mainEx ${CORELIBS} system)
我在源目录中有main.cpp、system.h(类定义)和system.cpp(类实现)
大体上:
#include"system.h"
system sys;
int main(int argc, char *argv[]) {
while(1)
{
sys.Run();
}
}
X11和GL includes etc在system.h中,我认为其中的代码是正确的,不会导致错误(因为如果我不尝试创建该类的实例,它构建得很好)。为了简洁起见,我省略了实际的头文件和实现,希望它在CMakeList文件中会是一个明显的错误,但是如果有必要,我也可以添加它们
有什么想法吗
提前谢谢
编辑:以下是终端中的错误
[tims@localhost build]$ make
Scanning dependencies of target system
[ 33%] Building CXX object CMakeFiles/system.dir/system.cpp.o
Linking CXX static library libsystem.a
[ 33%] Built target system
Scanning dependencies of target mainEx
[ 66%] Building CXX object CMakeFiles/mainEx.dir/main.cpp.o
/home/tims/Code/GL_Project/main.cpp:5:1: error: ‘system’ does not name a type
/home/tims/Code/GL_Project/main.cpp: In function ‘int main(int, char**)’:
/home/tims/Code/GL_Project/main.cpp:11:3: error: ‘sys’ was not declared in this scope
make[2]: *** [CMakeFiles/mainEx.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/mainEx.dir/all] Error 2
make: *** [all] Error 2
编辑2:system.h
#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
class system {
public:
system(void);
~system(void);
void CreateGLXWindow();
void Run();
//Contains information about X server we will be communicating with
Display *display;
XEvent xEvent;
//Window instance
Window rootWindow;
XVisualInfo *xVisInfo;
XSetWindowAttributes setWindAttrs;
XWindowAttributes xWindAttrs;
//GL
GLXContext context;
Colormap cmap;
Window window;
private:
};
#包括
#包括
#包括
#包括
#包括
#包括
#包括
阶级制度{
公众:
系统(无效);
~系统(无效);
void CreateGLXWindow();
无效运行();
//包含有关我们将与之通信的X服务器的信息
显示*显示;
XEvent-XEvent;
//窗口实例
窗口根窗口;
XVisualInfo*xVisInfo;
Xsetwindowatts向setWindAttrs致敬;
XWindowAttributes xWindAttrs;
//德国劳埃德船级社
GLXContext上下文;
彩色地图;
窗口窗口;
私人:
};
类名system
与int system(const char*)
函数冲突,该函数在stdlib.h
中声明,包含在system.h
中。您需要重命名<代码>系统< /Cord>类,或者将其移动到命名空间中,因为类和函数在C++中不能有相同的名称。< /P>您能提供生成的错误消息吗?我认为一个问题可能是,您在target\u link\u libraries
中提到system
。您也可以使用find\u包(必需)
。如果找不到包,并且您不需要if(not _found)…
,这将自动生成错误消息。我已将生成的错误消息编辑到主帖子中。我想我需要添加系统类作为库,然后使用目标链接库链接它?另外-谢谢关于“REQUIRED”的提示,我将改为这样做。system
是否在名称空间中声明?否则,我认为如果发布system.hpp
,也会很有帮助。不,它不在名称空间内-我也将system.h标题编辑到了主帖子中。(使用hpp扩展而不是像我现在使用的.h有什么区别吗?)!我重新命名了它,它工作得很好,非常感谢!我想我应该开始在我的名字前面加前缀,或者把所有的东西都放到名称空间中,以避免将来发生这种情况。再次感谢!:)我也会投票支持这个答案,但我的声誉还不够好,呵呵,我把它勾对了,谢谢