Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse CDT build/run on file basis

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?

like image 634
dcn Avatar asked Mar 20 '11 17:03

dcn


1 Answers

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

like image 171
Tugrul Ates Avatar answered Oct 04 '22 21:10

Tugrul Ates