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

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?

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

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

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.

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

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.

Related

How can I build multiple products with different dependency versions in Visual Studio?

I'm building a Ruby C Extensions and I have to build for both Ruby 1.8 and Ruby 2.0. The source code is the same, but I have to link to different Ruby libs and set different build paths.
In Xcode I was able to set up a project where the project contained the common configuration. Then I set up two targets that configured different include paths and libs as well as different output (Debug/Release) paths.
When I build I will get two products built, one for each targeted Ruby version. When I build Release I get:
build/Release/1.8/Example.bundle
build/Release/2.0/Example.bundle
Now I am trying to do the same thing for Visual Studio 2010. I've been looking at the Configuration Manager and Property Sheets, but I'm struggling to fond out how I can make the same type of inheritance for the targets as I can in Xcode.
Is it possible to have multiple targets in VS, as you can in Xcode, that produce two products from the same project when you build?
Do I have to create a Debug and Release configuration for each Ruby core I want to target?
I think I know what you are looking for (but I can't be sure, as I have no experience with Xcode.)
If you are using Visual Studio, one way you can do something like this is to set up you project properties for a build configuration (e.g. for Release) and then copy that config into a new build type (e.g. call it "Release20") and go in and change the properties that you want for the new build. You can do this in "Configuration Manager". (When you select to add a new configuration, it lets you copy all properties from an existing one.)
However, it seems that you are already familiar with this, so I'm not sure what the problem is.
Another thing you should know is that the two build configs don't stay linked together (this is not a proper inheritance and overriding of properties, like using property sheets.) If you change something in one, it doesn't affect the other.
Precisely for these reasons (in addition to some others) I myself have foregone trying to wrangle the VS build system. I now use Premake and CMake for all my projects. But of course it's quite a chore building a build system for someone else's large code base.

Eclipse CDT : How to manage multiple main() functions in a single C++ project?

I am starting the developpement of a project which will be made of multiple modules. I will validate each of those modules with a dedicated testbench, each with their main() function.
I would like to avoid having one Eclipse project for each testbench and its main() function.
I read about Working Sets and the idea of showing only the files concerned for a particular testbench in my project is what I search for.
However, during the build phase, Eclipse continues building all the project files, not the current Working Set files only. I then have a Linker error because of multiple main() functions.
How can I configure Eclipse to Build the files of a Working Set only?
Is there another way to build multiple programs separately in a single project, i.e. an application and its components testbenches?
It is possible to deal with multiple main functions in Eclipse CDT by writting a custom makefile. It is a potential solution for C/C++ projects dealing with a main application and its components testbenches, for instance.
See here how to specify a custom makefile in Eclipse.
Then, define a main rule (see makefile documentation) building your whole application (without the testbenches) and define one additional rule for each of your testbenches (with its component) to be built.
To summarize : define one rule in your makefile for each of your main function, building the main and its dependencies.

Having several projects on VS2010 and several "main" functions

I'm working on a game which has different "modules" that I am developping on separate projets for now, with VS2010.
So far I have each project in its own solution, and the main.cpp file is used to basically initialize and do some quick tests on my project, that I change very often.
Is it possible (and how) to have 1 solution regrouping several projects while at the same time having also one "main" function per project that I could launch independently of the other projects to test one project specifically?
Here's what I would do:
Put all your projects in one solution. There is some button or menu option somewhere to Add a Project to a solution. The advantage of this is that you can be editing multiple projects at once and Visual Studio will automatically rebuild everything that needs to be rebuilt when you compile.
All your reusable code (code used in more than on executable) should be divided up into projects that compile to DLLs.
For each executable you want to generate, you should have a project that compiles to an executable and references/links to the DLLs it depends on. Each executable project will have its own main function which runs when you run the exe.
If you want to have a way to test your DLLs without generating an executable, you can make an entry point in your DLL and run it using rundll32. This would be good for developers testing your DLLs, but I would never tell a user to use rundll32.
The build configurations are specific of each project, including which class contains the main method -IIRC, you can define several main in your project and define which one should be called through project configuration-.

gTest and multiple main()

I have an Eclipse project. All testcases are in one *.cpp file. The problem is that this way I end up with two main() functions. One for the app itself and one for the testcases. And Eclipse, of course, refuses to build... I would like to keep everything together under one project (and avoid having multiple configurations, SVN repositories etc). Ideally, I would want to force Eclipse to build two executables (one for the app and one for the testcases). I had a quick look at the Eclipse Makefile, but to be honest, I don't quite understand how exactly it works. It is possible to always exclude Main.cpp/Testcases.cpp and build one executable, but it is not very elegant...
Anybody with similar experience?
I stumbled over this link which was very helpfull: http://pezad-games.googlecode.com/svn/trunk/GTestTest/HOWTO.
The author is describing how to setup gtest with one project in eclipse whilst having two source files with main() methods: one in a directory called "src" and the other in a directory called "test".
Therefor he introduces a new configuration (besides DEBUG/RELEASE) called "GTEST" - in this configuration all linker/header includes are set to gtest and also an exclude to the src/[main].cpp:
Properties for main.cpp >> C/C++ Build >> Settings: Exclude resource
from build
On the other side DEBUG & RELEASE configs exclude the test/[main_test].cpp.
Are you linking with libgtest_main in addition to libgtest? If you don't link in the libgtest_main you should be good.
If you want to create two executables with Eclipse CDT the easiest way is to have each executable have a separate project. You can have one project reference another if you have common code.

Visual Studio: how to create a project that would compile 2 exe files?

So I have main.cpp and main2.cpp with int main in each. I want to get 2 exes out of it. Is it possible and what would be instruction to create such project?
Nope, Visual Studio's project model is rigidly built around the assumption that "one project generates one output".
If you need two executables, you have to create two projects. You can keep them in the same solution to make things easier for yourself, but they have to be separate projects.
Edit
Ok, as other answers have pointed out, it can of course be done, if you're desperate. You can add a custom build step, which does anything you like, including building another executable. (However, the build system won't understand that this file should be considered project output, and so some scenarios may fail: for example the file won't be automatically copied to the output folder, and when checking dependencies before a rebuild, it might not be able to understand which files to check, and what (or how) to rebuild.)
Visual Studio (at least up to 2008, not sure about 2010) also allows the use of nmake files, but then I think you're stretching the definition of "a Visual Studio project".
But under "normal" circumstances, one project implies one output. And in order to get two executables, you'd normally create two projects.
You need a solution which includes two projects. Have a read of the Visual Studio documentation on solutions and projects.
Here's my solution, since nobody in a Google search seems to suggest this method. It's quite simple and I've used/seen it used in other IDEs (like Code::Blocks).
Within your project, create a configuration for each output that you want. Then, only include one main source file in each configuration.
In VS, this means for each source file with main: right-click -> Properties -> Excluded From Build = Yes. So, once you're done, only one main source is built for each configuration. You can then specify a different output for each configuration in the Project Properties. I did this on VS 2010, but it should probably work with other versions.
I'm using this approach so that I can have several tests for one project, without cluttering the solution with more test projects than actual code projects.
I don't know if it can be done ,but the only change you have ,to do this ,is with custom build step.
EDIT: Since someone downvoted this ,i did a test making a dummy configuration.
In the custom build step I two Link-cmds (copied form original link-cmdline and modified it a bit) taking as input main1.obj resp. main2.obj and outputting App1.exe resp. App2.exe.
It's executed after Compiling and before linking.
It worked !
The downside is I cannot prevent (yet) the linking ot the orinal exe (which errors on duplicate main function).
Solution to this could be to have a lib project excluding the sources with main()from build and build them in the custum-step too.
So the answer to the question should : Yes ,it can be done!
You can't have more than one main() function in a single visual studio project. So you can't compile 2 executables, the only way is to make two different project in the same solution
You can create a solution with two project one for each output you want. Then head to Build menu and select Batch Build.. and select both projects.
If you want both exe files to be in one place you can specify a custom Post-build action:
For both project:
view the project properties page and in Build events select Post-Build events, then in the Command line field enter the command that will copy the output to the location you want, something like:
copy $(TargetPath) c:\yourlocation /Y
Then after you build the solution the output files will be copied to that location.
Another option you have is to use conditional compilation with sth like
main()
{
#ifdef VERSION1
call_main_logic();
#else
call_main2_logic();
#endif
}
and then define different configurations within the project. For each configuration you will then need to define preprocessor symbols appropriately (in: project settings -> configuration properties -> C/C++ -> preprocessor).
Please note, that it will be only one executable created at a time, but with switching configurations you'll get the one that does what you want at the moment.
... which may suit your needs or not, depending on more specific situation that you are in.
Edit: In addition, since different configurations create their separate output folders, you will have your both execs as outputs.