If I were to create several C++ executables and main functions ...


I am currently following Stroustrup's A Tour of C++ and experimenting with CLion.

I typically create a main function, required additional functions and call them from the main function. This setup is for each feature of the C++ that I am studying, like, for example reference variables. I get an instant gratification in that I run the program and analyze the output.

However, CLion project structure (with builtin CMake) makes it harder. I can't have one project with multiple main functions (each in its own file) in a project. Creating a separate C++ project for each feature is too onerous. Note that Ij allows creation of multiple Java classes with the main method.

How do people do it, and is there a CLion way of learning C++ piecemeal like this?

1 comment


I can't have one project with multiple main functions (each in its own file) in a project

Actually, you can. If it's a CMake-based project, you just need to create a separate CMake target for each source file with the main function. 

For example, if you have two files `main.cpp` and `main2.cpp`, and you have a `main()` function in each of these files, the simplest CMakeLists.txt for such a project should look similar to the following:

cmake_minimum_required(VERSION 3.15)
add_executable(my_project main.cpp)
add_executable(my_project_2 main2.cpp)

After that, you can choose which target to run (`my_project` or `my_project_2`) via "Run configuration" pop-up (left to the "Run" button).

Please read our quick CMake tutorial for more details: https://www.jetbrains.com/help/clion/quick-cmake-tutorial.html.


Please sign in to leave a comment.