gpt4 book ai didi

c++ - CMake 链接不正确?

转载 作者:行者123 更新时间:2023-11-28 00:52:19 24 4
gpt4 key购买 nike

我目前正在尝试调试一个程序,该程序是确定不运行的。我想这个错误很简单,我真的是 c++ 和使用 cmake 的新手。

这是错误信息:

Scanning dependencies of target viewer

[ 25%] Building CXX object CMakeFiles/viewer.dir/Main.cpp.o
[ 50%] Building CXX object CMakeFiles/viewer.dir/TriangleMesh.cpp.o
[ 75%] Building CXX object CMakeFiles/viewer.dir/Camera.cpp.o
[100%] Building CXX object CMakeFiles/viewer.dir/MainWindow.cpp.o
Linking CXX executable viewer
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mousePressed(int, int, int, int): error: undefined reference
to 'MainWindow::mouse_button'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mousePressed(int, int, int, int): error: undefined reference
to 'MainWindow::mouse_state'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::render(): error: undefined reference to 'MainWindow::cam'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::render(): error: undefined reference to 'MainWindow::mesh'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::mouse_button'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::old_y'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::old_x'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::cam'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::cam'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::mouse_button'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::old_x'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::old_y'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::mouse_button'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::mouseMove(int, int): error: undefined reference to
'MainWindow::cam'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::MainWindow(Camera*, int, char**): error: undefined reference
to 'MainWindow::old_x'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::MainWindow(Camera*, int, char**): error: undefined reference
to 'MainWindow::old_y'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::MainWindow(Camera*, int, char**): error: undefined reference
to 'MainWindow::mesh'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::MainWindow(Camera*, int, char**): error: undefined reference
to 'MainWindow::mesh'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::MainWindow(Camera*, int, char**): error: undefined reference
to 'MainWindow::mesh'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::getInstance(Camera*, int, char**): error: undefined reference
to 'MainWindow::instance'
CMakeFiles/viewer.dir/MainWindow.cpp.o:MainWindow.cpp:function
MainWindow::getInstance(Camera*, int, char**): error: undefined reference
to 'MainWindow::instance'
collect2: ld returned 1 exit status
make[2]: *** [viewer] Error 1
make[1]: *** [CMakeFiles/viewer.dir/all] Error 2
make: *** [all] Error 2

所以,代码编译得很好,它只是不能真正链接 4 个源文件之一..

编辑:

这是 MainWindow.hpp 的要点

class MainWindow
{
private:
MainWindow() {}
MainWindow(const MainWindow&) {}
MainWindow(Camera *cam, int argc, char* argv[]);
static MainWindow *instance;
static Camera *cam;
static TriangleMesh mesh;
static int old_x;
static int old_y;
static int mouse_button;
static int mouse_state;
static void mousePressed(int button, int state, int x, int y);
static void initGlut(int argc, char* argv[]);
static void initCallbacks();
static void render();
static void reshape(int w, int h);
static void keyPressed(unsigned char key, int x, int y);
static void mouseMove(int x, int y);

public:
static MainWindow* getInstance(Camera *cam, int argc, char* argv[]);
static void setCamera(Camera* cam);
static void setMesh(TriangleMesh* mesh);
static void destroy();
};

最佳答案

如果你有静态成员变量,它们也需要定义,而不仅仅是声明:

在头文件中:

class Foo
{
...

// This declares a static member variable
static int static_member_variable;

...
};

在源文件中:

// This defines the variable
int Foo::static_member_variable;

关于c++ - CMake 链接不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com