我正在从头开始编写我的第一个cmake文件,用于需要ODE等项目。如果我正在做其他不良做法,那么对此的评论也非常受欢迎。
使用cmake的makefile生成步骤正在运行,但是在make的构建步骤中,它会抱怨:
Scanning dependencies of target ode_oscillex
[ 50%] Building CXX object CMakeFiles/ode_oscillex.dir/ode_oscillex.cpp.o
[100%] Linking CXX executable ode_oscillex
ld: library not found for -lGLU
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [ode_oscillex] Error 1
make[1]: *** [CMakeFiles/ode_oscillex.dir/all] Error 2
make: *** [all] Error 2
我无法弄清楚-lGLU应该是什么库。基于此项目之前设置的Eclipse设置,我已经添加了X11,OpenGL和GLUT,此外不应该有任何其他内容。我在OS X上设置了它。
我的CMakeLists.txt是
cmake_minimum_required (VERSION 3.3)
set(PROJECT_NAME ode_oscillex)
set(TARGET ${PROJECT_NAME})
project (${PROJECT_NAME})
# Add manually built ODE library
set(ODE_PATH /Users/steve/ode-0.13.1)
set(ODE_INC_PATH ${ODE_PATH}/include)
set(ODE_LIB_PATH ${ODE_PATH}/ode/src/.libs)
set(DS_INC_PATH ${ODE_PATH}/include/drawstuff) # not sure why drawstuff needs subfolder to be specified, when ode doesn't...
set(DS_LIB_PATH ${ODE_PATH}/drawstuff/src/.libs)
include_directories(${ODE_INC_PATH} ${DS_INC_PATH})
link_directories(${ODE_LIB_PATH} ${DS_LIB_PATH})
### Add manually built Eigen
SET( EIGEN3_INCLUDE_DIR ../eigen3.1.4) # This is also sucky
include_directories(${EIGEN3_INCLUDE_DIR})
### Add Executable
add_executable(${TARGET} ${PROJECT_NAME}.cpp)
### Adding other required libraries
find_package(X11 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ${X11_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${X11_LIBRARIES})
target_link_libraries(${TARGET} ode drawstuff GLU GL glut X11)
答案 0 :(得分:1)
对我来说有问题的说明是:
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${X11_LIBRARIES})
target_link_libraries(${TARGET} ode drawstuff GLU GL glut X11)
一般情况下,您不应该链接"项目",您需要链接 "项目工件" (即" $ {TARGET}")。 因此,请尝试按如下方式重写:
target_link_libraries(${TARGET} <other required libraries like "ode"...> {OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${X11_LIBRARIES})
并完全删除另一行。
通常,当您调用上面的find_package()
时,这些宏填充相应的变量,告诉如何正确获取正确的编译器和链接器标志(分别用于OpenGL,GLUT等)。因此,当您在target_link_libraries
调用中使用适当的变量时,您可以确定链接器标志和库,特定于特定系统。
另一方面,当您只是使用target_link_libraries(<target> GLU GL glut...)
时,强制指示cmake将<target>
链接到那些与GL相关的库,无论它们是安装在特定系统中,还是它们如何安装在那里被命名。
所以一般来说,你应该更喜欢外部库的第一种方法,它更可靠和通用。