Compiling with -g for GDB in Makefile - c

new to using Makefiles and was wondering how I would include the -g command (and anything else I need) in order to compile my program to be run with gdb. Here is the Makefile I was given. Thanks in advance.
include parallelDefs
COMMON = dataGen.h utils.h IO.h parseCommandLine.h graph.h graphIO.h graphUtils.h parallel.h sequence.h blockRadixSort.h deterministicHash.h transpose.h
GENERATORS = rMatGraph gridGraph powerGraph randLocalGraph addWeights randDoubleVector fromAdjIdx adjToEdgeArray adjElimSelfEdges
.PHONY: all clean
all: $(GENERATORS)
$(COMMON) :
ln -s ../../common/$# .
%.o : %.C $(COMMON)
$(PCC) $(PCFLAGS) -c $< -o $#
rMatGraph : rMatGraph.o
$(PCC) $(PLFLAGS) -o $# rMatGraph.o
gridGraph : gridGraph.o
$(PCC) $(PLFLAGS) -o $# gridGraph.o
powerGraph : powerGraph.o
$(PCC) $(PLFLAGS) -o $# powerGraph.o
randLocalGraph : randLocalGraph.o
$(PCC) $(PLFLAGS) -o $# randLocalGraph.o
addWeights : addWeights.o
$(PCC) $(PLFLAGS) -o $# addWeights.o
fromAdjIdx : fromAdjIdx.o
$(PCC) $(PLFLAGS) -o $# fromAdjIdx.o
randDoubleVector : randDoubleVector.o
$(PCC) $(PLFLAGS) -o $# randDoubleVector.o
adjToEdgeArray : adjToEdgeArray.C $(COMMON)
$(PCC) $(PCFLAGS) $(PLFLAGS) -o $# adjToEdgeArray.C
adjElimSelfEdges : adjElimSelfEdges.C $(COMMON)
$(PCC) $(PCFLAGS) $(PLFLAGS) -o $# adjElimSelfEdges.C
clean :
rm -f *.o $(GENERATORS)
make clean -s -C data
cleansrc :
make -s clean
rm -f $(COMMON)

If only randLocalGraph should be compiled with the -g option, then it will need an explicit rule:
randLocalGraph.o: randLocalGraph.C $(COMMON)
$(PCC) $(PCFLAGS) -g -c randLocalGraph.C -o $#
All the other objects will be created using the implicit rule.

Related

Makefile adding libraries from others directory

I want to create a Makefile for a big C project, with files divided into directories. What I wrote is
CC = gcc
AR = ar
ARFLAGS = rvs
CFLAGS += -std=c99 -Wall -g
INCDIR = ./include
LIBDIR = ./lib
SRCDIR = ./src
BINDIR = ./bin
OBJDIR = ./obj
TESTDIR = ./testfile
INCLUDES = -I.
LDFLAGS = -L/lib
LIBFUNCTIONS = /-lfunctions
LIBTH = -lpthread
LIBUTILITY = /-lutility
OPTFLAGS = -O3 -DNDEBUG
TARGETS = $(BINDIR)/test_functions \
.PHONY: all clean cleanall test1 test2
.SUFFIXES: .c .h
$(BINDIR)/%: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OPTFLAGS) -o $# $<
$(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OPTFLAGS) -c -o $# $<
all : $(TARGETS)
$(BINDIR)/test_functions: $(OBJDIR)/test_functions.o $(LIBDIR)/libutility.a $(LIBDIR)/libfunctions.a
$(CC) $(CFLAGS) $(INCLUDES) $(FLAGS) $(LDFLAGS) -o $# $< $(LIBUTILITY) $(LIBFUNCTIONS)
$(OBJDIR)/test_functions.o: $(SRCDIR)/test_functions.c $(INCDIR)/utility.h $(LIBDIR)/libutility.a $(INCDIR)/functions.h $(LIBDIR)/libfunctions.a
$(CC) $(CCFLAGS) $(INCLUDES) $(FLAGS) -c -o $# $<
$(LIBDIR)/libutility.a: $(OBJDIR)/utility.o $(INCDIR)/utility.h
$(AR) $(ARFLAGS) $# $<
$(LIBDIR)/libfunctions.a: $(OBJDIR)/functions.o $(INCDIR)/functions.h $(INCDIR)/utility.h
$(AR) $(ARFLAGS) $# $<
clean :
rm -f $(TARGETS)
cleanall : clean
\rm -f *.o *~
the problem is that when I try to compile the project using the makefile, the result is
gcc -std=c99 -Wall -g -I. -L/lib -o bin/test_functions obj/test_functions.o -lutility -lfunctions
/usr/bin/ld: impossibile trovare -lutility
/usr/bin/ld: impossibile trovare -lfunctions
collect2: error: ld returned 1 exit status
make: *** [Makefile:54: bin/test_functions] Error 1
it seems that the compiler cannot find the libraries. what can I do to make it work properly?
You have this:
LIBDIR = ./lib
then in your link line you have this:
LDFLAGS = -L/lib
Notice the difference?
Why don't you just say:
LDFLAGS = -L$(LIBDIR)
to avoid repeating yourself (and getting it wrong)?

GCC symbol lookup error

I try to launch my executable but i got this error:
.dudac/stage/monkey/lib/hello.duda: undefined symbol: DDS_sequence_BoardGlobalParameters_BoardGPSoftState__alloc
Whereas link and compile work fine
Here is my makefile:
# ====================================
# Date : Tue 21, Feb 2017 at 10:15
NAME = hello
CC = /opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/x86_64-wrlinuxsdk-linux/usr/bin/arm-wrs-linux-gnueabi/arm-wrs-linux-gnueabi-gcc
CFLAGS = -g -Wall -DDEBUG -L/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/RTS/armv7at2_vfp_neon.WRlinux7_gcc/lib -ldcpssac -ldcpsisocpp2
#-g -Wall -DDEBUG -fPIC
LDFLAGS =
DEFS =
INCDIR = -I/home/T0181049/.dudac/stage/monkey//include/ -I/home/T0181049/.dudac/stage/monkey//src/include -I/home/T0181049/.dudac/stage/monkey//plugins/duda/src -I/home/T0181049/.dudac/stage/monkey//plugins/duda/ -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/include -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/include/dcps/C/SAC -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/include/sys -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSplice/6.7.1p1/HDE/x86_64.linux/include -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSplice/6.7.1p1/HDE/x86_64.linux/include/sys -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++ -I/opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++/4.9.1 -I/opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++/4.9.1/arm-windriverv7atneon-linux-gnueabi -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++/4.9.1/backward -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/dtc -I/opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/x86_64-wrlinuxsdk-linux/usr/lib/arm-wrs-linux-gnueabi/gcc/arm-wrs-linux-gnueabi/4.9.1/include -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/x86_64-wrlinuxsdk-linux/usr/lib/arm-wrs-linux-gnueabi/gcc/arm-wrs-linux-gnueabi/4.9.1/include-fixed
OBJECTS = main.o
_PATH = $(patsubst /%, %, $(CURDIR))
_CC = #/bin/echo -e " [\033[33mCC\033[0m] $#"; $(CC)
_DD = #/bin/echo -e " [\033[32mDD\033[0m] $#"; $(CC)
_CC_QUIET = #/bin/echo -n; $(CC)
all: $(NAME).duda
$(NAME).duda: $(OBJECTS)
$(_DD) $(CFLAGS) $(DEFS) -shared -o $# $^ -lc $(LDFLAGS)
.c.o:
$(_CC) -c $(CFLAGS) $(DEFS) $(INCDIR) -fPIC $<
$(_CC_QUIET) -MM -MP $(CFLAGS) $(DEFS) $(INCDIR) $*.c -o $*.d > /dev/null &2>&1
clean:
rm -rf *.o *.d *~ $(NAME).duda
How do i resolve that ?
Thanks
During linking you have specified libraries that are not in the standard path
Solutions
link statically
Use LD_LIBRARY_PATH
Use LD_PRELOAD
Use rpath
I try to use rpath just like this but got the same error:
CFLAGS = -g -Wall -DDEBUG -L/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/lib -Wl,-rpath,/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/lib -ldcpssac -ldcpsisocpp2

Makefile error in C

CC = gcc
OPTS = -W -O3
SRCDIR=./src
OBJDIR=./obj
INCDIR=./inc
BINDIR=./bin
SRCS=$(SRCDIR)/Functions.c \
SRCS=$(SRCDIR)/Function1.c \
SRCS=$(SRCDIR)/Function2.c \
SRCS=$(SRCDIR)/Function3.c \
INCLUDE = $(addprefix -I,$(INCDIR))
OBJS=${SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o}
CFLAGS = $(OPTS) $(INCLUDE) $(DEBUG)
TARGET = $(BINDIR)/ Functions
all: $(TARGET)
$(TARGET): $(OBJS)
${CC} ${CFLAGS} -o $# $(OBJS)
$(OBJS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c $< -o $#
Functions.c has a main inside that calls Function1(); .....Function3();
I'm trying to add the debug flag based on #ifndef
How to add a debug flag and set the Debug compiler directive inside this Makefile? I have #ifndef NDEBUG...#endif from Functions.h
TARGET = $(BINDIR)/ Functions
should be ?
TARGET = $(BINDIR)/Functions
CC=gcc
OPTS=-W -O3
DEBUG=
RM=rm
SRCDIR=src
OBJDIR=obj
INCDIR=inc
BINDIR=bin
SRCS=$(SRCDIR)/*.c
INCLUDE=$(addprefix -I, $(INCDIR))
OBJS=obj/Functions.o obj/Function1.o obj/Function2.o obj/Function3.o
CFLAGS = $(OPTS) $(INCLUDE) $(DEBUG)
TARGET = $(BINDIR)/Functions
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $# $(OBJS)
$(OBJS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c $< -o $#
clean:
$(RM) -f $(TARGET) $(OBJDIR)/*.o
I couldn't get your OBJS expansion working, but the adove works on my end.

Makefile - wrong sequence of compiling

In makefile below I'm trying to compile one directory, but make against my will is trying to do it in wrong order. It's starting from compiling main.o and later it goes straight to compiling console. I have no idea why it's not trying to solve all dependences before it. I just get:
user#ubuntu:~/Dokumenty/Sysopy/cw1/zad3b$ make
gcc -c -Wall -I ./src/include src/main.c
gcc -o macierze main.o
main.o: In function `main':
main.c:(.text+0x28): undefined reference to `wczytaj'
main.c:(.text+0x31): undefined reference to `wczytaj'
main.c:(.text+0x7e): undefined reference to `suma'
...
collect2: ld returned 1 exit status make: *** [console] Błąd 1
Here's a makefile:
############ makra #############
CC = gcc
CFLAGS = -Wall -I ./src/include
LFLAGS =
VPATH = ./src/operacje: ./src/we_wy: ./src/reszta
########### pliki ##############
SRC_CONSOLE = wczytaj_konsola.c wypisz_konsola.c
SRC_FILE = wczytaj_plik.c wypisz_plik.c pliki.c
SRC_FUNCTION = suma.c roznica.c iloczyn.c macierz.c
HEADERS = suma.h roznica.h iloczyn.h wypisz.h wczytaj.h macierz.h
OBJ_SONSOLE = $(SRC_CONSOLE:.c=.o)
OBJ_FILE = $(SRC_FILE:.c=.o)
OBJ_FUNCTION = $(SRC_FUNCTIONS:.c=.o)
console: $(OBJ_CONSOLE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
file: $(OBJ_FILE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
console_logs: $(OBJ_CONSOLE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
file_logs: $(OBJ_FILE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
############# objekty ##############
main.o:
$(CC) -c $(CFLAGS) src/main.c
$(OBJ_CONSOLE): $(SRC_CONSOLE)
$(CC) -c $(CFLAGS) $^
$(OBJ_FILE): $(SRC_FILE)
$(CC) -c $(CFLAGS) $^
$(OBJ_FUNCTION): $(SRC_FUNCTION)
$(CC) -c $(CFLAGS) $^
%logs : LFLAGS += -D KOMUNIKATY
file% : LFLAGS += -D PLIKI
./PHONY: clean
clean:
rm -f *.o
rm -f ./src/include/*.gch
rm -f macierze
you write : OBJ_FUNCTION = $(SRC_FUNCTIONS:.c=.o)
instead of : OBJ_FUNCTION = $(SRC_FUNCTION:.c=.o)
and
OBJ_SONSOLE = $(SRC_CONSOLE:.c=.o)
instead of OBJ_CONSOLE = $(SRC_CONSOLE:.c=.o)

ISO C, sprintf and decimal conversion grouped with thousands

I'm trying to do a simple sprintf like this:
snprintf(dest, maxlen_dest, "%'lu", along);
Note the quote: ' in the string.
This means (according to the manual):
' For decimal conversion (i, d, u, f, F, g, G) the output is to be
grouped with thousands’ grouping characters if the locale
information indicates any. Note that many versions of gcc(1) cannot
parse this option and will issue a warning. SUSv2 does not include
%'F.
So when I compile, I get this warning:
../common/utils.c:29: warning: ISO C does not support the ''' printf flag
../common/utils.c:29: warning: ISO C does not support the ''' printf flag
So, my question is: is there a function that "supports the ''' printf flag" I could use?
I want my output to be like:
51 254 234
1 424 000
...
...
...
Here's my Makefile (in case there's some options I should modify):
CC = gcc
# -g = all debug options
CFLAGS = -g -I../common -W -Wall -pedantic -std=c99
OBJECTS = o/main.o o/opts.o o/verif_liens_bidirectionnels.o o/ecriture.o o/plugin_utils.o o/plugin_algo_groupes.o ../common/o/cell.o ../common/o/utils.o ../common/o/verif.o ../common/o/lecture.o
gen : $(OBJECTS)
$(CC) -o gen $(OBJECTS) $(CFLAGS) -lm -lgd -lz
o/main.o : main.c
$(CC) -o $# -c main.c $(CFLAGS)
o/opts.o : opts.c opts.h
$(CC) -o $# -c opts.c $(CFLAGS)
o/verif_liens_bidirectionnels.o : verif_liens_bidirectionnels.c verif_liens_bidirectionnels.h
$(CC) -o $# -c verif_liens_bidirectionnels.c $(CFLAGS)
o/ecriture.o : ecriture.c ecriture.h
$(CC) -o $# -c ecriture.c $(CFLAGS)
o/plugin_utils.o : plugin_utils.c plugin_utils.h
$(CC) -o $# -c plugin_utils.c $(CFLAGS)
o/plugin_algo_groupes.o : plugin_algo_groupes.c plugin_algo_groupes.h
$(CC) -o $# -c plugin_algo_groupes.c $(CFLAGS)
../common/o/cell.o : ../common/cell.c ../common/cell.h
$(CC) -o $# -c ../common/cell.c $(CFLAGS)
../common/o/utils.o : ../common/utils.c ../common/utils.h
$(CC) -o $# -c ../common/utils.c $(CFLAGS)
../common/o/verif.o : ../common/verif.c ../common/verif.h
$(CC) -o $# -c ../common/verif.c $(CFLAGS)
../common/o/lecture.o : ../common/lecture.c ../common/lecture.h
$(CC) -o $# -c ../common/lecture.c $(CFLAGS)
clean :
rm $(OBJECTS)
You could add
../common/o/utils.o : CFLAGS := $(filter-out -pedantic,$(CFLAGS))
to the makefile, which should suppress the warning for the specific file.
If you don't want to rely on non-standard libc extensions, you'll have to implement the functionality yourself...

Resources