与 cmake 链接时对共享对象的未定义引用

Undefined reference to shared object when linking with cmake

我正在尝试创建一个共享对象库,我可以在不共享“.cpp”文件且无需将其安装在“/usr/”中的情况下与其他人共享。为了做到这一点,因为我不熟悉 cmake,所以我从一个最小的例子开始,但是我在导入库时遇到了问题。

我有一个 ExampleLib:

include/example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H    

namespace example {
class Example {    
public:
  Example();       
  bool is_working();    
}; 
} 

#endif 

src/example.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 所建议的那样。