Search code examples
ccompilationideclion

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


I am currently doing some Project Euler challenges in C using the JetBrains CLion IDE. When I completed these in Python and Java (in PyCharm and IntelliJ, respectively), I was always able to create a single project named "ProjectEuler" and add any number of source files that I could execute independently. However, it seems the same isn't possible in CLion. Each of the source files I use to solve a problem contains a main() function, and CMake is unhappy about that everytime I attempt to compile ("multiple definition of 'main'").

Is there a way to configure CLion to only compile and execute a single source file in a project at a time without having to rewrite my CMakeLists.txt or create a new project for every problem everytime?

I'm aware that C is compiled and not interpreted like Java or Python, but I could conceivably just compile every single source file manually. Is there a way to configure CLion to do the same? If so, how?


Solution

  • 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.