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?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
Dusan J.
  • 303
  • 5
  • 19
  • 1
    I prefer the exact opposite (and [I'm not alone](http://stackoverflow.com/a/1060061/3962537)) -- 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. – Dan Mašek May 05 '16 at 10:42
  • 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. – usr1234567 May 05 '16 at 10:51
  • 1
    @usr1234567 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. – Dusan J. May 07 '16 at 08:00
  • @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. – Dusan J. May 07 '16 at 08:03

5 Answers5

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.

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
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

abd3lraouf
  • 1,438
  • 1
  • 18
  • 24
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)
bronze man
  • 1,470
  • 2
  • 15
  • 28
-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

Dusan J.
  • 303
  • 5
  • 19
-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)
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • file(GLOB SOURCES "./*.c") Only work in one level of directory. It will not include file in directory in ./src/1_test.c – bronze man Feb 24 '21 at 03:43