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.
请先登录再写评论。
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.
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.
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
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.
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.
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
Hi
- make a cmake project as a usual
- create a shell file (linux, mac) or a batch file (windows) to compile the source file and place them in root of current folder..
- open default source file (main.c)
- remove cmake file.
- remove debug.. folder.
- write your code and compile it by your shell file in Clion terminal
Hinata
Hinata_template.c
Hi all!
CLion now has an option to run/debug a single file - https://www.jetbrains.com/help/clion/run-single-file.html
This sounds very primitive, I have 5120 files and only one month trial.... typing all the file names should be automated, but a decent program like Clion should be able to do this. For now I stay in Eclipse, that's working great.. I'm not going to pay monthly for adding a lot of problems and extra work... but Clion looks compiling slightly faster. What I also don't like, it looks a look like my Android Studio Kotlin, which is very unstable.
Bart ICQ you can run/debug a file using the gutter menu - https://www.jetbrains.com/help/clion/run-single-file.html#rundebug-file-gutter. No need to type a file name in this case.
It's really sad that if I have multiple files with main I need to edit manually the CMakeLists. I prefer using another IDE for C/C++ programming.
Flavio Prado, as mentioned in my previous comments, you can run/debug a single file in CLion without having or modifying CMakeLists.txt - https://www.jetbrains.com/help/clion/run-single-file.html.
If you would like to keep your project as a CMake project, and just don't want to edit the CMakeLists manually.txt, you can use the “Add new target” option when adding a new source file in CLion:
It will modify the CMakeLists.txt for you.