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?
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
In short you need a line like following in your CMakeLists.txt:
file(GLOB_RECURSE SOURCES *.cc *.cpp)
Here is my CMakeLists.txt file to view and edit the Chromium c++ project.
cmake_minimum_required(VERSION 3.17)
project(chromium)
set(CMAKE_CXX_STANDARD 11)
file(GLOB_RECURSE SOURCES *.cc)
include_directories(./)
add_executable(chromium ${SOURCES})
You can use file(GLOB_RECURSE SOURCES *.cc) to include all files in all level of directory that match some patterns.
Ps:
I wrote a template cmake file that I use which can do this for you. It will automatically make source files as separate executables.
Cmake template can be found here: template file
You just need to paste it into CMakeLists.txt and refresh cmake. You can then just choose from the dropdown menu which file you want to run.
Hint: Add keyboard shortcut for Cmake refresh in clion settings
Here's a complete example that does it.
cmake_minimum_required(VERSION 2.8.12)
project(openshell)
set(CMAKE_VERBOSE_MAKEFILE on)
file(GLOB SOURCES "./*.c")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -L/usr/local/include/ -L/usr/include -std=c11 -pedantic -O3 -g -Wall -pedantic -ledit -lncurses -lcurses -ltermcap")
include_directories(/usr/local/include/ /usr/include)
link_directories(/usr/lib)
link_directories(/usr/local/ib)
add_executable(openshell ${SOURCES})
target_link_libraries(openshell edit readline)
© 2022 - 2024 — McMap. All rights reserved.