8

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?

Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213
  • 3
    Possible duplicate of [Building multiple binaries within one Eclipse project](https://stackoverflow.com/questions/2424795/building-multiple-binaries-within-one-eclipse-project) – e4c5 Oct 19 '18 at 06:19

5 Answers5

7

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).

bta
  • 43,959
  • 6
  • 69
  • 99
  • How do you stop the container project from complaining that it has no 'all' or 'clean' make targets? Secondarily, how do you specify the order in which the sub-projects will be built? (Right now my set of projects includes one project that builds a library all the other projects rely on, but the library isn't built first (the vagaries of naming) which means I need to build the container project twice) – Urhixidur Mar 10 '17 at 17:26
  • @Urhixidur - You'd have to mark the library as a dependency of the programs that use it, but I don't remember offhand how to do that in the Eclipse UI. It's quite simple if you're writing your own makefiles, though. For the first question, simply define the make targets (even if they don't actually do anything). – bta Apr 11 '17 at 20:55
3

Keep it in the same project and use preprocessor defines which you define differently depending on what kind of main you want to include in the current project. Here the mains are in the same file, but they can of course reside in different files.

#if defined(MAIN_ONE)
int main()
{
    // Do stuff
}
#elif defined(MAIN_TWO)
int main()
{
    // Do some other stuff
}
#endif
rtn
  • 127,556
  • 20
  • 111
  • 121
1

If the makefile being invoked doesn't compile the 2 main() methods into the same executable, it won't cause a problem. I don't know how eclipse projects are handled - if it's like VS, where "project" means a single executable or library, and "solution" is a group of "projects", then it would seem you'd need more than one project. If, OTOH, a "project" can contain different "subprojects" where a "subproject" is an executable or library, you should be able to handle that easily.

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
1

I am not aware of any easy way to build two mains using Eclipse build system. The smallest change you need to do might be to move to makefiles and use makefile targets to build.

Instead, I'd advise you to move to using CMake. CMake can be used to generate makefiles to be used with eclipse. The advantage you get from using CMake is that you can easily state how to build the libraries and link the libraries to form the executables. CMake can generate builds for Eclipse, Visual Studio, Code Blocks, or makefiles (so you can use command prompt).

amit kumar
  • 20,438
  • 23
  • 90
  • 126
0

This is built in the C++ language. You would have to modify it to get your result. There is something to do 2 things at once if that is what you want.