简体   繁体   English

cmake链接到共享库而不使用完整路径

[英]cmake link to shared libraries without using full path

I have following CMakeLists.txt: 我有以下CMakeLists.txt:

set( PROJECT_LINK_LIBS lib1.so lib2.so )
link_directories( path/to/libs ) # lib1.so and lib2.so are there.

add_library( ${PROJECT_NAME} SHARED ${PROJECT_SOURCES} )
target_link_libraries( ${PROJECT_NAME} ${PROJECT_LINK_LIBS} )

Which compiles and links fine. 哪个编译和链接很好。

But when I do: 但当我这样做时:

ldd -d mylib.so

I get: 我明白了:

  1. libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf529b000)
  2. linux-gate.so.1 => (0xf777a000)
  3. /lib/ld-linux.so.2 (0xf777b000)
  4. lib1.so => /path/to/libs/lib1.so (0xf56a2000)
  5. lib2.so => /path/to/libs/lib2.so (0xf548f000)

My questions are: 我的问题是:

  • How to remove /path/to/libs/ for 4. and 5.? 如何删除/ path / to / libs / for 4和5.? Something to do with LD_LIBRARY_PATH? 与LD_LIBRARY_PATH有关吗?
  • Auto-answered: I get what means 1. Search for libc.so.6 in that path. 自动回答:我明白了1.在该路径中搜索libc.so.6。 But what about 2. and 3.? 但是2.和3.怎么样? locate linux-gate.so.1 gives nothing. locate linux-gate.so.1什么都没有。 Why 3. has no => symbol? 为什么3.没有=>符号? (found answer here ) (在这里找到答案)

Ok found the answer: 好的找到了答案:

set( CMAKE_SKIP_BUILD_RPATH true )

That did it. 这样做了。

Do you have an actual problem or are you just confused with the out put of ldd ? 你有实际问题还是只是与ldd的输出相混淆? To answer your question: If you successfully compile and link your library on Linux, the full paths to your external libraries will be stored. 回答您的问题:如果您在Linux上成功编译并链接了库,则将存储外部库的完整路径。 If you now copy or install (using CMake) your library, the full libraries paths will be stripped. 如果您现在复制或安装(使用CMake)库,则将剥离完整的库路径。 Now, you will have to make sure that the correct paths are provided by LD_LIBRARY_PATH or that the external libs reside in the same folder your library is in. 现在,您必须确保LD_LIBRARY_PATH提供了正确的路径,或者外部库驻留在您的库所在的同一文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM