我是CMake的初学者,我会用它来编译我的项目。 我有以下CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (rt)
include_directories ("${PROJECT_BINARY_DIR}/src/")
include_directories ("${PROJECT_BINARY_DIR}/src/hook")
include_directories ("${PROJECT_BINARY_DIR}/src/light")
include_directories ("${PROJECT_BINARY_DIR}/src/move")
include_directories ("${PROJECT_BINARY_DIR}/src/object/inter")
include_directories ("${PROJECT_BINARY_DIR}/src/object/normal")
include_directories ("${PROJECT_BINARY_DIR}/src/option")
include_directories ("${PROJECT_BINARY_DIR}/src/parsing")
include_directories ("${PROJECT_BINARY_DIR}/src/ray")
include_directories ("${PROJECT_BINARY_DIR}/src/render")
include_directories ("${PROJECT_BINARY_DIR}/src/shadow")
include_directories ("${PROJECT_BINARY_DIR}/src/utils")
include_directories ("${PROJECT_BINARY_DIR}/lib/minilibx")
file(GLOB hook
"*.h"
"*.c"
)
file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.c)
#set (EXTRA_LIBS ${EXTRA_LIBS} ${X11_LIBRARIES} m)
add_executable (rt ${SRC_FILES})
target_link_libraries (rt ${EXTRA_LIBS} X11 Xext m ${PROJECT_BINARY_DIR}/lib/libmlx.a)
但是当我运行make
时,我的静态库minilibx
不会编译。错误,X11
和Xext
库中的所有函数都未定义。
使VERBOSE = 1结果:
链接C可执行文件rt / usr / bin / cmake -E cmake_link_script CMakeFiles / rt.dir / link.txt --verbose = 1 / usr / bin / cc CMakeFiles / rt.dir / src / move / translation.co CMakeFiles / rt.dir / src / move / rotation.co CMakeFiles / rt.dir / src / light / light.co CMakeFiles / rt.dir /src/option/option.co CMakeFiles / rt.dir / src / option / file_option.co CMakeFiles / rt.dir / src / render / render.co CMakeFiles / rt.dir / src / hook / control.co CMakeFiles / rt .dir / src / ray / eye.co CMakeFiles / rt.dir / src / ray / ray.co CMakeFiles / rt.dir / src / utils / my_strlen.co CMakeFiles / rt.dir / src / utils / my_strncpy.co CMakeFiles /rt.dir/src/object/inter/plan.co CMakeFiles / rt.dir / src / object / inter / cone.co CMakeFiles / rt.dir / src / object / inter / cylinder.co CMakeFiles / rt.dir / src / object / inter / sphere.co CMakeFiles / rt.dir / src / object / inter / tore.co CMakeFiles / rt.dir / src / object / normal / plan.co CMakeFiles / rt.dir / src / object / normal /cone.co CMakeFiles / rt.dir / src / object / normal / cylinder.co CMakeFiles / rt.dir / src / object / normal / sphere.co CMakeFiles / rt.dir / src / parsing / scene.co CMakeFiles / rt .dir / src / parsing / file_access.co CMakeFiles / rt.dir / src / parsing / str_tool.co CMak eFiles / rt.dir / src / shadow / shadow.c.o CMakeFiles / rt.dir / src / main.c.o -o rt -rdynamic -lX11 -lXext -lm lib / libmlx.a
这是一个错误的例子:
lib / libmlx.a(mlx_init.o):在函数mlx_int_deal_shm'中: mlx_init.c :(。text + 0x2a):undefined reference toXShmQueryVersion' mlx_init.c :(。text + 0x104):对XShmPixmapFormat'
的未定义引用如何解决未定义的参考错误?
答案 0 :(得分:0)
CMake使用target_link_libraries指定您链接的库。
在你的情况下,似乎你正在链接正确的库(target_link_libraries (rt ${EXTRA_LIBS} X11 Xext m ${PROJECT_BINARY_DIR}/lib/libmlx.a))
,所以我的赌注是他们找不到。
尝试跑步:
make VERBOSE=1
这将为您提供gcc输出。粘贴它在这里生成的最后几个gcc指令,我们就会明白为什么它找不到你的库。
答案 1 :(得分:0)
我已经解决了问题,库的顺序不正确。
我只是在另一个lib之前链接我的lib。 Etvoilà:)
target_link_libraries (rt ${EXTRA_LIBS} ${PROJECT_BINARY_DIR}/lib/libmlx.a X11 Xext m)