Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include same header file for different libs

Tags:

c++

cmake

libs

I have a C++ project with the following structure:

CMakeLists.txt
code/
  libClient/
     CMakeLists.txt
     include/
        LibClient/
          lib_client.h
     src/
        lib_client.cpp
  libServer/
     CMakeLists.txt
     include/
        LibServer/
          lib_server.h
     src/
        lib_server.cpp
  libSDK/
     include/
       CMakeLists.txt
       LibSDK/
         lib.h

My root CMakeLists.txt has the following content:

cmake_minimum_required(VERSION 3.0)

project(libExample)


set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

add_subdirectory(code/libClient)
add_subdirectory(code/libServer)
add_subdirectory(code/libSDK)

Now, I would like to build the libs for the server and for the client as .a files. The lib_client.cpp includes the header files with

#include <LibClient/lib_client.h>

Up to here, it is all clear for me, but the header file lib_client.h includes the lib.h which is located at /code/libSDK/include/LibSDK/lib.h with

#ifndef LIBCLIENT_H
#define LIBCLIENT_H

#include <string>
#include <LibSDK/lib.h>

To make this work, I wrote the following content in the CMakelists.txt for the ClientLib:

project(LIBCLIENT)

add_library(
    Client_lib STATIC
    src/lib_client.cpp
        include/LibClient/lib_client.h
    )

target_include_directories(egoClient_lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(egoClient_lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../libSDK/include")   ###this line I think should be different.

My question is now, is it possible to avoid this line with the hard coded path? This lib.h is also used (included) in other header files in this project.

The CMakelists.txt for this lib.h has up to now only the content:

cmake_minimum_required(VERSION 3.0)

project(LIBSDK)

I thought about using the CMake find_package() method and write a Find<package>.cmake file, but I do not see any advantages of this because inside this file I also have to write the paths?

Thank you very much in advance.

like image 553
Morgijl Avatar asked Dec 01 '25 23:12

Morgijl


1 Answers

If Client_lib uses SDK_lib, you'll want to use the following in Client_lib's CMakeLists.txt:

target_link_libraries(Client_lib SDK_lib)

This will tell CMake that Client_lib uses SDK_lib. It will also automatically make the PUBLIC or INTERFACE include paths configured on SDK_lib (with target_include_directories in SDK_lib's CMakeLists.txt) available for Client_lib.

like image 88
Mike.nl Avatar answered Dec 04 '25 14:12

Mike.nl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!