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

6

44

I am working on some c++ stuff and I hate having to create a whole new project just to run a few things on a file.

I also don't like how when you create a project a file is already called main.cpp.

I just want to make a single file with a few functions or classes. It's not important enough to create a whole project.

I want to create a file and call it what i want. Just create a file what I call, then compile and run.

I don't want to deal with the whole CMake thing, just compile ONE file.

No project related. Thank you.

I know you can do this on visual studio, but i am working on a mac OS X using Clion.

Whitehouse answered 24/8, 2015 at 4:54 Comment(2)
Call me oldschool, but I just do it from the comand line: g++ -std=c++11 -o target target.cpp where "target" is the name of the program I'm building.Spam
You may modify the CMakeLists.txt. Here's an implementation. https://mcmap.net/q/389886/-is-it-possible-to-configure-clion-to-compile-source-files-in-a-project-independentlyThrong
R
28

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/program
add_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/program
add_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

Rill answered 24/8, 2015 at 8:2 Comment(0)
S
20

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!

Spradling answered 23/3, 2017 at 14:37 Comment(0)
P
6

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.

Pennyroyal answered 16/11, 2019 at 17:49 Comment(0)
A
2
  1. install plugin inside CLion "C/C+​+​ Single File Execution"
  2. go to that file that you want to execute and right click then select last option i.e. add single executable single c/cpp file
  3. go to run option or press Alt + shift + f10 and select file that you want to run
Annmarie answered 25/11, 2020 at 14:16 Comment(0)
I
0

For a portable solution across IDE's, I call a scratch() function at the start of my main() and put exit(0); at the end of the scratch function.

Inside scratch(), you can call something in a different file if you want. I usually just test snippets in there.

Ihs answered 28/4, 2019 at 19:9 Comment(0)
P
0

CLion is based on CMake. So if you want to not use CMake, you can use other editors like Sublime Text.

But a simple CMake script could solve the problem.

The following CMake script automatically adds cpp files in the current directory and its subdirectories to executables (filename as the target name).

cmake_minimum_required(VERSION 3.15)
project(MyApp)

set(CMAKE_CXX_STANDARD 17)

file(GLOB APP_SOURCES *.cpp */*.cpp)
foreach (testsourcefile ${APP_SOURCES})
    get_filename_component(testname ${testsourcefile} NAME_WE)
    message("${testname}")
    add_executable(${testname} ${testsourcefile})
endforeach (testsourcefile ${APP_SOURCES})
Pareto answered 10/9, 2020 at 4:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.