LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-11-2009, 05:01 PM   #1
StevenR
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
Makefile debug target


Hello,

I'm having a problem with a makefile script i'm trying to write.
I want to compile a C++ program in two possible ways: "make"
(default target, main) and "make debug". When i supply the debug
target, exactly the same as main should be built, but then with the
DEBUG flag (-g -D DEBUG).

This sounds really logical, but i can't find the answer anywhere
in the forums.

This is the script:

CXX=g++
CXXFLAGS=-ansi -std=c++98 -Wall -W
DEBUG=-g -D DEBUG
OBJS=main.o NgramCreator.o

main: $(OBJS)
$(CXX) $(OBJS) -o $@
$(OBJS): NgramCreator.h
$(CXX) $(CXXFLAGS) $(DEBUG) -c $*.cpp

clean:
rm -f *~ *.o main *.gch


I don't have a clue how to do this, can anybody help me?

Thanks

Steven
 
Old 01-11-2009, 07:47 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
If you don't mind running make clean when you switch between debug and normal builds the following should suffice. The main trick is Target Specific Variables.

Code:
CXX=g++
CC=g++ # CC is used for linking
CXXFLAGS=-ansi -std=c++98 -Wall -W
DEBUG=-g -D DEBUG
OBJS=main.o NgramCreator.o

main: $(OBJS)
debug: CXXFLAGS += $(DEBUG)
debug: main
$(OBJS): NgramCreator.h

clean:
        rm -f *~ *.o main *.gch
If you are going to be switching back and forth a lot, I would recommend keeping things in separate directories, like this:

Code:
CXX=g++
CC=g++
CXXFLAGS=-ansi -std=c++98 -Wall -W
DEBUG=-g -D DEBUG
OBJS=main.o NgramCreator.o
SRC=$(OBJS:.o=.cpp)
DEBUG_OBJS = $(addprefix debug/, $(OBJS))

main: $(OBJS)
debug: CXXFLAGS += $(DEBUG)
debug: main
$(OBJS): NgramCreator.h

clean:  
        rm -f *~ *.o main *.gch debug/*.o debug/main

$(DEBUG_OBJS) : $(SRC) NgramCreator.h

debug : debug/main
debug/main : $(DEBUG_OBJS)
        $(CXX) $(DEBUG_OBJS) -o debug/main

debug/%.o : %.cpp
        $(CXX) $(CXXFLAGS) $(DEBUG) -c $< -o $@
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
makefile subdirectory target sancho1980 Linux - General 1 08-18-2008 05:25 PM
makefile for debug and release version nikoladsp Programming 1 04-27-2007 04:30 AM
duplicated definition for a target in Makefile George2 Programming 2 05-07-2006 04:01 AM
strange target name containing % in Makefile George2 Programming 2 04-25-2006 06:28 AM
Trying to install kwvdial ,no target specified an no makefile found mtnhigh Linux - Software 0 09-27-2004 01:38 PM


All times are GMT -5. The time now is 08:56 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration