Tuesday, April 23, 2013

Multiple binaries in a single Eclipse CDT project

Sometimes it is convenient to have a single Eclipse project that produces multiple executables. An example is a set of tools that share some base code like a set of image manipulation tools that share some common image loading / saving code.  In a situation like this, the source code has multiple source files that contain a main() function. The standard way to do this is to create a makefile eclipse project and edit the makefile so that you have a target for each executable:

binary_1: main_1.cpp shared_code.cpp
       gcc -o  $@  $^

binary_2: main_2.cpp shared_code.cpp
       gcc -o  $@  $^

If for some reason you want to use a managed eclipse project (where the makefile is automatically generated) do the following:
  1. Create a managed project (File > New  C++ Project > Executable)
  2. Add the source code containing multiple main() functions
  3. Go to Project > Properties > C/C++ Build > Manage Configurations...
  4. Make a build configuration for each executable and name it appropriately (you can clone existing configurations like Debug and Release).
  5. From the project explorer, right click on each source file that contains a main() function > Resource Configurations > Exclude from Build and exclude all build configurations except the one that builds the executable with this main() function
  6. All other code is included in all build configurations by default. You may need to change this depending on your application.
  7. You can now build an executable for each main function by going to Project > Build Configurations > Set Active , Project > Build Project
To debug each executable:
  1. Build all binaries (as explained above) 
  2. Go to Run > Debug Configurations.
  3. Create a configuration for each binary
  4. For each configuration set the correct Build Configuration and C/C++ Application (this is the binary). 
  5. Use Search Project... to locate the binaries easily 

2 comments: