I am currently starting a new project using C++ and CMake,
in my last project I organized the code the following:
src
|--foo
| |--bar.cpp
| |--bar.hpp
| |--CMakeLists.txt
|--baz
| |--anotherFoo
| | |--Bahama.cpp
| | |--Bahama.hpp
| | |--CMakeLists.txt
| |--baz.cpp
| |--baz.hpp
| |--CMakeLists.txt
|--CMakeLists.txt
so every subdirectory got its own CMakeLists.txt, which was included as library from other CMakeList files.
Now I want to separate the .hpp and the .cpp files.
Something like this:
src
|--foo
| |--bar.cpp
| |--CMakeLists.txt
|--baz
| |--anotherFoo
| | |--Bahama.cpp
| | |--CMakeLists.txt
| |--baz.cpp
| |--CMakeLists.txt
|--CMakeLists.txt
include
|--foo
| |--bar.hpp
| |--CMakeLists.txt
|--baz
| |--anotherFoo
| | |--Bahama.hpp
| | |--CMakeLists.txt
| |--baz.hpp
| |--CMakeLists.txt
|--CMakeLists.txt
What I am supposed to do with the CmakeLists files?
Do I also need a CMakeLists file in every subdirectory, like I did in my example above? Or is there another way to do this?
What do I write in this CMake files
In the future I also want to add documentation with doxygen, how can I enable Doxygen support via Cmake in a structure like this?
should also every subdirectory get its own?
Related
I'm trying to compile a c++ program with the SDL package using cmake but I have issues finding the SDL.h file when I compile it with the make command.
Currently the CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)
project(TestProject VERsion 1.0 DESCRIPTION "Game project" LANGUAGES CXX)
add_compile_options(-Wall -Wextra -pedantic -Werror)
add_library(Gamelib ${PROJECT_SOURCE_DIR}/Source/Game/game.cc)
target_include_directories(Gamelib PUBLIC ${PROJECT_SOURCE_DIR}/Source/include
${PROJECT_SOURCE_DIR}/SDL/include)
add_executable(main ${PROJECT_SOURCE_DIR}/Source/main.cc)
target_link_libraries(main Gamelib)
The project structure looks like this:
|--project
|
+-- CMakeLists.txt
|
+-- Source
| |
| +--Game
| | |
| | +-- game.cc
| |
| +--include
| | |
| | +--game.h
| |
| +-- main.cc
|
|
+--SDL
|
+--include
I also tried with using the find_package command but then I ended up with a linker error instead. Does anybody have an idea of how I can solve this?
My source project directory is something like this,
|── CMakeLists.txt
|
├── src
| |
| |
| |
| └── a
| | |
| | |__ a.cpp
| |
| |
| |
| └── b
| |
| |__ b.cpp
|
|___test
|
|__test.cpp
I need to make a executable out of source a.cpp and b.cpp,
file(GLOB_RECURSE SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*/ *.cpp)
I expect this to take a.cpp and b.cpp as the only source files, but when I print
message("All source file - ${SRC_FILES}")
It picks up test.cpp as well, which I don't know why it's happening. My understanding here is ${CMAKE_CURRENT_SOURCE_DIR}/src/*/ *.cpp, in this statement
due to wildcard * it would go through each subdirectory of src folder, and then with *.cpp it would append all the cpp files inside each of these subdirectory.
I'm trying to write Makefile script which will compile my project in VS Code which contains sub directories. My project structure looks like:
src
| - subdirectory1
| | - file1.cpp
| | - file2.cpp
| - subdirectory2
| | - sub3
| | | - file4.cpp
The problem is solved, it is due to other dependency of my project, it has nothing wrong with cmake. Thanks for everyone trying to help in the question.
Here is my project tree Tree 1:
project
| + src
| | + other_src
| | | - abc.cpp
| | | - abc.h
| | - main.cpp
| - CMakeLists.txt
3rd_party
| + pcap
| | - pcap.h
| - pcap.h
Inside my CMakeLists.txt, I do:
include_directories("/path/to/3rd_party")
Inside my abc.cpp, I have:
#include "pcap.h"
Then I do cmake and make. I get error pcap.h: No such file or directory
I then change my project tree Tree 2:
project
| + src
| | - abc.cpp
| | - abc.h
| | - main.cpp
| - CMakeLists.txt
3rd_party
| + pcap
| | - pcap.h
| - pcap.h
Without modifying my CMakeLists.txt, I do cmake and make again. The header file is found.
How can I build this project while my source code is placed in different folders?
I am trying to use biicode to manager dependencies for my project so that I can automate things like boost or sqlite and use travis-ci
From what I understand bii is expecting your source files to be at the root folder of your block like mentionned in their tutorials:
|-- my_project
| +-- bii
| +-- bin
| +-- blocks
| | +-- myuser
| | | +-- my_block
| | | | |-- main.cpp
| | | | |-- biicode.conf
But in my case, the source file is like this
|-- my_project
| +-- bii
| +-- bin
| +-- blocks
| | +-- myuser
| | | +-- my_block
| | | | |-- src
| | | | | +--folderA
| | | | | | +--core
| | | | | | |-- various .cpp
| | | | | | +--impl
| | | | | | |-- various .cpp
| | | | | |-- various .cpp
| | | | |-- biicode.conf
and running configuration keep missing these folders
bii cpp:configure
I have read the doc about biicode.conf but it doesn't mention an alternative path for source files.
So my question is, do I really need to put everything as a flat directory where every source file is in the same folder to use biicode ?
EDIT: I forgot to mention that I am trying to build a library (to be used in another bii project), not an executable
You can sort your block like you want inside your blocks/my_user/my_block. For example, take a look to this block:
fenix/flatbuffers block
Here there is a structure without any pattern.
biicode.conf, among other things, helps you to tell biicode where your header files (but not source ones) are, thanks to [paths]section, because the source ones biicode detects them automatically, else you could customize your dependencies with [dependencies]section.
Making an example with this layout:
|-- my_project
| +-- bii
| +-- bin
| +-- blocks
| | +-- myuser
| | | +-- my_block
| | | | |-- src
| | | | | +--folderA
| | | | | | +--core
| | | | | | |-- core.h
| | | | | | |-- core.cpp
| | | | | | +--impl
| | | | | | |-- impl_ext.h
| | | | | | |-- impl.h
| | | | | | +--src
| | | | | | |-- impl.cpp
| | | | | | |-- impl_ext.cpp
| | | | | |-- CMakeLists.txt
| | | | |-- biicode.conf
| | | | |-- CMakeLists.txt
Then, your .cpp files might have relative includes like:
#include "core/core.h"
#include "impl/impl.h"
#include "impl/impl_ext.h"
Supposing your CMakeLists.txt files are right, you'd only have to tell biicode where it must search this "relative" headers, so write into the biicode.conf:
[paths]
folderA/core
folderA/impl
I hope it resolves your doubts! ;)