How to avoid adding every source file to CMake in CLion?
Asked Answered
D

5

8

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?

Depriest answered 5/5, 2016 at 10:21 Comment(4)
I prefer the exact opposite (and I'm not alone) -- to explicitly list each and every source that should be used for each component in the project. As the linked answers explain in detail, it avoids a lot of problems once you start dealing with bigger projects.Chinaman
Your question is less of a question, more of an opinion. Given you answered your own question in the very same minute, leaves me puzzled what you actually want. Downvoting.Demurrer
@Demurrer For many people switching from codeblocks and doing competitive programming problems for example(where you have multiple separate programs each in its own source file), its real pain to add every source file name into cmakelists.txt and create configurations that way. I thought that this could be helpful to someone as it was to me, because I've lost all day trying to figure it out.Depriest
@DanMašek Yes, but as you and others have said for "large, multi-developer projects". I agree on that one, but thats not the only use case.Depriest
P
3

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.

Pelaga answered 5/5, 2016 at 16:4 Comment(0)
B
3

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.

Restrictions:

  1. One source file for each sub sub directory
  2. the structure needs to be the same as AAA/BBB where AAA is a parent directory and BBB is it's sub directory.
  3. You can add more AAA in the same level and also more BBB

Hope it helps

Bin answered 30/9, 2017 at 1:14 Comment(0)
T
1

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:

  • use include_directories(./) to support chromium header include rule.
  • use Help -> Change Memory Settings to give 10GB memory to Clion to avoid java gc use 100% cpu.(Of cause you need more than 10GB Pyhsical Memory of your computer.)(I tested 5GB memory is not enough for chromium)
Terminus answered 24/2, 2021 at 3:51 Comment(0)
D
-1

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

Depriest answered 5/5, 2016 at 10:21 Comment(0)
R
-1

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)
Rumormonger answered 5/5, 2016 at 22:58 Comment(1)
file(GLOB SOURCES "./*.c") Only work in one level of directory. It will not include file in directory in ./src/1_test.cTerminus

© 2022 - 2024 — McMap. All rights reserved.