Makefile for use with gdb - c++

I need help configuring my makefile to use it with the GNU debugger. I am running it on debian.
I am quite new to makefiles and after going through similar questions I've tried adapting the answers of those to my code, but it didn't work out the ways I tried (probably because i don't fully understand the syntax of makefiles).
This is the original (shortened) makefile:
INC=-I include
all: libs poisson_solver
poisson_solver:
g++ -o bin/poisson $(INC) src/main.c\ src/problem_setup.c\ libs/timer_tools.o
libs: libs/timer_tools.o src/problem_setup.o
libs/timer_tools.o: utilities/gettime.c
g++ -c -o libs/timer_tools.o $(INC) utilities/gettime.c
src/problem_setup.o: src/problem_setup.c include/problem_setup.h
g++ -c -o src/problem_setup.o $(INC) src/problem_setup.c include/problem_setup.h

Your Makefile has several errors, and in general contains more cruft than it should.
Here is roughly what it should be:
CFLAGS = -Iinclude -g
OBJS = src/main.o src/problem_setup.o utilities/gettime.o
all: poisson_solver
poisson_solver: $(OBJS)
src/problem_setup.o: src/problem_setup.c include/problem_setup.h
See this section of the manual.

Related

GDB no debugging symbols found

I am trying to compile a program consisting of two source files:
wildcardtrie.h, wildcardtrie.cpp
using a Makefile. However, when I run GDB to debug, I get the following error:
Reading symbols from /home/meric/Documents/Random/SectionLeading/wildcardtrie...(no debugging symbols found)...done.
I have tried a number of different compiler flags, none of which worked. The thing that perplexes me is that I have used a nearly identical Makefile in other programs and missing symbols has never been a problem. I have included the Makefile below:
CC=g++
CFLAGS = -g -ggdb g++ -O0 -Wall -Wfloat-equal -Wtype-limits -Wpointer-arith -Wlogical- op -fno-diagnostics-show-option
LDFLAGS = -g -ggdb -std=c++0x
programs = wildcardtrie
all : $(programs)
clean:
rm -f $(programs) core *.o
.PHONY: clean all
I have tried removing '-g' and '-ggdb' in the compiler and linker flags, but nothing seems to work. When I call 'make', I get the following output on the terminal:
g++ -c -o wildcardtrie.o wildcardtrie.cpp
g++ -g -ggdb -std=c++0x wildcardtrie.o -o wildcardtrie
Any help would be greatly appreciated!
g++ -c -o wildcardtrie.o wildcardtrie.cpp
This clearly shows that -g is not on your compile line (which is exactly the cause of your problem).
To get -g there, either add it to CXXFLAGS (this is the preferred solution), or just write the compile rule explicitly (instead of relying on built-in make rule):
wildcardtrie.o: wildcardtrie.cpp
$(CC) $(CFLAGS) -o wildcardtrie.o wildcardtrie.cpp

Making -std=c++11 the default in mac terminal

When I want to compile, I need to specify -std=c++11 like this:
g++ -Wall -std=c++11 main.cpp -o main
and I wonder if there was a solution to set the -std=c++11 flag permanently so it will be possible to do:
g++ -Wall main.cpp -o main
without flags.
Create an alias: alias g++='g++ -std=c++11' should do the trick.
(However, the version of GCC that comes with OS X is so ancient that it doesn't support C++11, you'd be better off using clang and clang++.)
I know this already has an accepted but I feel like I have some advice to offer. For one you should be using a makefile for c++, this is the one I use for answering on SO.
CFLAGS=-std=c++11
CFLAGS+=-stdlib=libc++
CC=clang++
#flags for test.c
cc=clang
DEBUG=-g
#warnings
WARNINGS=-Weverything
#always have -Weverything on for SO lol
OPT= -O0 -O1 -O2 -O3 -O4
test: test.cpp
$(info set CC for compiler)
$(CC) $(CFLAGS) $< -o $# $(DEBUG)
stack: stack.cpp
$(CC) $(CFLAGS) stack.cpp -o $# $(DEBUG) $(WARNINGS)
testc: test.c
$(cc) $< -o $# $(DEBUG)
clean:
rm test
Now whenever I download someones crappy code from SO I have a makefile for c and c++ files where I can easily change the flags if I want to.
As for bash alias I would suggest you alias it like so alias clang++11='clang++ -std=c++11 this way you don't overwrite the clang++ if you don't want to use the c++11 standard. Lastly you can add the line I just showed you to your .bash_profile on a mac which is in your home or ~ folder, this will make the change permanent. Once you change it run source .bash_profile to put the changes into effect. On linux I think the file is called .bashrc. Hopefully these tips will help you out when ur c++ing, I would advise you to learn the mac command line, has differences from the linux one, it can be very useful to know some of the things it can do.

Simple makefile for compiling a shared object library file

I need a very simple makefile to compile a shared object library file (*.so). I also need to know how to pass optimization parameters like -O2 and -O3. I tried to search for simple examples using google, but all examples are twisted.
I don't need to create any version like *.so.1.0 but only a simple *.so file. My project would have multiple files, so I need an example which compiles multiple files.
The simplest makefile that I can think of that does what you want:
CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
g++ -shared $^ -o $#
In the alternative, you may want to use more of make's built-in rules and variables:
CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
$(LINK.cc) -shared $^ $(LOADLIBES) $(LDLIBS) -o $#

makefile auto removing .o files

I am taking a C++ course in college and they want us to manually type in all of the test files... I know, however, that there is a way to do it with out, which is how I ended up with the current(http://pastebin.com/6d9UtKM4) makefile. My question is, why is this makefile automatically removing all the .o files it uses for compiling when it is done? It's not killing me, but I would like to preserve the .o files. I have pasted the makefile here(http://pastebin.com/6d9UtKM4). I have also pasted the current result of running "make tests" here(http://pastebin.com/h3Ny3dib). (Note the part at the bottom of that page that removes all the .o files automatically.)
I would also like to be able to make it generate it like this:
g++ -o compileDir/assembler.o -c -Wall src/assembler.cpp
g++ -o compileDir/string.o -c -Wall src/string.cpp
g++ -c -Wall -o compileDir/test_assignment.o testSrc/test_assignment.cpp
g++ -o testDir/test_assignment compileDir/test_assignment.o compileDir/string.o compileDir/assembler.o
g++ -c -Wall -o compileDir/test_bracket.o testSrc/test_bracket.cpp
g++ -o testDir/test_bracket compileDir/test_bracket.o compileDir/string.o compileDir/assembler.o
testDir/test_bracket
testDir/test_assignment
In other words, I want it to compile everything, then run everything. I hope this isn't too much to ask!
Edit: Additional Information: (This is the code that does "make tests")
tests: assembler.o string.o $(test_output) $(test_stringOutput)
#echo '--- Testing complete ---'
$(testDir)%: $(compileDir)%.o string.o
g++ -o $# $< $(compileDir)string.o $(compileDir)assembler.o
$#
#echo ''
$(compileDir)%.o: $(testSourceDir)%.cpp
g++ -c -Wall -o $# $<
$(compileDir)%.o: $(testStringSrc)%.cpp
g++ -c -Wall -o $# $<
EDIT: -----------------------------------------
Resolved via comments:
Adding this line fixed it:
.PRECIOUS $(compileDir)%.o
You might add
.PRECIOUS: %.o
which should be implicit, but perhaps you've got a weird setup.
Make treats your .o files as intermediate and removes them. You can prevent automatic deletion of those by adding them a dependency of the special .SECONDARY target. See Chains of Implicit Rules for more details. Good luck!

No debugging symbols in gdb even when compiling with -g flag

I am trying to compile my program with debugging symbols for use in gdb. I have added the -g flag to my makefile but I still get "Reading symbols from ...(no debugging symbols found)" when I load the program in gdb. What is wrong??
Here is a stripped down example of my makefile which should have the relevant bits:
CPP = g++
CFLAGS = -c -g -Wall
$(BIN): $(OBJ)
$(CPP) $(LDFLAGS) $(OBJ) -o $(BIN) $(LIBS)
<test.o>: <test.cpp>
$(CPP) $(CFLAGS) <test.cpp> -o <test.o>
If you'd like to see the whole thing you can go here instead, though I don't think it's necessary:
http://pastebin.com/vGNjy0ga
Miscellaneous notes.. I'm compiling with MinGW on Windows and I have SFML and OpenGL as dependencies.
And no, the -s flag is nowhere to be found in my makefile.
Ahh. I'm very sorry. It turns out the "clean:" portion of my makefile is broken. Thus when I used make clean nothing happened. Deleting the .o files manually fixed the problem. The flags work perfectly now. Thanks to everyone who posted anyway! This can be deleted now.
I had the same issue when using a makefile I inherited on some old F77 code. I tried all the flags people recommend (-g -ggdb ...etc.)
the solution was run make clean
If you don't have that or know what it means, basically delete all the compiled (.o) files.
the makefile didn't know to recompile since only flags were changed, so I wasn't actually compiling with -g or -ggdb when I thought it was. Hope this helps someone!
try to replace
$(BIN): $(OBJ)
$(CPP) $(LDFLAGS) $(OBJ) -o $(BIN) $(LIBS)
with
$(BIN): $(OBJ)
$(CPP) $(CFLAGS) -o $(BIN) $(OBJ) $(LDFLAGS) $(LIBS)
(edit)
Note: -c option will not work with executable
I dont have much experience with Mingw but try replacing -g with -ggdb. This may solve your problem. According to gcc man page
Produce debugging information for use
by GDB. This means to use the most
expressive format available (DWARF 2,
stabs, or the native format if neither
of those are supported), including GDB
extensions if at all possible.
I think you need -g when linking the object into a binary code.
CPP = g++
CFLAGS = -g -Wall
$(BIN): $(OBJ)
$(CPP) $(CFLAGS) $(OBJ) -o $(BIN) $(LDFLAGS) $(LIBS)
<test.o>: <test.cpp>
$(CPP) $(CFLAGS) -c <test.cpp> -o <test.o>
I am not sure, but I think you need -g even while linking.