gpt4 book ai didi

c - 如何使用 cmake 静态共享库

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:27 25 4
gpt4 key购买 nike

我是 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 不会编译。 X11Xext 库中的所有函数都未定义的错误。

使 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.c.o CMakeFiles/rt.dir/src/move/rotation.c.o CMakeFiles/rt.dir/src/light/light.c.o CMakeFiles/rt.dir/src/option/option.c.o CMakeFiles/rt.dir/src/option/file_option.c.o CMakeFiles/rt.dir/src/render/render.c.o CMakeFiles/rt.dir/src/hook/control.c.o CMakeFiles/rt .dir/src/ray/eye.c.o CMakeFiles/rt.dir/src/ray/ray.c.o CMakeFiles/rt.dir/src/utils/my_strlen.c.o CMakeFiles/rt.dir/src/utils/my_strncpy.c.o CMakeFiles/rt.dir/src/object/inter/plan.c.o CMakeFiles/rt.dir/src/object/inter/cone.c.o CMakeFiles/rt.dir/src/object/inter/cylinder.c.o CMakeFiles/rt.dir/src/对象/inter/sphere.c.o CMakeFiles/rt.dir/src/object/inter/tore.c.o CMakeFiles/rt.dir/src/object/normal/plan.c.o CMakeFiles/rt.dir/src/object/normal/cone.c.o CMakeFiles/rt.dir/src/object/normal/cylinder.c.o CMakeFiles/rt.dir/src/object/normal/sphere.c.o CMakeFiles/rt.dir/src/parsing/scene.c.o CMakeFiles/rt .dir/src/parsing/file_access.c.o CMakeFiles/rt.dir/src/parsing/str_tool.c.o 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): 未定义对 XShmQueryVersion 的引用'mlx_init.c:(.text+0x104): 未定义对“XShmPixmapFormat”的引用

如何解决 undefined reference 错误?

最佳答案

我已经解决了问题,库的顺序不正确。

我只是将我的库链接到另一个库之前。 Et voilà :)

target_link_libraries (rt ${EXTRA_LIBS} ${PROJECT_BINARY_DIR}/lib/libmlx.a X11 Xext m)

关于c - 如何使用 cmake 静态共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30961378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com