Building several executables in one Eclipse CDT project

After having imported some C projects into Eclipse CDT, I needed, for one of the projects, to build several executables, all of them being defined in the same project. What’s more, some of the executables had to be linked with object files created by another project. This article describes the configuration I set up to meet theses requirements.

Context

Let’s consider the following context:

  • I have two projects: projectA and projectB
  • projectB defines one executable, resulting from mainb1.c source file
  • a new source file, mainb2.c, is added to projectB. It requires to be linked with functiona2.o, generated in projectA, in order to get another executable
  • I want to be able to build this executable only

I’m using Eclipse Oxygen 1a Release, on Linux Mint 18.2.

Creating a configuration

The first step is the creation of a configuration that will be used to generate the new executable, independently of the existing one:

  • Project / Build Configurations / Manage… / New…
  • name it projectBmain1, for instance
  • set the new configuration active, in the Manage Configurations window, or later, from Project / Build Configurations / Set Active

Adding a header path

Add path to projectA header files:

  • Project / Properties / C/C++ General / Paths and Symbols
  • in Languages column, select GNU C
  • click on Add… button
  • check Add to all configurations
  • click on Workspace… button
  • select include folder

I observed that I had to check Add to all configurations, to ensure that the directory was added to the project. Kind of weird…

Configuring executable generation

  • for every source file containing a main() function, other than mainb2.c, right-click on it, select Resource Configurations / Exclude from Build… and check projectBmain1 configuration

Declaring project dependency

ProjectB depends on projectA, as it uses one of its files. Declare this dependency:

  • Project / Properties / Project References
  • check projectA

Declaring external object file

To declare that functiona2.o object file from projectA must be linked with the executable:

  • Project / Properties / C/C++ Build / Settings / Tool Settings
  • select GCC Linker / Miscellaneous
  • click on the Add… icon for Other objects
  • click on Workspace… button and select functiona2.o file. It can be found in a subdirectory of projectA directory. The name of this subdirectory is the name of the configuration used to build projectA executable. The default name is… Default. Intermediate subdirectories can exist, depending on source files directory hierarchy. In our example, that’s Default/src.

Building another executable

To build the other executable of projectB, switch active configuration:

  • Project / Build Configurations / Set Active