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?

1

5 Answers 5

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

2
  • 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, 2017 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, 2017 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
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.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.