Multiple Main Source files in CLion

Hello All,

        I am looking for a solution in CLion IDE. While practicing some programs I noticed in CLion that I could only use one main c file for executing project. I know that project can have only on main function but in Eclipse IDE for C/C++ I have a comfortable facility which allows me to have multiple main source files (i.e. with main function) and I can run each particular main file by right clicking on it and choose 'Run as'. But in Jet Brains unlike IDEA (Where we can choose run configurations by configuring main class in run configurations) Clion doesn't have this facility so far as I know. Please correct me if I'm wrong. and let me know how I can use multiple source files (which has main function) in same project and will be able execute desired source file when ever I want in the same project.

Thanks,
Sudhir.

6 comments
Comment actions Permalink

Hi Sudhir.

CLion now relies on CMake as a project model. It takes all the information about the project from CMake build system.
You can add several executable targets via CMake using add_executable() command.

2
Comment actions Permalink

Hello Anna,

          I was able to figure out the cmake setting you've mentioned and was able to use it as expected. What I'm expecting from JetBrains is that is there is a way to use build configuration or run configurations to select required executable using GUI. on eclipse what I'm doing is that I simply right click on my desired c file and select run as C/C++ application and it'll execute.

Thanks,
Sudhir.

0
Comment actions Permalink

Sudhir,

It's already implemented in CLion.
You can choose your target at Configurations dropdown menu.
Please see the screenshot.



Attachment(s):
Screen Shot 2015-07-29 at 11.13.31.png
0
Comment actions Permalink

Hello Anna,

        Thank You for Information. What I'm looking for is to choose desired main c file to start execution please see below intelliJ IDEA screenshot so you'll understand what I'm trying to learn.

    2015-08-01_8-39-18.png

0
Comment actions Permalink

Sudhir,

in C/C++ it's not generally possible to select any arbirary file (event with main() function) and launch it, since it first needs to be compiled first.
To compile a file, the build system needs various information like compiler options, header hearch paths, linked options etc.
All these parameters are usually configured per-target; that is, a given file compiled and launched in context of a 'target'.

In CMake executable targets are speified with something like 'add_executable(TargetName main.cpp)'.

In the other words, there is no explicit functionality in the IDE to launch a specific standalone file. We may improve this in future.
Here is a related request for you to follow and comment: CPP-192

Hope this will make things more clear for you.

1
Comment actions Permalink

Hi Sudhir Boddu,

 

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

 

Thanks 

Loveneesh

1

Please sign in to leave a comment.