31

I develop C/C++ using the Eclipse IDE. Eclipse also generates a makefile which I don't want to edit as it will simply be overwritten.

I want to use that makefile for nightly build within Hudson.

How do I pass #defines which are made in the project file of the IDE to the makefile ? (and why doesn't Eclipse already include them in the generated makefile?)

I actually had this figured out once, then accidentally overwrote it :-( But at least I know that it can be done...

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

4 Answers4

30

If you are running make from the command line, use

make CPPFLAGS=-DFOO

which will add -DFOO to all compilations. See also CFLAGS, CXXFLAGS, LDFLAGS in the make manual.

Scott Wales
  • 11,336
  • 5
  • 33
  • 30
  • 1
    It seems this overrides the CPPFLAGS that I am setting in the Makefile. Is there a way to add this flag to CPPFLAGS? – matec Oct 27 '14 at 13:43
  • 5
    @matec - Yeah definitions from environment variables have priority. You can use `CPPFLAGS += -DBAR` in your makefile to add more flags – Scott Wales Oct 27 '14 at 22:44
2

You could write a small program to include the headers and write a makefile fragment which you include in the main makefile (requires GNU make).

This is a fairly ugly solution that requires a fair amount of hand hackery. More elegant would be to parse the project file and write the makefile fragment.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • yes, but the thing is that I know that I did it before without resorting to hackery. Alas, I can no longer remember how. – Mawg says reinstate Monica Jan 11 '10 at 13:27
  • See comment below. Maybe the Jaunty/Kosmic CDT fiasco means I can't repeat whatever I did previously. I may well have to do as you suggest and read in the makefile, massage it and write it out to another makefile. Btw, teh #defines I want to use are not exactly the ones used in Eclipse (e.g, I may swap -Doutput=file for -Doutput=console, etc) – Mawg says reinstate Monica Jan 12 '10 at 02:35
1

For GCC use -D define.

OP commented below that he wants to pass the define into make and have it pass it on to GCC.

Make does not allow this. Typically you just add another make rule to add defines. For instance 'make release' vs 'make debug'. As the makefile creator you make the two rules and have the defines right in the make file. Now if Eclipse is not putting the defines into the makefile for you, I would say Eclipse is broken.

Gregor Brandt
  • 7,659
  • 38
  • 59
  • One of us has answered the wrong question. I *think* he has a c++ header with some `#defines`, and wants to use them to make decisions in make. You suggestion is correct if he has some variables in make and wants to get them considered by the preprocessor during c++ compilation. – dmckee --- ex-moderator kitten Jan 11 '10 at 03:02
  • Maybe I wasn't clear enough, sorry. I want to pass the #defines to make, which will then use them when calling gcc. So, something like `make -DmaxVal=42` – Mawg says reinstate Monica Jan 11 '10 at 13:29
  • Hmmm, I *know* that I managed it before, but forget how... I do agree that Eclipse out to be putting the defines into the makefile. I notice makfile lines `-include ../makefile.init' and `-include ../makefile.defs` but these files don't exist(!). The Eclipse CDT guys seem to have dropped the ball as there is no longer a package after upgrading from Jaunty to Karmic. There is a plethora or web pages offering different solutions as to how to install CDT. Maybe the one I chose "almost works". That is to say, works from within CDT, but not externally with `make` ? – Mawg says reinstate Monica Jan 12 '10 at 02:33
1

If you're using autotools another options is to have 2 directories 'bin/debug' and 'bin/release'.

# Simple bootstrap script.

# Remove previously generated filed and call autoreconf.
# At the end configure 2 separate builds.
echo "Setting up Debug configuration: bin/debug"
../../configure CXXFLAGS="-g3 -O0 -DDEBUG=1"
echo "Setting up Release configuration: bin/release"
cd bin/release/
../../configure CXXFLAGS="-O2"

Setup Eclipse. Open the project's properties (Project->Properties->C/C++ Build->Builder Settings) and set the Build Location->Build Directory to

${workspace_loc:/helloworld/bin/debug}

Replacing 'helloworld' with your project's directory relative to the workspace (or you can supply an absolute path ${/abs/path/debug}). Do the same thing with the Release config, replacing "/debug" with "release" at the end of the path.

This method seems like a waste of disk space, but a valid alternative to achieve completely separate builds.

Plamen
  • 165
  • 2
  • 10