I'm a little new here, this is my first post. I have a c++ project in CLion that is structured like so:
project-root/
├─ cmake-build-debug/
├─ controller/
├─ model/
│ ├─ foo/
│ │ ├─ a.h *
│ │ ├─ a.cpp *
│ ├─ bar/
│ │ ├─ b.h *
│ │ ├─ b.cpp *
├─ view/
│ ├─ c.h *
│ ├─ c.cpp *
├─ new_file
├─CMakeLists.txt
├─main.cpp *
I've starred the actual cpp and h files just for visual purposes.
In a.h, suppose I'd like to include b.h. The way I'd like to do this in a.h is by doing:
#include "project-root/model/bar/b.h"
Similarly, I'd like to use this pattern for all includes. For example, if I wanted to include a.h in c.h, in c.h I'd do:
#include "project-root/model/foo/a.h"
And, just one more example, even if I was in main.cpp and I'd like to include c.h, I'd do:
#include "project-root/view/c.h"
I tried doing the following in my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.22)
project(project-root)
set(CMAKE_CXX_STANDARD 17) // this ".." seems incorrect
include_directories(..)
add_executable(project-root main.cpp <the rest of all the .h and .cpp files>)
And actually, this works! However, I think it seems a little weird to have include_directories(..), since there's a relative path above the project root. That doesn't seem correct to me.
I originally thought include_directory(.) would work, but unfortunately, no dice. When I try to do #include project-root/model/bar/b.h in a.h, I get an error saying project-root/model/bar/b.h not found
I would also advise against .. since you are depending on name of a folder that you might not necessarily control. Well, it depends on how you plan to distribute the code, for example if you use git, then project-root folder itself should be inside the repository, not the other way around.
The include paths have little to do with CMake, it will just pass them (their absolute versions) to the compiler.
In C++, you have two "types" of includes - #include "foo.h" or #include <foo.h>. Both are implementation-specific, i.e. the compiler dictates how they are interpreted. In practice, <> searches on include paths, "" searches relative paths first, then behaves like <>. One convention is to use <> for public headers in the include path and "" for relative includes or private headers.
Libraries might want to distinguish between public and private headers. Public ones are meant for the user of the library, private ones are internal. But this is not universally adhered to, sometimes the are internal directories...
Personally, I like to use the following structure for my projects:
. // root of a git repository
├── include/
│ └── project-name/
│ ├── model/
│ │ └── foo/
│ │ └── a.h
│ └── view/
│ └── c.h
├── src/
│ ├── model/
│ │ ├── foo/
│ │ │ └── a.cpp
│ │ └── bar/
│ │ ├── b.h
│ │ └── b.cpp
│ ├── view/
│ │ └── c.cpp
│ └── main.cpp
└── CMakeLists.txt
with CMakeLists.txt containing something like:
//Global
cmake_minimum_required(VERSION 3.22)
project(project-name)
//Repository-wide options
set(CMAKE_CXX_STANDARD 17)
//Local
add_executable(project-root)
target_include_directories(project-name PUBLIC include)
target_include_directories(project-name PRIVATE src)
// Project-specific options
// Assumes gcc/clang toolchain as an example.
target_compile_options(project-name -Wall -Wextra -Werror -pedantic)
target_sources(project-name
PUBLIC
include/project-name/model/foo/a.h
include/project-name/view/c.h
PRIVATE
src/model/foo/b.h
src/model/foo/b.cpp
src/view/c.cpp
src/main.cpp
)
Modern way is to use target_ variants instead of global ones.
If the repository contained more than one project, have one //Global CMakeLists.txt with the three lines above - with global options - and bunch of add_subdirectory calls which each contain //Local CMakeLists.txt for the submodule - executable/library.
a.h,c.h are marked as public, one should always include them with #include <project-name/model/foo/a.h> from anywhere.
I marked b.h as private just for demonstration purposes, there are two ways how to include it. b.cpp could use #include "b.h" or #include "model/foo/b.h" (no <> although it would work). I try to use the latter always apart from b.cpp-b.h declaration-definition pairs. I never use ../ or subdir/b.h since it makes a headache to move anything. This way, each header is included exactly the same way from anywhere - easily searchable and replaceable, b.{cpp,h} move together so the relative path is OK there.
For an executable, I usually just go with all public headers.
If this were a library, it can easily be converted to a e.g. a debian package since one can cleanly install the whole include directory to /usr/include without fear of clashes or hassle to extract the headers from src since src contains only private ones.
You can use cmake builtin variables to construct absolute paths, e.g. PROJECT_SOURCE_DIR.
include_directories(${PROJECT_SOURCE_DIR})
More fine-grained control over includes is possible using target_include_directories.
Related
I have a project with structure like this:
project/
├─ src/
│ ├─ main.cpp
│ ├─ CMakeLists.txt
├─ lib/
│ ├─ libapi.so
├─ CMakeLists.txt
├─ dep/
│ ├─ api/
│ │ ├─ api_types.h
In the main CMakeLists I have:
add_subdirectory(dep)
add_executable("my_${PROJECT_NAME}")
add_subdirectory(src)
And in CMakeLists inside src folder:
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_library(api SHARED IMPORTED)
set_property(TARGET api PROPERTY IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/lib/libapi.so")
target_sources("my_${PROJECT_NAME}" PRIVATE main.cpp)
In main.cpp I do #include <api_types.h> but I get this error during compile:
api_types.h: No such file or directory
How can I fix it?
First - add_subdirectory in fact just looks for CMakeLists.txt in
the specified directory to apply the sub-set of the rules to the
parent project. If the specified folder doesn't contain any
CMakeLists.txt this command does nothing (i.e. the command
add_subdirectory(dep)).
Second - target_include_directories expects a target object to link against (not the project name). Assuming ${PROJECT_NAME} is not a name of any of your targets (and the code given in the question reveals only two targets my_${PROJECT_NAME} and api) the command target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) does nothing sensible.
Last - you don't specify path to your actual headers anywhere. CMAKE_CURRENT_SOURCE_DIR refers to the current folder CMakeLists.txt is located in. You should specify path to the folder the headers are located in.
Putting all the said info together, the desired content for the top-level CMakeLists.txt should be something like that:
cmake_minimum_required(VERSION 3.20)
set(PROJECT_NAME Hello)
project(${PROJECT_NAME})
add_executable(my_${PROJECT_NAME})
add_subdirectory(src)
And for src's file it's apparently something like that:
target_include_directories(my_${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/dep/api)
target_sources(my_${PROJECT_NAME} PRIVATE main.cpp)
This solution, however, doesn't resolve another mistake in your code - the CMake files you provided don't link the library itself to the executable target. However this is not the question asked, and with respect to the members of the community, I suggest referring to answers to another related question here.
target_include_directories requires your executable as target, in this case my_${PROJECT_NAME}.
You can try
target_include_directories(my_${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
As alternative I would recommend following structure
project/
├─ src/
│ ├─ main.cpp
├─ lib/
│ ├─ libapi.so
├─ CMakeLists.txt
├─ include/
│ ├─ api/
│ │ ├─ api_types.h
with following CMakeList.txt
add_executable(my_${PROJECT_NAME} src/main.cpp)
target_include_directories(my_${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
link_directories(${CMAKE_SOURCE_DIR}/lib)
target_link_libraries(my_${PROJECT_NAME} libapi)
I have a repository with 2 Projects, which both link a single (self-made) library which is also contained in the repository. I also include some dependencies, which both projects use.
The project structure is as follows:
repo/
├─ dependencies/
│ ├─ audeo/
│ ├─ argparse/
│ ├─ json
├─ my-library/
│ ├─ CMakeLists.txt
│ ├─ ...
├─ projet2/
│ ├─ CMakeLists.txt
│ ├─ ...
├─ projet1/
│ ├─ CMakeLists.txt
│ ├─ ...
├─ CMakeLists.txt
I don't know how to utilize this. I already tried to set up a project, but I ran into numerous problems, which I don't want to further go into detail about.
My question is whether there are examples of this kind of project structure or if you have good practice advice, or if this problem has a common name that I can research.
Edit:
I will post my CMake files and the link to the current state of the source code (Disclaimer: the second project is not already included).
Content of repo/CMakeLists.txt:
cmake_minimum_required(VERSION 3.11)
project(root)
set(CMAKE_CXX_STANDARD 20)
# dependencies
add_subdirectory(dependencies/dep1)
add_subdirectory(dependencies/dep2)
add_subdirectory(dependencies/dep3)
# sub-projects
add_subdirectory(my-library)
add_subdirectory(project1)
add_subdirectory(project2)
Content of my-library/CMakeLists.txt:
cmake_minimum_required(VERSION 3.11)
project(my-library)
set(CMAKE_CXX_STANDARD 20)
add_library(my-library STATIC
src/World.cpp include/World.h
include/Player.h
include/Position.h
include/Audio/Effect.h
include/Audio/Music.h
include/Audio/Audio.h)
target_link_libraries(my-library PRIVATE
dep1::dep1
dep2::dep2)
Content of projet1/CMakeLists.txt:
cmake_minimum_required(VERSION 3.11)
project(project1)
set(CMAKE_CXX_STANDARD 20)
add_executable(project1 main.cpp)
target_link_libraries(project1 PRIVATE
dep1::dep1
dep3::dep3
)
Content of projet2/CMakeLists.txt:
cmake_minimum_required(VERSION 3.11)
project(project2)
set(CMAKE_CXX_STANDARD 20)
add_executable(project2 main.cpp)
target_link_libraries(project2 PRIVATE
dep2::dep2
)
Since my source code is open source anyway I will post the link to my repository. But please note that the names of the projects may differ as well as the structure.
Problem
My Problem is, that I cannot include one dependency (warning! this is a translation):
fatal error: dep1/dep1.hpp: file or directory not found
11 | #include <dep1/dep1.hpp>
And I want to be able to include my library in Angle-bracket form.
I have a project where I need to run one application on multiple boards. To do this I have two exectuables that share a main.c, but are compiled with a different board.c. The directory tree is as follows:
CMakeLists.txt
src/
├─ board/
│ ├─ board_a/
│ │ ├─ board.c
| | ├─ CMakeLists.txt
│ ├─ board_b/
│ │ ├─ board.c
| | ├─ CMakeLists.txt
├─ app/
│ ├─ main.c
├─ lib/
│ ├─ somelib/
│ │ ├─ somelib.h
│ │ ├─ somelib.c
In an attempt to make the root CMakeLists.txt smaller and more maintainable, I've created a CMakeLists.txt for both boards, and would like to compile each board as an object library for the main executable in the root CMakeLists.txt to add_subdirectory on and link in.
My problem is that board.c (as well as main.c) depends on somelib. How can I add this dependency to the subdirectory CMakeLists.txt? Is there a way I can do that without hard-coding a path to somelib? My feeling is that I should create a CMakeLists.txt in somelib, and I feel this would be easy if I were handling the library linking from the root CMakeLists.txt, but my confusion is that board is adjacent to lib. I'm relatively new to CMake and am not sure how to best structure these dependencies.
My problem is that board.c (as well as main.c) depends on somelib. How can I add this dependency to the subdirectory CMakeLists.txt? Is there a way I can do that without hard-coding a path to somelib? My feeling is that I should create a CMakeLists.txt in somelib, and I feel this would be easy if I were handling the library linking from the root CMakeLists.txt, but my confusion is that board is adjacent to lib. I'm relatively new to CMake and am not sure how to best structure these dependencies.
This is very straightforward start by linking each board_<X> to somelib's target.
# in each board_<X>/CMakeLists.txt
add_library(board_<X> OBJECT ...)
target_link_libraries(board_<X> PUBLIC proj::somelib)
then create the target for somelib:
# src/lib/somelib/CMakeLists.txt
add_library(somelib somelib.c)
add_library(proj::somelib ALIAS somelib)
target_include_directories(
somelib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>")
As long as the top-level CMakeLists.txt includes these subdirectories via add_subdirectory (multiple levels is fine), then other targets can #include <somelib.h> and CMake will handle linking.
I am new to CMake. I'm trying to create a list of C++ source files, and build an executable using those source files. To that avoid passing the list one level at a time, I am using an environment variable. However, not all listed sources are added to the env var, only the first one is.
Here's what I have:
Directory structure:
<root>
├─ build
│ ├─ <snip>
├─ src
│ ├─ something
│ │ ├─ CMakeLists.txt
│ │ ├─ a.cpp
│ │ ├─ a.h
│ │ ├─ b.cpp
│ │ └─ b.h
│ ├─ CMakeLists.txt
│ └─ main.cpp
├─ CMakeLists.txt
root/CMakeLists.txt:
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(samplecpp VERSION "0.1.0" LANGUAGES CXX)
# ---- SOURCES ----
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
set(ENV{MY_SOURCES} "")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
message(STATUS "MY_SOURCES = " $ENV{MY_SOURCES})
# ---- EXECUTABLE ----
add_executable(samplecpp ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp $ENV{MY_SOURCES})
root/src/CMakeLists.txt:
add_subdirectory(something)
root/src/something/CMakeLists.txt:
set(
ENV{MY_SOURCES}
$ENV{MY_SOURCES}
${CMAKE_CURRENT_SOURCE_DIR}/a.cpp
${CMAKE_CURRENT_SOURCE_DIR}/b.cpp
)
Running cmake should print the message MY_SOURCES = root/src/something/a.cpproot/src/something/b.cpp, but I only get MY_SOURCES = root/src/something/a.cpp. Because of this, cmake --build fails with this error:
<root>/src/main.cpp:11: undefined reference to `B::do_b()'
What am I doing wrong?
Just in case, here's the sample on github
It is not stated in the documentation, but setting an environment variable accepts only a single value:
set(ENV{name} value)
Before 3.14 version CMake silently ignores other values.
Since 3.14 version CMake emits a warning when more than one values are passed:
Only the first value argument is used when setting an environment variable.
Source: Source/cmSetCommand.cxx
It is still possible to assign a list to the environment variable by using its semicolon-separated string representation:
# Don't work: assigns only 'a' and emits warning in CMake 4.14+
set(ENV{MY_SOURCES} a b c)
# Works: assign list of 'a', 'b', 'c' elements
set(ENV{MY_SOURCES} "a;b;c")
# The same but using intermediate CMake variable
set(MY_SOURCES_TMP a b c)
set(ENV{MY_SOURCES} "${MY_SOURCES_TMP}")
I'm trying to learn CMake, but I can't get to grips with the tutorials that are available.
Let's say my project has the structure below, and I want to make my_lib available through its CMakeLists file and use it in my main.cpp, what would my CMakeLists files look like?
├── CMakeLists.txt
├── externals
│ └── my_lib
│ └── src
│ ├── CMakeLists.txt
│ ├── MyClass.h
│ └── MyClass.cpp
└── main.cpp
Should I use include_directories or add_subdirectory?
To match the directory structure you indicated, your CMakeLists.txt files could look like this:
/CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Main)
add_subdirectory(externals/my_lib/src)
add_executable(my_main main.cpp)
target_link_libraries(my_main my_lib)
/externals/my_lib/src/CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(MyLib)
add_library(my_lib MyClass.cpp MyClass.h)
target_include_directories(my_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Using PUBLIC in the target_include_directories call means that not only does your library have this as an "include path", but so does any target linking to it.
The downside with this setup however is that you're also going to expose any internal headers of my_lib to consuming targets too. Say you also have an "Internal.h" as part of my_lib which you don't want to expose. You can do this by moving the intentionally-public headers into a separate folder, e.g. "include":
├── CMakeLists.txt
├── externals
│ └── my_lib
│ ├── CMakeLists.txt
│ ├── include
│ │ └── MyClass.h
│ └── src
│ ├── Internal.h
│ └── MyClass.cpp
└── main.cpp
Your top-level CMakeLists.txt wouldn't change, but /externals/my_lib/CMakeLists.txt (which has moved up a level) now reads:
cmake_minimum_required(VERSION 3.0)
project(MyLib)
add_library(my_lib src/MyClass.cpp src/Internal.h include/MyClass.h)
target_include_directories(my_lib
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src
)
Now, since your library's "src" folder is PRIVATE to my_lib, main.cpp won't be able to #include "Internal.h".