CMake can't find file or directory - c++

I'm working on a CMake project with multiple subdirectories and I can't get it to work. My working directory is the following:
├───main.cpp
├───CMakeLists.txt
├───build
├───States
└───CMakeLists.txt
└───Elevator
├───CMakeLists.txt
Once I build the project, I get Elevator/Elevator.h: No such file or directory as an error. The way my project is set up, Elevator is included in States and apparently CMake isn't linking them properly.
My root CMakeLists.txt:
cmake_minimum_required(VERSION 3.21.4)
project(Test)
set(CMAKE_CPP_STANDARD 11)
set(CMAKE_CPP_STANDARD_REQUIRED True)
add_subdirectory(Elevator)
add_subdirectory(States)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC Elevator States)
The one in Elevator:
set(elevator_source_files
Elevator.cpp
Elevator.h
Set.cpp
Set.h
)
add_library(Elevator ${elevator_source_files})
The CMakeLists.txt in States:
set(state_source_files
State.h
State.cpp
InitialState.h
InitialState.cpp
EmergencyState.cpp
EmergencyState.h
IdleState.h
IdleState.cpp
MaintenanceState.h
MaintenanceState.cpp
MovingState.h
MovingState.cpp
AllStates.h
FSM.h
FSM.cpp
)
add_library(States ${state_source_files})
target_include_directories(States PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..)
target_link_libraries(States PUBLIC Elevator)
I'm still a novice so any help would be appreciated! It's worth noting that States.h includes Elevator with #include "Elevator/Elevator.h"
UPDATE: The project now builds and runs. I updated the CMake files in the description.

For #include "Elevator/Elevator.h" to work in the States library you need to include the folder containing the Elevator folder.
One way to fix this is to change
target_include_directories(States PUBLIC ${CMAKE_CURRENT_LIST_DIR})
to
target_include_directories(States PUBLIC ${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/..)
to have CMake add the State folder and its parent to the include directories.

Related

Facing problems in my first time handling CMake, Third party(header only) libraries

I want to use the following library
https://github.com/gmeuli/caterpillar
It's documentation says that it's a header-only library, and that I should "directly integrate it into my source files with #include <caterpillar/caterpillar.h>." It also depends on a few other libraries, one of which I need to use directly as well.
So far I have done the following:
create cmake project to make an 'executable' (with the vscode extension)
created a 'lib' folder, inside which I did
git clone https://github.com/gmeuli/caterpillar
Then, I did include_directories(lib) in my cmake file.
But #include <caterpillar/caterpillar.h> doesn't quite work in my singular main.cpp file.
I played around with various CMake functions, and it either gave the error "No such file or directory" regarding caterpillar/caterpillar.h itself, or it gave "cannot open source file... dependent of caterpillar/caterpillar.h" depending on how I messed with the cmake file.
For reference:
cat ~/project/main.cpp
#include <caterpillar/caterpillar.hpp>
#include <lorina/lorina.hpp> //how do I include this ? it's in the lib folder of caterpillar itself, or do I need to have a copy of it in my lib folder too
int main()
{
// stuff in lorina:: namespace
// stuff in caterpillar:: namespace
return 0;
}
cat ~/project/CMakeLists.txt
include_directories(lib)
//... rest is stuff like CXX standard, etc etc
tree ~/project
main.cpp
lib/
caterpillar/
build/
cmake generated stuff
CMakeLists.txt
Firstly, modern cmake recommends target_include_directories() instead of old include_directories() for better scope management.
Actually <caterpillar/caterpillar.hpp> is not in $PROJECT_SOURCE_DIR/lib directory. That's why your code not works.
CMakeLists example:
cmake_minimum_required(VERSION 3.22)
project(myproject)
set(CMAKE_CXX_STANDARD 17)
add_executable(my_project main.cpp)
target_include_directories(my_project PRIVATE ${PROJECT_SOURCE_DIR}/lib/caterpillar/include)
# project_src_dir/lib/catepillar/include/ is the directory where you find the headers like <catepillar/catepillar.hpp>
target_include_directories(my_project PRIVATE ${PROJECT_SOURCE_DIR}/lib/caterpillar/lib/lorina)
caterpillar's document describes how to include their headers in a traditional style, assuming the readers could understand this and decide where to put the headers themselves. (which means you don't need the whole git repo but only the "include" dir.)
For this specific problem, the library has provided a detailed CMakeLists.txt for users to include:
cmake_minimum_required(VERSION 3.22)
project(my_project)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(lib/caterpillar)
# this works because "project_src_dir/lib/catepillar/CMakeLists.txt" exists.
add_executable(my_project main.cpp)
target_link_libraries(my_project PRIVATE caterpillar)
# you need to tell cmake to add all catepillar settings into your project

Include same header file for different 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.
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.
Firstly, if your libClient and libServer depend on libSDK, you should rearrange your top-level CMake to look like the below. Also, based on your example project structure, your add_subdirectory() for the SDK should be code/libSDK/include:
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/libSDK/include)
add_subdirectory(code/libClient)
add_subdirectory(code/libServer)
You want to be sure to gather information about you SDK first, before traversing to the client and server directories.
Secondly, if you do not intend to build libSDK into a library, but simply use its headers, you can just define a new variable in your libSDK CMake file to specify the location of the headers:
project(LIBSDK)
set(SDK_INCLUDE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "My SDK headers directory.")
Now, with a cached variable from the libSDK directory, we can use it in the other CMake files:
project(LIBCLIENT)
add_library(Client_lib STATIC
src/lib_client.cpp
)
target_include_directories(Client_lib PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include"
"${SDK_INCLUDE_DIRECTORY}"
)
EDIT: As commented, to avoid the use of cache variables, you can also set the include directories variable with PARENT_SCOPE to make the variable accessible from your client and server CMake files:
project(LIBSDK)
set(SDK_INCLUDE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)

cmake: Include directories root?

I'm trying to set up cmake for a project I'm working on, but I have a problem which I can't resolve currently. My project has the following folder structure:
MotorEngine (root dir)
| CMakeLists.txt
| ThirdParty
|-| SDL2
|-|-| include (contains all header files for SDL2)
|-|-| lib
|-|-|-| x64
|-|-|-|-| SDL2.lib (the library file I need to link with)
| Source
|-| CMakeLists.txt
|-| main.cpp
The root CMakeLists.txt file:
cmake_minimum_required(VERSION 2.6)
project(MotorEngine)
# Set an output directory for our binaries
set(BIN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Binaries)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries)
set(THIRDPARTY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty)
# Include SDL2
include_directories(${THIRDPARTY_PATH}/SDL2/include)
# Add the engine + third party subdirectory
add_subdirectory(Source)
The Source's CMakeLists.txt:
project(MotorEngine)
add_executable(MotorEngine main.cpp)
target_link_libraries(MotorEngine ${THIRDPARTY_PATH}/SDL2/lib/x64/SDL2.lib)
Now, I want to achieve the following, in the main.cpp I want to write
#include "SDL2/include/SDL2.h"
But currently I have to write
#include "SDL2.h"
Since there will be files with the same name later on, I need to distinguish them in their folders. So the easiest would be to add the "ThirdParty" folder as a root so I can use #include relative to that then, but doing
include_directories(${THIRDPARTY_PATH})
does not work. Any ideas? Thank you!
With help of k.v. I was able to sort this out. I needed to add the following in the CMakeLists.txt in the Source directory:
target_include_directories(MotorEngine PUBLIC ${THIRDPARTY_PATH})

CMake doesn't include header directory of submodule A within submodule B

I have a CMake project that looks like this:
project/
CMakeLists.txt
subprojectA/
CMakeLists.txt
include/
headerA.hpp
src/
libraryA.cpp
subprojectB/
CMakeLists.txt
src/
mainB.cpp
The "library" subproject, A, is compiled as a static library, becoming libsubprojectA.a. The "main" project, B, is compiled as a binary and depends on the library. mainB.cpp includes a reference to headerA.hpp.
Here is subprojectA/CMakeLists.txt:
project(SubProjectA)
include_directories(include)
add_library(subprojectA STATIC src/libraryA.cpp)
set(${PROJECT_NAME}_INCLUDE_DIRS
${PROJECT_SOURCE_DIR}/include
CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
And here is subprojectB/CMakeLists.txt:
project(SubProjectB)
include_directories(${SubProjectA_INCLUDE_DIRS})
add_executable(mainBinary src/mainB.cpp)
target_link_libraries(mainBinary subprojectA)
The main Project CMakeLists.txt looks like:
project(Project)
add_subdirectory(subprojectB)
add_subdirectory(subprojectA)
Note that subprojectB, the main project, is listed before subprojectA.
Here's the problem. When I first run "cmake" on this project, ${SubProjectA_INCLUDE_DIRS} is not set within SubProjectB.
What I think is happening is that the CMakeLists for SubProjectB loads first, when ${SubProjectA_INCLUDE_DIRS} has not yet been set. It sets its own include path to an empty string as a result. However, even though libsubprojectA.a gets built successfully before mainBinary, the include path was already set empty beforehand. As a result, I get this error when trying to make mainBinary:
subprojectB/src/mainB.cpp:1:23: fatal error: headerA.hpp: No such file or directory
#include "headerA.hpp"
^
It's a workaround to put subprojectA before subprojectB in the main Project CMakeLists in the declarative world of CMake. What I really want is to know the proper way to indicate to CMake that the include_directories(${SubProjectA_INCLUDE_DIRS}) line depends on the definitions that exist inside SubProjectA's CMakeLists. Is there a better way to do this?
If you want to express that include directory subprojectA/include is an interface of the library subprojectA, attach this property to the target with target_include_directories command:
subprojectA/CMakeLists.txt:
project(SubProjectA)
add_library(subprojectA STATIC src/libraryA.cpp)
# PUBLIC adds both:
# 1) include directories for compile library and
# 2) include directories for library's interface
target_include_directories(subprojectA PUBLIC include)
So any executable(or other library) which linked with subprojectA will have this include directory automatically:
subprojectB/CMakeLists.txt:
project(SubProjectB)
add_executable(mainBinary src/mainB.cpp)
target_link_libraries(mainBinary subprojectA)
Of course, for use last command properly you need to process directory with library before one with executable:
CMakeLists.txt:
project(Project)
add_subdirectory(subprojectA)
add_subdirectory(subprojectB)

CMake: Build Multiple Executables in one Project with Static Library

I'm working on a project that consists of 3 server executables and one library for shared code. I want it to be cross-platform, so I'm using CMake (since Xcode is being a pain anyway) to handle the build process. I'm having trouble with setting up the CMakeLists so that I can include the library from a directory at the same level when I'm building the executable.
Here's the directory structure (and the CMake files):
tethealla2.0/
CMakeLists.txt
libtethealla/
CMakeLists.txt
encryption/
utils/
patch_server/
CMakeLists.txt
login_server/
CMakeLists.txt
ship_server/
CMakeLists.txt
My top-level CMake (tethealla2.0/CMakeLists.txt, only includes the sub-project that should compile):
project(tethealla CXX)
cmake_minimum_required(VERSION 2.6)
add_subdirectory(libtethealla)
add_subdirectory(patch_server)
tethealla2.0/libtethealla/CMakeLists.txt, which generates a static library:
project(Libtethealla C)
cmake_minimum_required(VERSION 2.6)
include_directories(encryption)
set(ENC_DR encryption/)
set(ENCRYPTION_SOURCES
${ENC_DR}/psobb-crypt.c
${ENC_DR}/psogc-crypt.c
${ENC_DR}/psobb-crypt.c
${ENC_DR}/encryption.c
)
add_library(tethealla STATIC ${ENCRYPTION_SOURCES})
tethealla2.0/patch_server/CMakeLists.txt thus far:
project(patch_server CXX)
cmake_minimum_required(VERSION 2.6)
add_executable(server main.cc)
target_link_libraries(server tethealla)
So it makes more sense if I build it from the top level since tethealla2.0/CMakeLists.txt will inherit the targets from each of the subdirectories and the one in patch_server will have access to the tethealla library. However what I want is to be able to build from within these subdirectories to generate Xcode projects so that I can work on/recompile them individually. To do so I need to be able to get to the libtethealla/build directory (where CMake outputs) to access the libtethealla.a library from patch_server. Is this possible?
On kind of another note, even in building from the top-level directory my source in patch_server can't include "encryption.h", the header file for the library in encryption. Which seems to be building fine. Any thoughts on that are also greatly appreciated!
My solution is to use add_subdirectory with relative patch to shared_lib directory. I don't think that this is a perfect solution it has its caveats:
Logic very similar to a header guard must be added to library CMakeLists.txt to prevent from defining targets multiple times.
Each CMakeList.txt file must know the relative path to the library, if one want to move library all CMakeLists must be updated.
Let's assume that the directory structure looks like this:
root/
CMakeLists.txt
shared_lib/
CMakeLists.txt
inc/
foo.h
src/
foo.c
exec1/
CMakeLists.txt
main.c
exec2/
CMakeLists.txt
main.c
root/CMakeList.txt
cmake_minimum_required(VERSION 2.6)
add_subdirectory(shared_lib)
add_subdirectory(exec1)
add_subdirectory(exec2)
I have decided that shared_lib/CMakeLists.txt will export a variable named SHARED_DIR_INCLUDE_DIR. This approach helps to decouple things a little bit.
root/exec1/CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
add_subdirectory(./../shared_lib shared_lib)
include_directories(${SHARED_LIB_INCLUDE_DIR})
set(SRCS main.c)
add_executable(exec1 ${SRCS})
target_link_libraries(exec1 shared_lib)
if() in the fourth line solves the issue with target's multiple definition in case the CMakeLists file is added multiple times. The second and the third lines exports the include directory for library in SHARED_LIB_INCLUDE_DIR
root/shared_lib/CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
set(SHARED_LIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc)
set(SHARED_LIB_INCLUDE_DIR ${SHARED_LIB_INCLUDE_DIR} PARENT_SCOPE)
if(TARGET shared_lib)
message("shared_lib is already defined")
else()
include_directories(${SHARED_LIB_INCLUDE_DIR})
set(LIB_SRCS ./src/foo.c)
add_library(shared_lib STATIC ${LIB_SRCS})
endif()