Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create two mains in an eclipse C++ project

We've got a program which runs separately, executed with an execvp command. So it needs a main method, but I believe that poses a problem to eclipse with a managed make. Do we have to keep this code segregated into a separate project, or is there a way to incorporate it into the same eclipse project?

like image 295
Jack BeNimble Avatar asked Jun 18 '09 20:06

Jack BeNimble


People also ask

Can you have multiple main in C?

it's not possible in c to have more than 1 main-function. you could use preprocessor directives like "#ifdef" to compile just one main-function at the time.

Can a C++ project have more than one main?

It could compile each main-containing file into separate EXE and run it by right-click on source CPP (for example). C++ is not Java :) You can have multiple projects in solution, but you cannot simply run a cpp file.


1 Answers

Create a project for each executable that has a main() function, and create an additional project to represent the software as a whole (a "container" project of sorts). Eclipse allows you to specify projects as dependencies of other projects, and in this case you will want to set up the container project to list the other projects as "Referenced Projects".

To do this, create the container project, then right-click on the project in the left-hand column (project explorer) and click "Properties". A dialog box will appear. Select the "Project References" item in the list on the left-hand side and you will see a list of all projects that Eclipse is currently working with. Check the boxes next to the projects for your individual executables, then click OK. Now, when you perform a build on the container project, Eclipse should automatically perform a build on these dependent projects as well.

When using sub-projects in this manner, I have (personally) found it useful to create a working set that includes the container project and all of the sub-projects (this can make searching the entire software project easier).

like image 89
bta Avatar answered Nov 12 '22 20:11

bta