0

I've written an entire program and its makefile, the only trouble is I have to idea how to implement the debugger. I've been looking at similar questions online but to no avail.

Here's my makefile:

#  the variable CC is the compiler to use.
CC=gcc 
#  the variable CFLAGS is compiler options.
CFLAGS=-c -Wall 

assign4: main1.o ObjectManager.o
    $(CC) main1.o ObjectManager.o -o assign4

main1.o: main1.c ObjectManager.h
    $(CC) $(CFLAGS) main1.c

ObjectManager.o: ObjectManager.c ObjectManager.h
    $(CC) $(CFLAGS) ObjectManager.c

clean:
    rm -rf *o assign4

I've tried to adjust the CFLAGS: to -g Wall, but all it says is:

Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [ObjectManager.o] Error 1

Any suggestions to fix this?

2
  • 1
    do you have a main() in main.c? Commented Nov 28, 2015 at 2:25
  • 1
    Also, debugging a c program is normally done with gdb. You just compile your program, then run it like so: gdb assign4 Commented Nov 28, 2015 at 2:29

1 Answer 1

2

Look at this rule:

ObjectManager.o: ...
    $(CC) $(CFLAGS) ObjectManager.c

If CFLAGS is -c -Wall, that -c means that gcc will compile the source file, but not attempt to link it. This is exactly what was intended, to produce an object file and not an executable. One consequence of this is that the source dile (ObjectManager.c) need not contain a main function-- the compiler trusts you to provide one at link time.

But if CFLAGS is -g -Wall, the -g does not imply -c. So gcc will attempt to create an executable, and abort if main is not present.

Solution:

CFLAGS := -Wall -g # the -g can be added with a conditional if you like

ObjectManager.o: ...
    $(CC) $(CFLAGS) -c ObjectManager.c
Sign up to request clarification or add additional context in comments.

1 Comment

Since the makefile is using gcc to compile/link and gdb to debug, suggest CFLAGS := -Wall -Wextra -pedantic -ggdb and adding to the end of each compile statement: -I. (although the compiler, by default, will look in the local directory for header files that are #include'd using double quote marks

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.