How to Create, Compile, And Run a single file in CLion How to Create, Compile, And Run a single file in CLion c c

How to Create, Compile, And Run a single file in CLion


You may modify the CMakeLists.txt

Here an example :

cmake_minimum_required(VERSION 3.3)project(test_build)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")set(BUILD_1 main)set(SOURCE_FILES_1 main.cc) //where main.cc is your first main/programadd_executable(${BUILD_1} ${SOURCE_FILES_1})set(BUILD_2 main_2)set(SOURCE_FILES_2 main_2.cc) //where main_2.cc is your second main/programadd_executable(${BUILD_2} ${SOURCE_FILES_2})

Or use a test (garbage version) :
add_executable(foo bar.cc)

After that you can choose the build you want in CLion


I just had the same question and stumbled upon this thread and then found my solution in this plugin. What this plugin does is basically what user Waxo suggested automatically: adds a single line in CMakeLists.txt for each executable file for you. You just have to right click in editor and select it. I found it pretty useful and use it mostly for algorithms competitions.Hope this helps: https://plugins.jetbrains.com/plugin/8352-c-c--single-file-execution

Cheers!


Inside every CLion project there is a file CMakeLists.txt.
To run a single file you will have to write a single command inside this file and that is:

add_executable(file_name_without_extension_cpp  file_name_with_extension_cpp)

For example: add_executable(CoinChange CoinChange.cpp)

Then click reload changes and then go to run option and then select the file you want to run then hit the run button. Your single file will be run.

How many single files will be in your CLion project you will have to perform this same action for running every single file.