views:

1681

answers:

3

add_definitions adds definition that is fixed at time when cmake (not make) is executed. How to define them when make (not cmake) is executed? The value of the definition will be an output of a custom command.

A: 

Here is an example of a make file:

.SUFFIXES: .c.o
CC = cc
INCLUDE = -I/usr/include -I/usr/sys/include
# Comment out one or the other, mutually exclusive
CFLAGS = -g # Debugging info
#CFLAGS = -O2 # Optimize code
LIBS = -L/usr/lib -L/usr/sys/lib -lsome_lib1 -lsome_lib2 # Debugging libs
#LIBS = -L/usr/lib # Release
FILES = mysource.o myroutines1.o myroutines2.o
APP = myapp
.c.o:
     $(CC) $(INCLUDE) $(CFLAGS) -c $<

all : $(APP)

$(APP) : $(FILES)
     $(CC) -o $@ $(FILES) $(LIBS) $(CFLAGS)

clobber : clean mrproper

clean:
     rm -rf $(FILES)

mrproper:
     rm -rf $(APP)

Notice how the flag CFLAGS is defined. Comment out the other depending if you are doing an debug or release build..

The above sample is for a make file from my tutorial for building an executable called myapp which composes of mysource.c, myroutines1.c and myroutines2.c. And it shows how make is intelligent enough to build an executable without recompiling.

Note The makefile has unix/linux orientation and is very stringent on tabs/spaces which do not show up in the example above!

Hope this helps, Best regards, Tom.

tommieb75
Don't understand why this was downvoted as the OP clearly stated 'How to define them when make (not cmake) is executed?' in other words how to define them in a makefile, that was my understanding? Correct me if I misunderstood?
tommieb75
I'm not the OP, and this is a little late... but... I imagine that the OP (like myself) is looking for a CMake command that implements this logic in the automatically generated Makefiles. Manually editing Makefiles isn't an option.
Matt B.
@MattB: Oh... have forgotten about this one...a little too late!!! I do know there's a CMake GUI..its not much of an answer but more of a point in the direction to see if there's an option on the CMake GUI to do that for you and see what it generates...
tommieb75
Thanks, Tom. I found my solution through the link in the other answer here (http://stackoverflow.com/questions/1438535/how-to-run-a-command-at-compile-with-in-makefile-generated-by-cmake/1468695#1468695). I just thought I'd drop you a note on why I thought someone may have downvoted you. Of course, now I see your comment on the other downvoted answer, so you already knew. Oh well...
Matt B.
A: 

I'm not sure I understand the question, but...

Perhaps you could use an additional macro for providing command-line C compiler flags, e.g.:

CFLAGS=  <whatever>
CFLAGS2= #nothing  

.c.o:
    $(CC) $(INCLUDE) $(CFLAGS) $(CFLAGS2) -c $<

Then when you issue the make command, provide a "CFLAGS2=xxx" command line argument, which overrides the default setting in the makefile.

Update

Yep, I did not understand the OP's question. So kindly disregard.

Loadmaster
@Loadmaster: I did the same thing here but got downvoted as I do not understand the OP's question - he did say 'when make (not cmake) is executed?'...
tommieb75
The question is about cmake. CMake generates a makefile on linux platform, so I can't directly modify it.
Łukasz Lew
@Lukasz Lew: Ahh....no wonder...I got downvoted...lol...
tommieb75