Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 497
feronjb Avatar asked Nov 05 '13 12:11

feronjb


People also ask

Is it possible to have more than one main() function in C program?

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.

How do I open an existing C project in Eclipse?

Open the Import Wizard Dialog by selecting File > Import... or activate a context menu on a C/C++ project and select Import.... Select C/C++ > C/C++ Project Settings. Click Next.

What is Eclipse CDT builder?

The CDT is Eclipse's C/C++ Development Tooling project. It is an industrial-strength C/C++ IDE that also serves as a platform for others to provide value-added tooling for C/C++ developers.


1 Answers

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.

like image 100
feronjb Avatar answered Sep 22 '22 11:09

feronjb