Using shared library created in a different directory - c++

I've created a shared library with library.h and library.cpp. Then wrote a CMakeLists.txt file to build it as a shared library.
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(test_pro)
set(CMAKE_CXX_STANDARD 11)
add_library(test_pro SHARED library.cpp library.h)
after building the library, I was able to get a .so file as /home/user/projects/test_lib/bin/libtest_pro.so
Then I tried linking the created library to another project in /home/user/projects/testproject
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(testproject)
set(CMAKE_CXX_STANDARD 11)
link_directories(
/home/user/projects/test_lib/bin
)
add_executable(testproject main.cpp)
target_link_libraries (testproject test_pro)
It successfully builds the testproject (ldd command shows it has linked correctly), but I'm unable to use the library I've created in it.
In the main.cpp I've tried,
#include "library.h"
#include "test_pro"
#include <test_pro>
#include <test_pro/library.h>
But all the above gave build failures (fatal error: xxx: No such file or directory). How do I use this created library?

Just like you set link_directories() you have to specify include_directories(). And it is recommended not to use link_directories() at all, instead pass absolute path to the library into target_link_libraries().

Related

Require including <mylibrary/mylibrary.h> instead of <mylibrary.h> inside Cmake project that links to mylibrary

I have a very simple c++ shared library with a CMakeLists.txt file:
cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 23)
project(mylibrary)
add_library (mylibrary src/mylibrary.h src/mylibrary.cpp)
target_include_directories(mylibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
Now in the project that uses this library I link to this library with target_link_libraries(myapp mylibrary) and then I can include the library with #include <mylibrary.h>. However, to visually separate all library includes I want to (have to) use #include <mylibrary/mylibrary.h>, even though mylibrary.h is not inside a folder called mylibrary. Is there a way to achieve this with Cmake?

Why add_library() creates a codeblocks project instead of a .a file?

I'm trying to build a static library via CMake. I have this CMakeList.txt:
cmake_minimum_required(VERSION 3.12)
project(Test VERSION 1.0.0)
add_executable(Test main.cpp library.cpp)
add_library(MyLib STATIC library.cpp)
where library.cpp contains one function.
When I hit the build button, I find a Test.cbp file in one of build folders. Why isn't it a .a library? I'm using MacOS.

Linking two libraries together with CMake

File Structure:
CMakeLists.txt
src/
evolution.cpp
evolution.h
main.cpp
helpers/
disposable.h
engine/
game.h
game.cpp
CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(evolution)
add_library(helpers
src/helpers/disposable.h)
set_target_properties(helpers PROPERTIES LINKER_LANGUAGE CXX)
add_library(engine
src/engine/game.h
src/engine/game.cpp)
add_executable(evolution src/main.cpp)
target_link_libraries(evolution engine helpers)
The Game class (game.h) inherits from Diposable (disposable.h), but I am unable to build the project with a simple #include "disposable.h" in the game.h file.
How do I configure CMake so that the engine library can see the helpers library?
CMake error:
[ 20%] Building CXX object CMakeFiles/engine.dir/src/engine/game.cpp.o
In file included from /home/shane/projects/evolution/src/engine/game.cpp:1:
/home/shane/projects/evolution/src/engine/game.h:4:10: fatal error: disposable.h: No such file or directory
4 | #include "disposable.h"
| ^~~~~~~~~~~~~~
compilation terminated.
Adding headers to a project really only helps IDEs find the headers. It doesn't change the build instructions. Instead you need to use target_include_directories() with a INTERFACE or PUBLIC section to get any libraries that link to this to add the include directories to itself.
Your helpers library is more of an interface for now. To get this minimum example working do this:
cmake_minimum_required(VERSION 3.17)
project(evolution)
add_library(helpers INTERFACE) # INTERFACE is good for header-only libraries
target_include_directories(helpers INTERFACE src/helpers)
add_library(engine
src/engine/game.h
src/engine/game.cpp)
add_executable(evolution src/main.cpp)
target_link_libraries(evolution engine helpers)
Now include with angular brackets: #include <disposable.h>
If your library becomes more complicated (more than just header files), then replace INTERFACE in add_library with your sources. To make include directories available to the helpers library and any library that links to it, change INTERFACE to PUBLIC in target_include_directories.
cmake_minimum_required(VERSION 3.17)
project(evolution)
add_library(helpers src/helpers/helper.cpp)
target_include_directories(helpers PUBLIC src/helpers)
add_library(engine
src/engine/game.h
src/engine/game.cpp)
add_executable(evolution src/main.cpp)
target_link_libraries(evolution engine helpers)

Trying to add libraries with CMake results in error

I am trying to add an external .lib file to my project in Clion which uses CMake. My code is very simple and is simply to test whether the library gets included:
#include <iostream>
#include "header/test.h"
int main() {
test a; // returns error saying undefined reference to 'test::test()'
return 0;
}
When running this code I get the following error:
undefined reference to `test::test()'
This is because I am trying to make a test object however the library for test is not included.
The test.lib file and the test.h file are both in the "header" folder which is in the root of my project folder. The file path to this is F:\Project\header\
My Cmake text file is as follows:
cmake_minimum_required(VERSION 3.14)
project(Project)
set(CMAKE_CXX_STANDARD 14)
add_executable(Project main.cpp)
target_link_libraries(Project
F:\\Project\\header\\test.lib)
In the cmake text file i use the line:
target_link_libraries(Project F:\Project\header\test.lib)
This should include the library file, however it doesn't seem to because I get the "undefined reference to..." error as mentioned above. The Cmake compiler does not give me an error.
You are conceptually correct, however you are not doing it in the CMake fashion. Check out the following links on how to link an external library.
CMake link to external library
cmake doesn't support imported libraries?
https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Exporting-and-Importing-Targets
For your case, it would be as follows):
cmake_minimum_required(VERSION 3.14)
project(Project)
set(CMAKE_CXX_STANDARD 14)
# Import the library into the CMake build system
ADD_LIBRARY(test SHARED IMPORTED)
# Specify the location of the library
SET_TARGET_PROPERTIES(TARGET test PROPERTIES IMPORTED_LOCATION “/path/to/lib/test.dll”)
# create the executable
add_executable(Project main.cpp)
# Link your exe to the library
target_link_libraries(Project test)
The CMake documentation is very good. I recommend checking it out when you run into issues.
https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries

Should I use only add_executable() with raw cpp files or make a library via add_library()?

I'm learning CMake and I'm struggling with it a little. My "project" is using JsonCpp "library" that was provided as one .cpp file and two .h files. The structure looks like this:
myProject
build/
json/
CMakeLists.txt
jsoncpp.cpp
include/
json.h
json-forward.h
CMakeLists.txt
main.cpp
build/CMakeLists.txt:
cmake_minimum_required(VERSION 3.6.0)
project(myProject)
add_subdirectory(json)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE json)
# add_executable(app main.cpp json/jsoncpp.cpp json/include/json.h json/include/json-forwards.h)
json/CMakeLists.txt:
cmake_minimum_required(VERSION 3.6.0)
add_library(
json
jsoncpp.cpp
include/json.h
include/json-forwards.h
)
target_include_directories(json PUBLIC '${CMAKE_CURRENT_SOURCE_DIR}/include')
What's a difference between using only add_executable() with all .cpp files and using target_link_libraries that transforms jsoncpp into static library and then link it? What approach should I choose?
A next thing confusing me is a target_include_directories(). What are the benefits using this function? If I comment it, and run cmake (then makefile and launch the app) everything still works fine. If I delete "include/json.h" "include/json-forward.h" from add_library(), everything still works.
What's a difference between using only add_executable() with all .cpp files and using target_link_libraries that transforms jsoncpp into static library and then link it? What approach should I choose?
Using add_library is required when you have 2 executables using the same jsoncpp code. In this case, if you list jsoncpp sources in both add_executable() calls, you'd have to compile it twice. Grouping them into add_library() will make it compile only once and then linked to both executables.
Another reason to use add_library is purely logical composition of modules.