Reputation: 133
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/". 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.
I have an ExampleLib with:
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)
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:
ExampleCode
├── build
├── include
│ └── example.h
├── lib
│ └── libExample.so
├── src
│ └── main.c
└── CMakeLists.txt
Where I have the following code:
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})
But when I try to build it with cmake and make I get the following error:
[ 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.
Edit:
I corrected the CMakeList.txt to use a variable and now I have a different error message:
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
Upvotes: 0
Views: 2393
Reputation: 133
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.
Upvotes: 1