Most people that switched from some other IDE that had an option to build single file are a bit confused by CLion which doesn't happen to have an option.
So how can you avoid adding every name of every source file into CMake?
What you are looking for is something like file(GLOB_RECURSE *.cpp)
. You can customize this command to filter out directories, if you need to.
I had the same problem with c++ and CMake using CLion. And this how I managed to solve it:
Here is my project structure :
├── CMakeLists.txt
├── README.md
└── src
└── ch01
├── ex01
│ └── main.cpp
└── ex02
└── main.cpp
Here are the contents of CMakeLists.txt
:
cmake_minimum_required(VERSION 3.5)
project(CPPTutorial)
set(CMAKE_CXX_STANDARD 14)
set(sourceDir "${PROJECT_SOURCE_DIR}/src")
file(GLOB_RECURSE sourceFiles "${sourceDir}/*.cpp")
FOREACH (sourceFile ${sourceFiles})
MESSAGE(STATUS "Process file: ${sourceFile}")
get_filename_component(dir1 ${sourceFile} PATH)
get_filename_component(dir1 ${dir1} NAME)
get_filename_component(dir2 "${sourceFile}/../../" ABSOLUTE)
get_filename_component(dir2 ${dir2} NAME)
MESSAGE(STATUS "New target: ${dir2}_${dir1}")
add_executable("${dir2}_${dir1}" ${sourceFile})
endforeach (sourceFile)
It creates a new target for each source in the directory .. actually it needs more work but it does the job for me.
AAA/BBB
where AAA
is a parent directory and BBB
is it's sub directory.AAA
in the same level and also more BBB
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With