![](/img/trans.png)
[英]C++ CMake undefined reference when linking an executable to a third party dependant shared library
[英]Undefined reference to shared object when linking with cmake
我正在嘗試創建一個共享的 object 庫,我可以在不共享“.cpp”文件且無需將其安裝在“/usr/”中的情況下與其他人共享。 為了做到這一點,因為我不熟悉 cmake,所以我從一個最小的例子開始,但是我在導入庫時遇到了問題。
我有一個 ExampleLib:
包括/example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
namespace example {
class Example {
public:
Example();
bool is_working();
};
}
#endif
源代碼/例子.c
#include "example.h"
namespace example {
Example::Example() {};
bool Example::is_working(){
return true;
}
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project (Example)
set(CMAKE_CXX_STANDARD 11)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(Example SHARED src/example.cpp)
我用 cmake 和 make 構建它,然后我將“.so”和“.h”代碼復制到另一個項目,項目具有以下結構:
ExampleCode
├── build
├── include
│ └── example.h
├── lib
│ └── libExample.so
├── src
│ └── main.c
└── CMakeLists.txt
我有以下代碼的地方:
src/main.c
#include <iostream>
#include "../include/example.h"
int main(int argc, char** argv)
{
example::Example ex;
if(ex.is_working()){
std::cout << "It is working. \n";
}
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project (testing_example)
set(CMAKE_CXX_STANDARD 11)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(EXAMPLELIB SHARED IMPORTED)
set_property(TARGET EXAMPLELIB PROPERTY IMPORTED_LOCATION "lib/libExample.so")
add_executable(testing_example src/main.cpp)
target_link_libraries(testing_example PRIVATE ${EXAMPLELIB})
但是當我嘗試使用 cmake 構建它並 make 我收到以下錯誤:
[ 50%] Building CXX object CMakeFiles/testing_example.dir/src/main.cpp.o
[100%] Linking CXX executable testing_example
/usr/bin/ld: CMakeFiles/testing_example.dir/src/main.cpp.o: in function `main':
main.cpp:(.text+0x2a): undefined reference to `example::Example::Example()'
/usr/bin/ld: main.cpp:(.text+0x36): undefined reference to `example::Example::is_working()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/testing_example.dir/build.make:84: testing_example] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/testing_example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
我很確定我的錯誤在最后一個 CMakeLists.txt 中,我知道有類似的問題被問到,但是,在這種情況下我無法找到解決方案。
編輯:
我更正了 CMakeList.txt 以使用變量,現在出現了不同的錯誤消息:
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project (testing_example)
set(CMAKE_CXX_STANDARD 11)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(EXAMPLELIB SHARED IMPORTED)
set_property(TARGET EXAMPLELIB PROPERTY IMPORTED_LOCATION "lib/libExample.so")
add_executable(testing_example src/main.cpp)
target_link_libraries(testing_example PRIVATE EXAMPLELIB)
結果
make[2]: *** No rule to make target 'lib/libExample.so', needed by 'testing_example'. Stop.
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/testing_example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
問題是固定的改變
${EXAMPLELIB} -> EXAMPLELIB
set_property(TARGET EXAMPLELIB PROPERTY IMPORTED_LOCATION "lib/libExample.so") ->
set_property(TARGET EXAMPLELIB PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/lib/libExample.so)
正如用戶@KamilCuk 所建議的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.