[英]Undefined reference to shared object when linking with cmake
I am trying to create a shared object library that I can share with another person without sharing the ".cpp" files and without having to install it in "/usr/".我正在尝试创建一个共享的 object 库,我可以在不共享“.cpp”文件且无需将其安装在“/usr/”中的情况下与其他人共享。 In order to do so, since I am inexpert with cmake, I am starting with a minimal example, but I am having problems when importing the library.为了做到这一点,因为我不熟悉 cmake,所以我从一个最小的例子开始,但是我在导入库时遇到了问题。
I have an ExampleLib with:我有一个 ExampleLib:
include/example.h包括/example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
namespace example {
class Example {
public:
Example();
bool is_working();
};
}
#endif
src/example.c源代码/例子.c
#include "example.h"
namespace example {
Example::Example() {};
bool Example::is_working(){
return true;
}
}
CMakeLists.txt 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)
I am building it with cmake and make and then I am copying the ".so" and ".h" code to another project, project with the following structure:我用 cmake 和 make 构建它,然后我将“.so”和“.h”代码复制到另一个项目,项目具有以下结构:
ExampleCode
├── build
├── include
│ └── example.h
├── lib
│ └── libExample.so
├── src
│ └── main.c
└── CMakeLists.txt
Where I have the following code:我有以下代码的地方:
src/main.c 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 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})
But when I try to build it with cmake and make I get the following error:但是当我尝试使用 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
I am pretty sure that my mistake is in the last CMakeLists.txt and I am aware that there are similar questions asked, however, I have not been able to find the solution in this case.我很确定我的错误在最后一个 CMakeLists.txt 中,我知道有类似的问题被问到,但是,在这种情况下我无法找到解决方案。
Edit:编辑:
I corrected the CMakeList.txt to use a variable and now I have a different error message:我更正了 CMakeList.txt 以使用变量,现在出现了不同的错误消息:
CMakeLists.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)
outcome结果
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
The problem was fixed changing问题是固定的改变
${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)
As the user @KamilCuk suggested.正如用户@KamilCuk 所建议的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.