我目前正在尝试调试一个程序,该程序决定不运行。我猜错误很简单,我对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();
};
答案 0 :(得分:3)
如果你有静态成员变量,他们也需要定义,不仅声明:
在头文件中:
class Foo
{
...
// This declares a static member variable
static int static_member_variable;
...
};
在源文件中:
// This defines the variable
int Foo::static_member_variable;