英文:
CMake link external shared lib on linux?
问题
我尝试在Linux上使用CMake链接外部库。编译和构建都正常。但当我运行它时,它显示以下错误:
./test/calculator_test: error while loading shared libraries: ../../../third_party/add/lib/add.so: cannot open shared object file: No such file or directory
文件树(我删除了一些不必要的部分):
├── CMakeLists.txt
├── build
├── src
├── test
│ ├── CMakeLists.txt
│ └── calculator
│ ├── CMakeLists.txt
│ └── calculator_test.cpp
└── third_party
├── add
│ ├── include
│ │ └── add.h
│ └── lib
│ └── add.so
└── googletest
...
test/calculator/CMakeLists.txt:
add_library(lib-add SHARED IMPORTED)
set_target_properties(lib-add
PROPERTIES
IMPORTED_LOCATION "${PROJECT_SOURCE_DIR}/third_party/add/lib/add.so"
INTERFACE_INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/third_party/add/include"
)
add_executable(calculator_test calculator_test.cpp)
target_link_libraries(calculator_test PRIVATE lib-add lib-sub)
set_target_properties(calculator_test
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test"
COMMAND calculator_test
)
那么为什么会出现这个错误,如何修复它呢?
英文:
I try to link an external libaray on Linux in CMake.
The compile and make are fine.
But when I run it, it says:
./test/calculator_test: error while loading shared libraries: ../../../third_party/add/lib/add.so: cannot open shared object file: No such file or directory
File tree (I remove some unnecessary part):
├── CMakeLists.txt
├── build
├── src
├── test
│ ├── CMakeLists.txt
│ └── calculator
│ ├── CMakeLists.txt
│ └── calculator_test.cpp
└── third_party
├── add
│ ├── include
│ │ └── add.h
│ └── lib
│ └── add.so
└── googletest
...
test/calculator/CMakeLists.txt:
add_library(lib-add SHARED IMPORTED)
set_target_properties(lib-add
PROPERTIES
IMPORTED_LOCATION "${PROJECT_SOURCE_DIR}/third_party/add/lib/add.so"
INTERFACE_INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/third_party/add/include"
)
add_executable(calculator_test calculator_test.cpp)
target_link_libraries(calculator_test PRIVATE lib-add lib-sub)
set_target_properties(calculator_test
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test"
COMMAND calculator_test
)
So why and how to make it correct?
答案1
得分: 0
我认为这是CMake的一个错误。
我在使用3.22.1(Ubuntu 22.04)时遇到了这个问题。升级到CMake 3.27后问题解决了。看起来这个问题在CMake 3.24中已经修复了。
英文:
I think it is a bug of cmake.
I see the problem when using 3.22.1 (Ubuntu 22.04). After upgrading to cmake 3.27 it works. It seems it was fixed in cmake 3.24.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论