Incorrect makefile - can't launch an action.

Well, when it comes to makefile, i'm a total noob, and i've just recently written one for my small project.
However, when trying to run 'make debug' option,it stops with 'no rule..' error.
More than that, i'm not sure whether that action will work.
The makefile in question -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
debug: CXXFLAGS = -D "IS_VERBOSE"

	
all: hello
 
hello: main.o preprocess.o functions.o
	g++ $(CXXFLAGS) main.o preprocess.o functions.o -o program

main.o: main.cpp
	g++ $(CXXFLAGS) -c main.cpp

preprocess.o: preprocess.cpp
	g++ $(CXXFLAGS) -c preprocess.cpp

functions.o: functions.cpp
	g++ $(CXXFLAGS) -c functions.cpp

clean:
	rm -rf *o program

Ok, so two questions here -
First, how to make that work?
Second, what i want to achieve, is that, i coul use #ifdef IS_VERBOSE in my files, so if i made a program with debug parameter, it would work with IS_VERBOSE #definition visible in code. Is that correct way of thinking?
It is to be compiled under linux.
Ok, i found what's the problem, but then, another surfaced.
I moved CXXFLAGS to new line.
but then, there's this little one annoying bug -

CXXFLAGS = -D "IS_VERBOSE"
/bin/sh: CXXFLAGS: command not found
make: *** [debug] Error 127

What should i do?
What does it look like now -please repost the makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
all: hello

debug: 
	CXXFLAGS = -D "IS_VERBOSE"
	hello
 
hello: main.o preprocess.o functions.o
	g++ $(CXXFLAGS) main.o preprocess.o functions.o -o program

main.o: main.cpp
	g++ $(CXXFLAGS) -c main.cpp

preprocess.o: preprocess.cpp
	g++ $(CXXFLAGS) -c preprocess.cpp

functions.o: functions.cpp
	g++ $(CXXFLAGS) -c functions.cpp

clean:
	rm -rf *o program
That CXXFLAGS macro is misplaced.

What are you hoping to achieve with the debug target ??

Ok, so, with debug target, well... i said that in the first post. To be able to use it as a preprocessor symbol. In the code. For example, #define IS_VERBOSE, but not in any header, nor .c/cpp but in makefile file, and then use it with #ifdef, #ifndef, etc... for example, in main.cpp
1
2
3
	#ifdef IS_VERBOSE
	cout << "make debug works \n"
#endif 


And it can't be done in any other way, it has to be done in makefile. At least, that what my school's project says -
Make a mechanism that will log what application does when given -v parameter. The mechanism should be avaible only if made using make debug command.

ANd how do i 'de-misplace' CXXFLAGS?
Last edited on
Maybe they just meant this


CXXFLAGS = -D "IS_VERBOSE"
all: hello
 
hello: main.o preprocess.o functions.o
	g++ $(CXXFLAGS) main.o preprocess.o functions.o -o program

main.o: main.cpp
	g++ $(CXXFLAGS) -c main.cpp

preprocess.o: preprocess.cpp
	g++ $(CXXFLAGS) -c preprocess.cpp

functions.o: functions.cpp
	g++ $(CXXFLAGS) -c functions.cpp

clean:
	rm -rf *o program
But then, there's a problem. There is no place for 'make debug' here. And it can be made using one of these three commands -
make debug
make all
make

and, of course, make clean.

And as such, this make doesn;t cut it.

SO, how do i make CXX flags change its value accordingly to the position where the make was launched from.
There's no helping some people is there?

I can think of a couple of ways to do it.
This involves a conditional statement, and passing an extra parameter
on the command line to make.
If your makefile looks like this:
ifdef BUILD_TYPE
CXXFLAGS=
else
CXXFLAGS = -D "IS_VERBOSE"
endif

all: hello

hello: main.o command.o llist.o
	g++ $(CXXFLAGS) main.o command.o llist.o -o c_temp.exe

main.o: main.cpp
	g++ $(CXXFLAGS) -c main.cpp

llist.o: llist.cpp
	g++ $(CXXFLAGS) -c llist.cpp

command.o: command.cpp
	g++ $(CXXFLAGS) -c command.cpp

clean:
	rm -rf *o c_temp.exe
    


Notice the first few of the makefile.

You can now invoke the makefile like this.

make BUILD_TYPE=RELEASE to get the release build.
make all BUILD_TYPE=RELEASE to get the release build.

make BUILD_TYPE= to get the debug build.
make all to get the debug build.

The important thing is that BUILD_TYPE must = something to get the release build. You can say BUILD_TYPE=dog if you wanted.
To get the debug build omit the BUILD_TYPE option or say BUILD_TYPE=
Last edited on
Well, although i understand what you've said there, guestgulkan, i still don't know how to make that usable with make debug option.
The teacher won't listen to the "oh no, it's different, so please write make BUILD_TYPE". He will simply write make debug and check whether it works.
I tried to make it myself,and althouhh your solution works, the anwser is inadequate, sorry, but thanks for help.

So, how to make it work by writing make debug?

Oh, and btw, even if i force -D "IS_VERBOSE", it still doesn't work in main.cpp, the
1
2
3
#ifdef "IS_VERBOSE"
	cout << "make debug works \n";
#endif 

just doesn't work. So there are more mistakes than i think.
Last edited on
Try this
(The CXXFLAGS on line 1 is used when we say make or make all.
if we say make debug we call make recursively passing on the modified CXXFLAGS as required for generating the debug code).



CXXFLAGS=
OBJECTS=main.o command.o llist.o
EXE_NAME=c_temp.exe

all: hello

debug:
	$(MAKE) all CXXFLAGS='-D "IS_VERBOSE"'

hello: main.o command.o llist.o
	g++ $(CXXFLAGS)  $(OBJECTS) -o $(EXE_NAME)


main.o: main.cpp
	g++ $(CXXFLAGS) -c main.cpp

llist.o: llist.cpp
	g++ $(CXXFLAGS) -c llist.cpp

command.o: command.cpp
	g++ $(CXXFLAGS) -c command.cpp

clean:
	rm -rf *o c_temp.exe
Topic archived. No new replies allowed.