我目前正在尝试调试一个程序,该程序相当确定不运行。我想这个错误很简单,我对 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();
};