Running different C++ files on the same project

Answered

Hi all, I'm a student and just started using CLion.

I'm trying to run different C++ files on the same project, when the files aren't necessarily related to each other (usually school exercises).

It seems that I can't run the files independently and I couldn't figure out how to do so.

Would love to get some help with that and save some trouble of opening a new project for every time I need to run a simple code. 

Thanks!

P.S

I've searched the web for a while and couldn't find a solution.

Also, I'm using a mac, if that makes any different.

thanks again! 

2
9 comments

Hi! You need to create a new target for each unrelated file. Please read our quick CMake tutorial: https://www.jetbrains.com/help/clion/quick-cmake-tutorial.html#targets.

1

Hi Anna, thanks for replying.

I added 2 cpp files under a single project, and made sure I have each as a target. but still, trying to run them separately results in failure. Do you know why?

1

@Zickmusic, what error do you get? Do you have a main() function in each file?

1

Yes, I do have a main() function for each file.

The error is the same as it was before:

 

Both files are included in the cmakelist, as specified in the tutorial.

Both files have the same code in it.

What do you think?

 

and thank you for your help!

1

So in your CMakeLists.txt you should have smth similar to

add_executable(target_1 main.cpp)
add_executable(target_2 "New File.cpp")

Is it so?

Please do Tools | CMake | Reset Cache and Reload Project. Does it help?

 

1

It was all in one executable, now I split it as so:

 

But still, when I try to run either "multi_one" or "multi_two" it just runs the main. I have tried reseting cahce and reloading the project every time after changing the cmake.

 

Any idea? 

 

Thanks!

1

You should select the appropriate run configuration (target_2 if you want to run Multi_One.cpp; target_3 if you want to run Multi_Two.cpp;) in the Select Run/Debug Configuration drop-down list:

1

Works perfectly now! thank you for your patience and your help :)

1

Thanks ! I had the same problem for a long time, finally, it worked.

0

Please sign in to leave a comment.