【问题标题】:Cmake trouble linking a class from separate file C++Cmake从单独的文件C++链接一个类的麻烦
【发布时间】:2012-05-31 13:27:42
【问题描述】:

我是 linux 开发的新手,在我的 main.js 文件中使用单独文件中的类时遇到了麻烦。我在制作时遇到的错误(在使用cmake创建makefile之后)是系统没有命名类型,我认为系统类中的代码是正确的,就好像我编译时没有尝试创建系统类的对象我没有错误,所以我认为这可能与我编写 CMakeLists.txt 文件的方式有关。

这是我的 CMakeLists 文件:

    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 包含等在 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:

};

【问题讨论】:

  • 能否提供生成的错误信息?我认为一个问题可能是,您在target_link_libraries 中提到了system。
  • 你也可以使用find_package(&lt;name&gt; REQUIRED)。如果找不到包并且您不需要if(NOT &lt;name&gt;_FOUND)...,这将自动生成错误消息。
  • 我已将生成的错误消息编辑到主帖中。我以为我需要将系统类添加为库,然后使用目标链接库链接它?另外-感谢有关“必需”的提示,我将改为这样做。
  • system 是否在命名空间内声明?否则我认为如果也发布system.hpp 也会有所帮助。
  • 不,它不在命名空间内 - 我也将 system.h 标头编辑到主帖子中。 (像我使用的那样使用 hpp 扩展名而不是 .h 有什么不同吗?)

标签: c++ linux class types cmake


【解决方案1】:

类名system 与int system(const char*) 函数发生冲突,在stdlib.h 中声明,该函数包含在您的system.h 中。您要么需要重命名 system 类,要么将其移动到命名空间中,因为在 C++ 中类和函数不能具有相同的名称。

【讨论】:

  • 太棒了!我重命名了它,它工作正常,非常感谢!我想我应该开始为我的名字添加前缀或将所有内容放入名称空间以避免将来发生这种情况。再次感谢! :) 另外,我会投票给答案,但我的信誉不够好,虽然谢谢,但我打勾是正确的
猜你喜欢
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2023-03-22
相关资源
最近更新 更多