编译opengl的未定义引用

Posted

技术标签:

【中文标题】编译opengl的未定义引用【英文标题】:Undefined references compiling opengl 【发布时间】:2011-07-17 19:29:32 【问题描述】:

当我从 opengl Superbible 第 5 版编译 Triangle 项目时,我遇到了这个代码错误

> /home/fpointbin/projects/Triangle/build> make
[  7%] Building CXX object CMakeFiles/gltools.dir/src/GLBatch.cpp.o
[ 15%] Building CXX object CMakeFiles/gltools.dir/src/GLShaderManager.cpp.o
[ 23%] Building CXX object CMakeFiles/gltools.dir/src/GLTools.cpp.o
[ 30%] Building CXX object CMakeFiles/gltools.dir/src/GLTriangleBatch.cpp.o
[ 38%] Building CXX object CMakeFiles/gltools.dir/src/math3d.cpp.o
[ 46%] Building C object CMakeFiles/gltools.dir/src/glew.c.o
Linking CXX shared library libgltools.so
[ 46%] Built target gltools
Scanning dependencies of target gltools-static
[ 53%] Building CXX object CMakeFiles/gltools-static.dir/src/GLBatch.cpp.o
[ 61%] Building CXX object CMakeFiles/gltools-static.dir/src/GLShaderManager.cpp.o
[ 69%] Building CXX object CMakeFiles/gltools-static.dir/src/GLTools.cpp.o
[ 76%] Building CXX object CMakeFiles/gltools-static.dir/src/GLTriangleBatch.cpp.o
[ 84%] Building CXX object CMakeFiles/gltools-static.dir/src/math3d.cpp.o
[ 92%] Building C object CMakeFiles/gltools-static.dir/src/glew.c.o
Linking CXX static library libgltools.a
[ 92%] Built target gltools-static
Linking CXX executable triangle
CMakeFiles/triangle.dir/main.cpp.o: In function `ChangeSize(int, int)':
/home/fpointbin/projects/Triangle/main.cpp:22: undefined reference to `glViewport'
CMakeFiles/triangle.dir/main.cpp.o: In function `SetupRC()':
/home/fpointbin/projects/Triangle/main.cpp:32: undefined reference to `glClearColor'
/home/fpointbin/projects/Triangle/main.cpp:34: undefined reference to `GLShaderManager::InitializeStockShaders()'
/home/fpointbin/projects/Triangle/main.cpp:41: undefined reference to `GLBatch::Begin(unsigned int, unsigned int, unsigned int)'
/home/fpointbin/projects/Triangle/main.cpp:43: undefined reference to `GLBatch::End()'
CMakeFiles/triangle.dir/main.cpp.o: In function `RenderScene()':
/home/fpointbin/projects/Triangle/main.cpp:53: undefined reference to `glClear'
/home/fpointbin/projects/Triangle/main.cpp:56: undefined reference to `GLShaderManager::UseStockShader(GLT_STOCK_SHADER, ...)'
/home/fpointbin/projects/Triangle/main.cpp:57: undefined reference to `GLBatch::Draw()'
/home/fpointbin/projects/Triangle/main.cpp:60: undefined reference to `glutSwapBuffers'
CMakeFiles/triangle.dir/main.cpp.o: In function `main':
/home/fpointbin/projects/Triangle/main.cpp:68: undefined reference to `gltSetWorkingDirectory(char const*)'
/home/fpointbin/projects/Triangle/main.cpp:70: undefined reference to `glutInit'
/home/fpointbin/projects/Triangle/main.cpp:71: undefined reference to `glutInitDisplayMode'
/home/fpointbin/projects/Triangle/main.cpp:72: undefined reference to `glutInitWindowSize'
/home/fpointbin/projects/Triangle/main.cpp:73: undefined reference to `glutCreateWindow'
/home/fpointbin/projects/Triangle/main.cpp:74: undefined reference to `glutReshapeFunc'
/home/fpointbin/projects/Triangle/main.cpp:75: undefined reference to `glutDisplayFunc'
/home/fpointbin/projects/Triangle/main.cpp:77: undefined reference to `glewInit'
/home/fpointbin/projects/Triangle/main.cpp:79: undefined reference to `glewGetErrorString'
/home/fpointbin/projects/Triangle/main.cpp:85: undefined reference to `glutMainLoop'
CMakeFiles/triangle.dir/main.cpp.o: In function `__static_initialization_and_destruction_0':
/home/fpointbin/projects/Triangle/main.cpp:14: undefined reference to `GLBatch::GLBatch()'
/home/fpointbin/projects/Triangle/main.cpp:14: undefined reference to `GLBatch::~GLBatch()'
/home/fpointbin/projects/Triangle/main.cpp:15: undefined reference to `GLShaderManager::GLShaderManager()'
/home/fpointbin/projects/Triangle/main.cpp:15: undefined reference to `GLShaderManager::~GLShaderManager()'
CMakeFiles/triangle.dir/main.cpp.o: In function `GLBatch::CopyVertexData3f(float*)':
/home/fpointbin/projects/Triangle/include/GLBatch.h:89: undefined reference to `GLBatch::CopyVertexData3f(float (*) [3])'
collect2: ld returned 1 exit status
make[2]: *** [triangle] Error 1
make[1]: *** [CMakeFiles/triangle.dir/all] Error 2
make: *** [all] Error 2
*** Failed ***

这是我的 Cmakelist.txt:

cmake_minimum_required (VERSION 2.6)

project(triangle)

find_package(X11)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_library(M_LIBRARY m)
find_library(GLEW_LIBRARY GLEW)

set ( INCLUDE_DIRS
    "$CMAKE_SOURCE_DIR/include"
    $INCLUDE_DIRS
)

include_directories (
    $INCLUDE_DIRS
)

if(UNIX)
    set ( INCLUDE_DIRS 
        "/usr/include"
        "/usr/local/include"
        "/usr/include/GL"
        $INCLUDE_DIRS
    )
endif(UNIX)

set ( GLTOOLS_HDRS
    "$CMAKE_SOURCE_DIR/include/GLBatchBase.h"
    "$CMAKE_SOURCE_DIR/include/GLBatch.h"
    "$CMAKE_SOURCE_DIR/include/GLFrame.h"
    "$CMAKE_SOURCE_DIR/include/GLFrustum.h"
    "$CMAKE_SOURCE_DIR/include/GLGeometryTransform.h"
    "$CMAKE_SOURCE_DIR/include/GLMatrixStack.h"
    "$CMAKE_SOURCE_DIR/include/GLShaderManager.h"
    "$CMAKE_SOURCE_DIR/include/GLTools.h"
    "$CMAKE_SOURCE_DIR/include/GLTriangleBatch.h"
    "$CMAKE_SOURCE_DIR/include/math3d.h"
    "$CMAKE_SOURCE_DIR/include/StopWatch.h"
    "$CMAKE_SOURCE_DIR/include/glew.h"
)

set ( GLTOOLS_SRCS
    "$CMAKE_SOURCE_DIR/src/GLBatch.cpp"
    "$CMAKE_SOURCE_DIR/src/GLShaderManager.cpp"
    "$CMAKE_SOURCE_DIR/src/GLTools.cpp"
    "$CMAKE_SOURCE_DIR/src/GLTriangleBatch.cpp"
    "$CMAKE_SOURCE_DIR/src/math3d.cpp"
    "$CMAKE_SOURCE_DIR/src/glew.c"

)

add_library ( gltools-static $GLTOOLS_SRCS)   
add_library ( gltools SHARED $GLTOOLS_SRCS)

target_link_libraries (gltools $OPENGL_LIBRARIES $GLUT_LIBRARIES $GLEW_LIBRARY $M_LIBRARY $X11_LIBRARIES)
target_link_libraries (gltools-static $OPENGL_LIBRARIES $GLUT_LIBRARIES $GLEW_LIBRARY $M_LIBRARY $X11_LIBRARIES)
set_target_properties(gltools-static PROPERTIES OUTPUT_NAME gltools)


add_executable(triangle main.cpp)

有什么问题?

【问题讨论】:

【参考方案1】:

你是对的,错误不是来自编译器,而是来自链接器。由于许多未定义的引用都是关于 glut 函数的,因此首先验证您是否可以正确链接 glut。看看this link - 如何设置 Glut。

【讨论】:

【参考方案2】:

很可能你没有包含头文件或者makefile中的include指令是错误的,因为错误对我来说很清楚:编译器找不到包含在OpenGL头文件中的OpenGL核心函数。

【讨论】:

以上是关于编译opengl的未定义引用的主要内容,如果未能解决你的问题,请参考以下文章

C ++错误:对“”的未定义引用,并且ld返回了1个退出状态[重复]

C++ 错误:对“<func name>”的未定义引用和 ld 返回 1 个退出状态 [重复]

对 glew 的未定义引用 [重复]

libcvd - 编译 C++ 时对“x ...”的未定义引用

对 `JNI_CreateJavaVM' linux 的未定义引用

对符号 glTexImage2D 的未定义引用