CMake如何在Linux上链接外部共享库?

huangapple go评论101阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年8月10日 14:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定