8

In my scenario I have a C++ project in CDT Eclipse. This projects however is rather a collection of individual (helper) programs than one complex application. Consequently I want to be able to build and run them individually.

My project structure is very simple and looks like:

src/app1.cpp
src/app2.cpp
src/...

Note that I do not have common header files or libraries. However I want to be able to add programs to this project just by creating e.g. src/appx.cpp

Ideally I want to have shortcuts for

  • "Build currently opened .cpp"
  • "Run binary of currently opened .cpp"

Any suggestions on how to achieve this behaviour, if possible without additional plugins?

dcn
  • 4,389
  • 2
  • 30
  • 39

1 Answers1

5

The straightforward way to succeed what you aim is to create a Makefile project with CDT and add a new target rule for each of your applications inside your Makefile. You can even use SCons or other build systems with a CDT Makefile project and obtain the same effect.

You can also trick the managed build to create executables instead of object files. Remove -c option from Other flags of C++ compiler settings inside project properties. This will produce a separate application file for each of your source files.

Application files which are created inside the build directory will have the object file extension and they will not be executable. To solve this, you can add a post build script in your project directory such as:

postbuild.sh for Linux:

 chmod +x *.o
 rename -v 's/\.o$//' *.o

or postbuild.bat for Windows:

rename *.o *.exe

After adding ../postbuild.sh or ../postbuild.bat as a post build command in your build settings, you applications will be ready to run. Right click on any of these executable files and choose Debug As or Run As and a new Run configuration will be created.

Also you will have to stop the linker of the managed build to prevent errors. This can be achieved with changing the linker command to true (Linux) or true.exe (Windows, msys).

Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59
  • Surely this allows me to independently build/run different apps. However it involves quite some effort to add new apps for example. I was looking for a more convenient way, that uses the files themselves as objects for build/run, similar to "run as application" for individual java classes – dcn Mar 20 '11 at 20:05
  • @dcn: Please see the updated answer and see if it works. I have tested it under Windows and switching to Ubuntu to test it. If it doesn't work for you, your feedbacks will be valuable. – Tugrul Ates Mar 20 '11 at 20:39
  • This solution does indeed work. Quite frankly I had hoped for a more clean/non-tricky solution using some eclipse project options, which I have not found yet, but my intuition says your suggestion is as good as it gets. – dcn Mar 20 '11 at 20:50
  • [this](http://stackoverflow.com/questions/2424795/building-multiple-binaries-within-one-eclipse-project) might also be useful. – zardosht Aug 03 '14 at 22:54