gpt4 book ai didi

gcc与cmake编译的编译区别——ldd命令

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:45 26 4
gpt4 key购买 nike

我需要使用 cmake 创建共享库。它可以编译,但 ldd 命令显示未找到所有链接。

GCC 构建:

gcc -g -Wall -O0 -m64 -Wl,--unresolved-symbols=ignore-in-shared-libs  -fPIC -shared -o libcore.so  -Iinclude -Iinc -Itls -Llib64 -Wl,-rpath,protocol_so:lib64:libwifi -DLINUX -DCPU_64 -lprotocol_common -lthread -lvip -lcrypto -lglib-2.0 -lm -lpthread -lxml2

ldd命令:

> ldd libcore.so 
linux-vdso.so.1 => (0x00007fff30fff000)
libachk.so => /lib64/libachk.so (0x00007f2d9d7b5000)
libprotocol_common.so => lib64/libprotocol_common.so (0x00007f2d9d6b1000)
libthread.so => lib64/libthread.so (0x00007f2d9d5af000)
libvip.so => lib64/libvip.so (0x00007f2d9d20d000)
libcrypto.so.1.0.0 => lib64/libcrypto.so.1.0.0 (0x00007f2d9cef2000)
libglib-2.0.so.0 => lib64/libglib-2.0.so.0 (0x00007f2d9cd19000)
libm.so.6 => /lib64/libm.so.6 (0x00007f2d9caa0000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2d9c882000)
libxml2.so.2 => /usr/lib64/libxml2.so.2 (0x00007f2d9c526000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2d9c1af000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f2d9bfaa000)
librt.so.1 => /lib64/librt.so.1 (0x00007f2d9bda1000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f2d9ba9b000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f2d9b884000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2d9dbf1000)
libz.so.1 => /lib64/libz.so.1 (0x00007f2d9b66e000)

现在我需要用 cmake 构建这个库。CMake 脚本:

cmake_minimum_required(VERSION 3.2)
project(core_lib C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_AUTOGEN_VERBOSE ON)

set (CMAKE_C_FLAGS_COMMON "-g -std=gnu89 -Wall -O0 -m64 -fPIC -DLINUX -DCPU_64")
if(NOT CMAKE_C_CREATE_SHARED_LIBRARY)
set(CMAKE_C_CREATE_SHARED_LIBRARY
"<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
endif()

file(GLOB SOURCE_FILES
*.h
*.c
)
add_library(core_lib SHARED
${SOURCE_FILES}
)
set_target_properties(core_lib PROPERTIES LINKER_LANGUAGE C)
set_target_properties(core_lib PROPERTIES COMPILE_FLAGS " ${CMAKE_C_FLAGS_COMMON}")
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--unresolved-symbols=ignore-in-shared-libs -shared -Wl,-rpath,protocol_so:lib64:libwifi")
target_link_libraries(core_lib -L${PROJECT_SOURCE_DIR}/..)
target_link_libraries(core_lib -L${PROJECT_SOURCE_DIR}/../lib64)
target_link_libraries(core_lib -lprotocol_common -lthread -lvip -lcrypto -lglib-2.0 -lm -lpthread -lxml2)
target_include_directories(core_lib PUBLIC
${PROJECT_SOURCE_DIR}/../include
${PROJECT_SOURCE_DIR}/../inc
${PROJECT_SOURCE_DIR}/../tls
)

一些生成的命令:

/usr/bin/gcc  -Dcore_lib_EXPORTS -g -fPIC -I/home/user/cmake_libs/core_c/../include -I/home/user/cmake_libs/core_c/../inc -I/home/user/cmake_libs/core_c/../tls  -g -std=gnu89 -Wall -O0 -m64 -fPIC -DLINUX -DCPU_64 -o CMakeFiles/core_lib.dir/log.c.o   -c /home/user/cmake_libs/core_c/log.c

/usr/bin/gcc -fPIC -g -Wl,--unresolved-symbols=ignore-in-shared-libs -shared -Wl,-rpath,protocol_so:lib64:libwifi -shared -Wl,-soname,libcore_lib.so -o libcore_lib.so CMakeFiles/core_lib.dir/log.c.o CMakeFiles/core_lib.dir/logic.c.o CMakeFiles/core_lib.dir/json.c.o CMakeFiles/core_lib.dir/msg.c.o CMakeFiles/сore_lib.dir/buf.c.o -L/home/user/cmake_libs/core_c/.. -L/home/user/cmake_libs/core_c/../lib64 -lprotocol_common -lthread -lvip -lcrypto -lglib-2.0 -lm -lpthread -lxml2

ldd命令:

> ldd libcore_lib.so 
linux-vdso.so.1 => (0x00007fff329ff000)
libachk.so => /lib64/libachk.so (0x00007f9a91007000)
libprotocol_common.so => not found
libthread.so => not found
libvip.so => not found
libcrypto.so.1.0.0 => not found
libglib-2.0.so.0 => /usr/lib64/libglib-2.0.so.0 (0x00007f9a90d40000)
libm.so.6 => /lib64/libm.so.6 (0x00007f9a90ac7000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f9a908a9000)
libxml2.so.2 => /usr/lib64/libxml2.so.2 (0x00007f9a9054d000)
libc.so.6 => /lib64/libc.so.6 (0x00007f9a901d6000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f9a8ffd1000)
librt.so.1 => /lib64/librt.so.1 (0x00007f9a8fdc8000)
libpcre.so.0 => /usr/lib64/libpcre.so.0 (0x00007f9a8fb98000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9a9146a000)
libz.so.1 => /lib64/libz.so.1 (0x00007f9a8f981000)

为什么在 cmake build 中找不到一些链接?我认为构建版本之间没有区别。

我错过了什么?可能是链接阶段的一些标志?

UPD。

同样有趣的是,gcc 链接了我的 lib64 文件夹中的库。

Cmake 链接来自/usr/lib64,但我没有添加此路径。 (例如 libglib-2.0.so.0)

为什么 cmake 会忽略我自定义的 libs (lib64) 路径?

最佳答案

正如@Someprogrammerdude 所说,

Make sure that the paths for both -L and -Wl,-rpath matches. The important option here is the -Wl,-rpath option.

关于gcc与cmake编译的编译区别——ldd命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57395538/

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