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

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.

Related

Eclipse combining applications

I have two applications. One I wrote and one that comes from a third party vendor. My application in written in C, theirs in C++. My project builds in two ways, within eclipse or with autotools utilizing a makefile.am and configure.ac. To build their application in eclipse I have to run a build target. The build targets are:
build make --makefile=./Makefile ARCH=x86IsoApp
clean make --makefile=./Makefile ARCH=x86IsoApp clean
This works to give me an executable. isoApp, this can then be placed into a run configuration allowing me to debug.
My question is, how do I include this my application? I have tried copying in the files, adding build paths but their code is large and complex. There are many dependencies that I am unable to resolve. Once resolved I get linking errors. I have tried many times to get their code into my codebase. (They have C++ files with C extensions that refuse to compile until renamed). Is there a way to use the generated binary to access their APIs?
My code runs on an iMX and is compiled using an SDK I created using Yocto. If I cannot build the third party code in eclipse, I cannot place it in my Yocto build.

Multiple main() functions within a Netbeans (C++) project

the problem is the following. We have a (rather large) code base in form of a Netbeans C++ project. On compilation/execution, the program generates/executes a binary which then runs the main program. However, now we need to implement several smaller tools (like compilers, converters, etc.) which use many classes from within that code base.
Is it possible to setup the Netbeans IDE to generate multiple executables from within a single project? Things that would work, but are rather inconvenient cover the following:
Compile the whole project as both an executable and a library. Use the library to create compilers, converters, etc (we would have to write a lot of extra code...)
Create different targets, each having an individual macro defined. Use that macro inside the code to exchange the main function (this is just dirty and it doesn't really solve the problem of pushing a single button to compile the main program and all the tools associated with it).
Any suggestions?
I'm not sure whether I understood you project right, but for the task of building several executables I suggest the following: in the build command of your configuration call a script that builds all the executables you need.

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.

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