1

How can I make syntax control and debugging on makefile? I used g++ compiler. We can assume that following code our sample makefile.Thanks for your advice.

all: sample1
sample1: deneme.o hello.o
         g++ deneme.o hello.o -o sample1
deneme.o: deneme.cpp
         g++ -c deneme.cpp
hello.o : hello.cpp
         g++ -c hello.cpp
1
  • Perhaps you're confusing static analysis by 'syntax control'? Syntax is checked by the compiler and your Makefile should suffice for that. For debugging, it's not done in the makefile, you need to add another target with -g switch, say deneme-debug (and call it with make deneme-debug) so you can pass it to gdb for debugging if necessary. Commented Sep 14, 2015 at 13:14

2 Answers 2

0

Typically, you have makefile variables such as:

DEBUG=-Wall -g

and use them in your build commands:

sample1: deneme.o hello.o
    g++ deneme.o hello.o -o sample1
sample1-debug: deneme-debug hello-debug
    g++ $(DEBUG) deneme.o hello.o -o sample1
deneme.o: deneme.cpp
    g++ -c deneme.cpp
deneme-debug: deneme.cpp
    g++ $(DEBUG) -c deneme.cpp
hello.o: hello.cpp
    g++ -c hello.cpp
hello-debug: hello.cpp
    g++ $(DEBUG) -c hello.cpp

then use make sample1-degug for you debug executable.

Sign up to request clarification or add additional context in comments.

Comments

0

As for personal experience, I would keep the original structure of the makefile and insert the CDEBUG variable in each g++ recipe line. (of course, the makefile can be improved with static-patterns, which is not the case here). In that way all I have to do to generate a debuggable program is either to change CDEBUG declartion in the Makefile or to override in the make invokation 'make "CDEBUG=-g"'.

CDEBUG := -g #(or -ggdb, -g1- -g2 -gdwarf and so on)
all: sample1
sample1: deneme.o hello.o
         g++ deneme.o hello.o -o sample1
deneme.o: deneme.cpp
         g++ $(CDEBUG) -c deneme.cpp
hello.o : hello.cpp
         g++ $(CDEBUG) -c hello.cpp

The solution offered by Paul will work, but notice it will create lots of *-debug files which won´t be of any importance. But of course I will be happy to understand otherwise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.