Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get eclipse to run currently open source file?

Tags:

c++

eclipse

I feel embarrassed asking this question, but I've spent the last half an hour trying to figure out how to run a specific source file in eclipse with no luck.

I created a c++ project, then created a source folder and a c++ source file. That file works fine but when I added a second file into the folder it does not seem to run. I know this because 'hello world' is being outputted which was in the first source file. The strange thing is if I have an error in my second file then I get errors but after I correct them I still only get the output of the first file.

Is it possible to run the source file thats currently open in eclipse(kind of like how it does with Java)? My goal is to create a new source file for every exercise of a c++ book I work though and have them run independently of each other without having to create new projects for each exercise.

like image 369
Lostsoul Avatar asked May 05 '12 21:05

Lostsoul


1 Answers

I just finished creating an online C++ course for lynda.com (info here: http://cpp.bw.org/) using Eclipse, because it's cross-platform and people can follow along with the exercises on many different platforms. Here's how I solved this problem:

First understand how Eclipse CDT works -- all the files in a project get compiled and linked together. This means you can only have one main() in your entire project. So putting a bunch of exercise files in one project will not work. Here's an easy solution:

Create a new project and select General -> Project for the type of project. Inside of that generic project, link to your directory with all the exercise files in it. (Right-click on the project, select New->Folder, press Advanced in the dialog box, select "Link to alternate location".) If the directory has sub-directories that's fine -- it works great. This will serve as an easily accessible repository for you, and it will not get compiled.

Now create another project and select C++ Project for the type of project (I call this one "Working"). Now for each of the lessons copy the file (or files) you'll be working with from the general project into the Working project. Do your exercises, play with the files, etc. You still have the original files because you're working on copies, so feel free to make lots of mistakes.

When you're done with each exercise just delete the file(s) from Working and run Clean from the Project menu (this step is especially important on Windows using MingW) before copying the next set of exercise files into Working.

I found this workflow to work really well for this purpose.

//Bill

like image 71
Bill Weinman Avatar answered Oct 14 '22 10:10

Bill Weinman