Is it possible to configure CLion to compile source files in a project independently?

You could define multiple executables in the CMakeLists.txt for each problem.

instead of

add_executable(projecteuler ${SOURCE_FILES})

you could define

add_executable(problem1 problem1.c)
add_executable(problem2 problem2.c)

Then you get for each executable (problem1, problem2 etc.) a run configuration, which you can run independently. In this case you won't have to rewrite every time, instead you just add the new source file to a new executable.


Even I was facing the same problem, it's a tiring job to edit CMake file and add executable every time. So I found a solution to this,

There is a plugin which does it seamlessly for you.

Just add this plugin to your CLion and whichever file you want to make is executable right click and add it as Executable,

It will automatically edit your CMake file.

Link:

https://plugins.jetbrains.com/plugin/8352-c-c--single-file-execution


You can use

cmake_minimum_required(VERSION 2.8.4)

add_subdirectory(src/prj1)
add_subdirectory(src/prj2)

then in each directory create an other CMakeLists.txt like this one :

cmake_minimum_required(VERSION 2.8.4)
project(prj1)

set(EXEC_NAME prj1)

set(SOURCE_FILES
    main_prj1.cpp
    x.cpp
    y.cpp
)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(EXECUTABLE_OUTPUT_PATH ../../dist/${CMAKE_BUILD_TYPE})

add_executable(${EXEC_NAME} ${SOURCE_FILES})

You can use file(GLOB SOURCE_FILES *.cpp) if you want to automatically add files in your compilation. But keep in mind that this "trick" is strongly not encouraged.

This will also automatically add build configurations to CLion.