0

my goal is to create libraries like client and generator and use them in src/main.cpp, but sometimes these libraries depend each other.

In this case: client/User.hpp uses generator/IdGenerator.hpp

Project
│
├── CMakeLists.txt
├── libs
│   ├── CMakeLists.txt
│   ├── client
│   │   ├── CMakeLists.txt
│   │   ├── User.cpp
│   │   └── User.hpp
│   └── generator
│       ├── CMakeLists.txt
│       ├── IdGenerator.cpp
│       ├── IdGenerator.hpp
│       └── Types.hpp
└── src
    └── main.cpp

Project/CMakeLists.txt:

cmake_minimum_required (VERSION 3.8)

project(game-project VERSION 0.1.0)

set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_FLAGS "-Wall -Wextra -O0 -std=c++20")

add_executable (game src/main.cpp)

add_subdirectory(libs)

target_link_libraries(game libclient libgenerator)

libs/CMakeLists.txt:

add_subdirectory(generator)
add_subdirectory(client)

libs/client/CMakeLists.txt:

add_library(libclient STATIC
    User.cpp
    User.hpp
)

include_directories(generator/)
target_link_libraries(libclient libgenerator)

libs/generator/CMakeLists.txt:

add_library(libgenerator STATIC
    IdGenerator.cpp
    IdGenerator.hpp
    Types.hpp
)

C++ files:

main.cpp:

#include <client/User.hpp>

int main(int argc, const char* argv[])
{
    User user;
    return 0;
}

client/User.hpp:

#pragma once

#include <generator/IdGenerator.hpp>

class User
{
    Identifier id = IdGenerator::generateId();
};

When I run make after cmake, I get this error:

In file included from Project/libs/client/User.cpp:1:
Project/libs/client/User.hpp:3:10: fatal error: generator/IdGenerator.hpp: No such file or directory
    3 | #include <generator/IdGenerator.hpp>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

Sorry for the verbose summary, I was able to shorten the example this much. What do you think the problem is?

This question might seem like a duplicate of CMake libraries that depend on each other but as you can see I'm already applying the include_directories(generator/).

1 Answers1

0

I misunderstood what the problem was initially, but now I think this might help. Try adding to the CMakeLists.txt of your client instead of

include_directories(generator/)

this command

target_include_directories(libclient PUBLIC <Path to your generator file>)

Maybe you need to experiment a little to get the path correct as you might need to either specify it from the root directory or the current one (where this CMakeLists.txt resides), but it might solve your problem.

Geo
  • 56
  • 6
  • Is `${PROJECT_NAME}` accepted as a target? Unfortunately it says `target "game-project" which is not built by this project`. – Ömer Kurttekin Jul 10 '22 at 10:58
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 10 '22 at 10:59
  • @ÖmerKurttekin I think you are right. You have to use your target name. I edited my response. Does the answer do the trick for you in the end? – Geo Jul 10 '22 at 13:04
  • @Geo unfortunately it didn't change anything. – Ömer Kurttekin Jul 10 '22 at 13:44
  • After your edit, I put `target_include_directories(libclient PUBLIC ${CMAKE_SOURCE_DIR}/libs/generator)` in `libs/client/CMakeLists.txt` but the error is still the same. – Ömer Kurttekin Jul 10 '22 at 14:53
  • If I were you I would try to experiment with the path as I also wrote in my edit. I am not sure which path should be included. The problem you have is that during compiling, the path to the necessary header file for the creation of your library is not correct, so maybe focus on specifying the path correctly, since you know that the file you are searching for is there. In addition make sure this path is ok ```` #include ```` – Geo Jul 10 '22 at 15:01
  • You were right! I put `target_include_directories(libclient PUBLIC ${CMAKE_SOURCE_DIR}/libs/)` and it worked! Thank you so much. – Ömer Kurttekin Jul 10 '22 at 15:56
  • Keep in mind that this worked because in your source file you are including a path starting from generator/IdGenerator.hpp. If you shorten that path to just IdGenerator.hpp and add this to ${CMAKE_SOURCE_DIR}/libs/generator it will probably be ok again and you wont have to use long include paths in your source code. Glad I could help! – Geo Jul 10 '22 at 16:01