Configuring makefile to compile with debug information in linker phase - makefile

The Make file below is not compiling with debugging information.
Could anybody suggest what is wrong? I'm not a makefile expert.
I've tried adding the -g options. What else should I do?
This is related to this post - I can't use gdb in visual studio code?
Issue while debugging Linux based C++ code visual studio code
Thanks
EXEC = deserialize
WARNING_FLAGS := -pedantic-errors -Wall -Wextra -Werror -Wno-format-security
COMPILATION_FLAGS := -std=c++17 -fPIC -fconcepts -c -g
INCLUDES := -I../flatbuffers/include -I.
LIBS := -lstdc++
SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp, %.o, $(wildcard *.cpp))
STANDALONE_HEADERS :=
LIB := graph.so
LIB_OBJS := $(filter-out main.o,$(OBJS))
LIB_FLAGS := -shared
# .PHONY: all clean
%.o: %.cpp
gcc $^ $(INCLUDES) $(LIBS) $(COMPILATION_FLAGS) $(WARNING_FLAGS) \
-o $# $(STANDALONE_HEADERS) -g
$(LIB): $(LIB_OBJS)
gcc $(LIB_OBJS) $(LIBS) $(LIB_FLAGS) -g -o $#
$(EXEC): $(LIB) main.o
# gcc main.o $(LIB) $(LIBS) -o $(EXEC)
# sudo ldconfig
gcc main.o -L . -l:graph.so $(LIBS) -o $(EXEC) -g
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(PWD);
all: clean $(EXEC)
clean:
-rm *.o $(EXEC)
My task.json file in VS code is:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/project/main",
"args": [],
"stopAtEntry": true,
"cwd": "/project",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build tool"
}
]
}
The error in the VS code debugger is:
Stopped due to shared library event (no libraries added or removed)
Loaded '/lib64/ld-linux-x86-64.so.2'. Symbols loaded. [Inferior 1
(process 13729) exited with code 0177] The program
'/project/main'
has exited with code 177 (0x000000b1).

Related

RELEASE and DEBUG build in makefile

I am developing an application for STM32 in stm32cubeIDE
I am using the below custom makefile. Now i want to have a release and a debug build, but they both build the debug build.
I´ve tried
RELDIR = bin/release
DEBUGDIR = bin/debug
ifdef DEBUG
BIN=$(DEBUGDIR)
COMPILERFLAGS += -g3 -O0 -D_DEBUG
else
BIN=$(RELDIR)
COMPILERFLAGS += -O3
endif
and
debug : rebuild
BIN=$(DEBUGDIR)
COMPILERFLAGS += -g3 -O0 -D_DEBUG
release : rebuild
BIN=$(RELDIR)
COMPILERFLAGS += -O3
Can someone tell me how to achieve a release and a debug build :)
Full script:
RED = [31m
GREEN = [32m
YELLOW = [33m
BLUE = [34m
NC = [0m
BIN = bin
SRC = src
INC = inc
SOURCE = $(wildcard $(SRC)/*.c)
OBJECT = $(patsubst %,$(BIN)/%, $(notdir $(SOURCE:.c=.o)))
COMPILER=arm-none-eabi-gcc
ARCHITECTURE=cortex-m4
COMPILERFLAGS= -c -mcpu=$(ARCHITECTURE) -mthumb -mfloat-abi=soft -std=gnu11 -Wall -I $(INC)
LINKERFLAGS= -mcpu=$(ARCHITECTURE) -mthumb -mfloat-abi=soft --specs=nano.specs --specs=rdimon.specs -lc -lrdimon --specs=nosys.specs -T stm32_linker_script.ld -Wl,-Map=out.map
COMPILE= $(COMPILER) $(COMPILERFLAGS) -o $# $<
LINK= $(COMPILER) $(LINKERFLAGS) -o $# $^
RELDIR = bin/release
DEBUGDIR = bin/debug
ifdef DEBUG
BIN=$(DEBUGDIR)
COMPILERFLAGS += -g3 -O0 -D_DEBUG
else
BIN=$(RELDIR)
COMPILERFLAGS += -O3
endif
debug : rebuild
#BIN=$(DEBUGDIR)
#COMPILERFLAGS += -g3 -O0 -D_DEBUG
#
release : rebuild
#BIN=$(RELDIR)
#COMPILERFLAGS += -O3
.PHONY: rebuild
rebuild:
$(MAKE) clean
$(MAKE) all
all : out.elf
#echo "$(GREEN)Done!$(NC)"
out.elf : $(OBJECT)
#echo "$(YELLOW)Linking...$(NC)"
$(LINK)
$(BIN)/%.o : $(SRC)/%.c
$(COMPILE)
.PHONY: all clean debug prep release remake
clean:
#echo "$(GREEN)Cleaning...$(NC)"
rm -rf $(BIN)/*.o $(BIN)/*.elf
I ended up creating two makefiles, one for debug and one for release. It is kind of anoying to have duplicated code, but at least it is obvious what is going on in each script. The scripts are then called from the IDE via "make -f Makefile.DEBUG.mk rebuild" and "make -f Makefile.REBUILD.mk rebuild"
#RED = [31m
#GREEN = [32m
#YELLOW = [33m
BLUE = [34m
NC = [0m
BIN = bin/release
SRC = src
INC = inc
MAKE = make -f Makefile.RELEASE.mk
COMPILER=arm-none-eabi-gcc
ARCHITECTURE=cortex-m4
COMPILERFLAGS= -c -mcpu=$(ARCHITECTURE) \
-mthumb -mfloat-abi=soft -std=gnu11 -Wall -O3 -I $(INC)
LINKERFLAGS= -mcpu=$(ARCHITECTURE) \
-mthumb -mfloat-abi=soft \
--specs=nano.specs \
-T stm32_linker_script.ld \
-Wl,-Map=$(BIN)/out.map
COMPILE= $(COMPILER) $(COMPILERFLAGS) -o $# $<
LINK= $(COMPILER) $(LINKERFLAGS) -o $(BIN)/$# $^
SOURCE = $(wildcard $(SRC)/*.c)
OBJECT = $(patsubst %,$(BIN)/%, $(notdir $(SOURCE:.c=.o)))
.PHONY: rebuild
rebuild:
$(MAKE) clean
$(MAKE) all
all : out.elf
#echo "$(BLUE)Done!$(NC)"
out.elf : $(OBJECT)
#echo "$(BLUE)Linking...$(NC)"
$(LINK)
$(BIN)/%.o : $(SRC)/%.c
$(COMPILE)
.PHONY: all clean
clean:
#echo "$(BLUE)Cleaning...$(NC)"
rm -rf $(BIN)/*.o $(BIN)/*.elf $(BIN)/*.map
and
#RED = [31m
#GREEN = [32m
#YELLOW = [33m
BLUE = [34m
NC = [0m
BIN = bin/debug
SRC = src
INC = inc
MAKE = make -f Makefile.DEBUG.mk
COMPILER=arm-none-eabi-gcc
ARCHITECTURE=cortex-m4
COMPILERFLAGS= -c -mcpu=$(ARCHITECTURE) \
-mthumb -mfloat-abi=soft -std=gnu11 -Wall -g -I $(INC)
LINKERFLAGS= -mcpu=$(ARCHITECTURE) \
-mthumb -mfloat-abi=soft \
--specs=nano.specs \
--specs=rdimon.specs -lc -lrdimon \
--specs=nosys.specs \
-T stm32_linker_script.ld \
-Wl,-Map=$(BIN)/out.map
COMPILE= $(COMPILER) $(COMPILERFLAGS) -o $# $<
LINK= $(COMPILER) $(LINKERFLAGS) -o $(BIN)/$# $^
SOURCE = $(wildcard $(SRC)/*.c)
OBJECT = $(patsubst %,$(BIN)/%, $(notdir $(SOURCE:.c=.o)))
.PHONY: rebuild
rebuild:
$(MAKE) clean
$(MAKE) all
all : out.elf
#echo "$(BLUE)Done!$(NC)"
out.elf : $(OBJECT)
#echo "$(BLUE)Linking...$(NC)"
$(LINK)
$(BIN)/%.o : $(SRC)/%.c
$(COMPILE)
.PHONY: all clean
clean :
#echo "$(BLUE)Cleaning...$(NC)"
rm -rf $(BIN)/*.o $(BIN)/*.elf $(BIN)/*.map

modifying Makefile for silverfrost FTN95 ( Windows)

Compiling error
I am quite new to Fortran coding.
Currently, I have a makefile which has been previously used by original authors to compile the Fortran codes in Linux. However, I cannot understand what needs to be changed in the previous makefile to run it in Windows-based compiler Silverfrost FTN95.
While trying to compile in FTN95 I encountered the following error:
C:\Users\Geo-TGS\Documents\landslidemodel\src\TRIGRS>MAKE TPX
mpif90 -w -O3 -w -O3 -c ssizgrd.f95
process_begin: CreateProcess(NULL, mpif90 -w -O3 -w -O3 -c ssizgrd.f95, ...) failed.
make (e=2): The system cannot find the file specified.
makefile:59: recipe for target `ssizgrd.o' failed
MAKE: *** [ssizgrd.o] Error 2
C:\Users\Geo-TGS\Documents\landslidemodel\src\TRIGRS>make trg
mpif90 -w -O3 -w -O3 -c grids.f95
process_begin: CreateProcess(NULL, mpif90 -w -O3 -w -O3 -c grids.f95, ...) failed.
make (e=2): The system cannot find the file specified.
makefile:59: recipe for target `grids.o' failed
make: *** [grids.o] Error 2
Below is the Makefile code:
TRG = trg
PRG = prg
TPX = tpx
OBJT90 = trigrs.o flux.o prpijz.o svxmdv.o svijz.o dzero_brac.o
OBJT95 = grids.o input_vars.o model_vars.o dsimps.o input_file_defs.o iverson.o pstpi.o satfin.o savage.o steady.o trini.o unsinf.o ivestp.o pstpf.o rnoff.o satinf.o smallt.o svgstp.o unsfin.o unsth.o ssizgrd.o svlist.o
OBJT77 = calerf.o dbsct.o derfc.o irdgrd.o irdswm.o isvgrd.o roots.o srdgrd.o srdswm.o ssvgrd.o
OBJP90 = trigrs_p.o partial_p.o flux.o flux_p.o prpijz.o svxmdv.o svijz.o dzero_brac.o srdgrd_p.o irdgrd_p.o
OBJP95 = modules_p.o grids.o input_vars.o model_vars.o dsimps.o input_file_defs.o iverson.o pstpi.o pstpi_p.o satfin.o satfin_p.o savage.o steady.o trini.o trini_p.o unsinf.o unsinf_p.o ivestp.o ivestp_p.o pstpf.o pstpf_p.o rnoff.o satinf.o satinf_p.o smallt.o svgstp.o svgstp_p.o unsfin.o unsfin_p.o unsth.o unsth_p.o ssizgrd.o ssizgrd_p.o svlist.o rnoff_p.o steady_p.o
OBJP77 = calerf.o dbsct.o derfc.o irdgrd.o irdswm.o irdswm_p.o isvgrd.o roots.o srdgrd.o srdswm.o srdswm_p.o ssvgrd.o
OBJX90 = tpindx.o nxtcel.o
OBJX95 = ssizgrd.o
OBJX77 = isvgrd.o mpfldr.o rdflodir.o sindex.o slofac.o srdgrd1.o
LIBS =
CC = gcc -O3
CCFLAGS = -lgsl -lgslcblas -lm
FC = ftn95 -w -O3
FFLAGS =
F90 = f95 -w -O3
MPIF90 = mpif90 -w -O3
F90FLAGS = -w -O3
LDFLAGS = -w -O3
all: $(TRG) $(PRG)
#-----------------------------------------------------------------
$(TRG): $(OBJT95) $(OBJT90) $(OBJT77)
$(F90) $(CCLIBS) $(LDFLAGS) -o $# $(OBJT95) $(OBJT90) $(OBJT77) $(CCFLAGS) $(LIBS)
$(PRG): $(OBJP95) $(OBJP90) $(OBJP77)
$(MPIF90) $(CCLIBS) $(LDFLAGS) -o $# $(OBJP95) $(OBJP90) $(OBJP77)
$(CCFLAGS) $(LIBS)
$(TPX): $(OBJX95) $(OBJX90) $(OBJX77)
$(F90) $(CCLIBS) $(LDFLAGS) -o $# $(OBJX95) $(OBJX90) $(OBJX77) $(CCFLAGS) $(LIBS)
#-----------------------------------------------------------------
clean:
rm -f $(TRG) $(TPX) $(PRG)
rm -rf $(OBJT95) $(OBJT90) $(OBJT77) $(OBJP95) $(OBJP90) $(OBJP77)
rm -rf $(OBJX95) $(OBJX90) $(OBJX77)
rm -rf *.mod *.exe *.stackdump
.SUFFIXES: $(SUFFIXES) .f90 .f .c .f95
.f90.o:
$(MPIF90) $(F90FLAGS) -c $<
.f.o:
$(MPIF90) $(F90FLAGS) -c $<
.c.o:
$(CC) $(CCINCLUDE) -c -w $<
.f95.o:
$(MPIF90) $(F90FLAGS) -c $<
What changes need to be made for the code to compile?

Make error: commands commence before first target

Makefile:
CC = gcc
CFLAGS = -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_NO_XSLT=1 -DXMLSEC_NO_XKMS=1 -I/usr/include/libxml2 -DXMLSEC_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_CRYPTO=\"openssl\" -DUNIX_SOCKETS -DXML_SECURITY -DDEBUG
LDFLAGS= -lcrypto -I/usr/include/libxml2 -lxml2 -I/usr/local/include/xmlsec1 -lxmlsec1
$(CC) $(CFLAGS) $(LDFLAGS) src/aadhaar.c src/uid_auth.c -o AuthClient
I'm getting this error: error: commands commence before first target
You seem to miss the target definition:
AuthClient : src/aadhaar.c src/uid_auth.c
$(CC) $(CFLAGS) $(LDFLAGS) src/aadhaar.c src/uid_auth.c -o $#

make compiles some programs every time, even if they are just compiled

I have a Makefile below. Whenever I run make clean and then make everything is compiled again. But just after that if I run make again a subset of programs: convert_genomes, align_bs and methyl_extract are compiled again, which is strange since they have just compiled.
I have tested this in different operating systems, and the result is the same. Do you have any ideas?
EDIT
When I changed convert-genomes to convert_genomes (and similarly for the two other programs) the problem got solved, but I have still no idea what is the different between - and _ in Makefile that results in my problem
CC= gcc
CXX= g++
CFLAGS= -Wall -Wno-unused-function -O2
CXXFLAGS= -Wall -Wno-unused-function -O2
OBJS= QSufSort.o bwt_gen.o utils.o bwt.o bwtaln.o bwa2.o bwtgap.o sam.o hash.o smith.o aligner.o fa2bin.o \
is.o bntseq.o bwtindex.o ksw.o stdaln.o simple_dp.o \
bwaseqio.o bwase.o bwape.o kstring.o cs2nt.o \
bwtsw2_core.o bwtsw2_main.o bwtsw2_aux.o bwt_lite.o \
bwtsw2_chain.o bamlite.o bwtsw2_pair.o bwt2.o bwa.o
PROG= aryana
INCLUDES=
LIBS= -lm -lz -lpthread
SUBDIRS= .
debug: CFLAGS += -DDEBUG -g3 -O0
debug: CXXFLAGS += -DDEBUG -g3 -O0
.SUFFIXES:.c .o .cc
.c.o:
$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $#
.cc.o:
$(CXX) -c $(CXXFLAGS) $(INCLUDES) $< -o $#
all: $(PROG) aryana convert-genomes align-bs methyl-extract read_simul SamAnalyzer fastaseq bwtcheck SamToNormWig
debug: all
aryana:$(OBJS) aryana_main.o
$(CC) $(CFLAGS) $(OBJS) aryana_main.o -o aryana $(LIBS)
convert-genomes:
$(CXX) $(CXXFLAGS) convert_genomes.cpp -o convert_genomes
align-bs:
$(CXX) $(CXXFLAGS) align_bs.cpp -o align_bs
methyl-extract:
$(CXX) $(CXXFLAGS) methyl_extract.cpp -o methyl_extract
read_simul:
$(CXX) $(CXXFLAGS) read_simul.cpp -o read_simul
SamAnalyzer:
$(CXX) $(CXXFLAGS) SamAnalyzer.cpp -o SamAnalyzer
SamToNormWig:
$(CXX) $(CXXFLAGS) SamToNormWig.cpp -o SamToNormWig
fastaseq:
$(CXX) $(CXXFLAGS) fastaseq.cpp -o fastaseq
bwtcheck:
$(CXX) $(CXXFLAGS) bwtcheck.cpp -o bwtcheck
QSufSort.o:QSufSort.h
bwt.o:bwt.h
bwt2.o:bwt.h
bwtaln.o:bwt.h bwtaln.h kseq.h bwa2.h
bwt1away.o:bwt.h bwtaln.h
bwt2fmv.o:bwt.h
bntseq.o:bntseq.h
bwtgap.o:bwtgap.h bwtaln.h bwt.h
aligner.o: aligner.h bwt.h hash.h smith.h
fa2bin.o: fa2bin.h
hash.o: hash.h
bwa2.o: bwa2.h sam.h aligner.h bwt.h
sam.o: sam.h bwt.h
smith.o: smith.h bwt.h
bwtsw2_core.o:bwtsw2.h bwt.h bwt_lite.h stdaln.h
bwtsw2_aux.o:bwtsw2.h bwt.h bwt_lite.h stdaln.h
bwtsw2_main.o:bwtsw2.h
bwa.o: bntseq.h bwa.h bwt.h ksw.h utils.h kstring.h malloc_wrap.h kvec.h
aryana_main.o: aryana_main.h aryana_args.h bwt.h bwtaln.h kseq.h bwa2.h
clean:
rm -f gmon.out *.o a.out $(PROG) *~ *.a aryana align_bs methyl_extract convert_genomes read_simul SamAnalyzer bwtcheck fastaseq SamToNormWig
If your target is convert-genomes but the program is convert_genomes, then every time you try to make convert-genomes, the file convert-genomes does not exist, so it is out of date, and the rules to 'build it' are run once more, recreating convert_genomes instead of create-genomes.
make makes files. If you tell make 'this rule will make a file F' and actually the rule creates some other file G, then make will never find that F is up to date — as you carefully demonstrated, albeit by accident.

understanding Makefiles

I have the following make file:
CC = gcc
CCDEPMODE = depmode=gcc3
CFLAGS = -g -O2 -W -Wall -Wno-unused -Wno-multichar
COMPONENTHEADER = Q_OBJECT
CPP = gcc -E
CPPFLAGS = -I/usr/include/Inventor/annex -D_REENTRANT -I/usr/share/qt3/include
CXX = g++
CXXCPP = g++ -E
CXXDEPMODE = depmode=gcc3
CXXFLAGS = -g -O2 -fno-exceptions -W -Wall -Wno-unused -Wno-multichar -Woverloaded- virtual
CYGPATH_W = echo
GUI = QT
Gui = Qt
INCLUDES =
LIBS = -lSoQt -lqt-mt -lXmu -lXi -lCoin -lGL -lXext -lSM -lICE -lX11 -ldl -lpthread -lm -lcxcore -lcv -lhighgui -lcvaux
OBJS = MathTools.o PointCloud.o ExtractFeatures.o Tile.o Shape.o RoadDynamic.o
SRCS = MathTools.cpp PointCloud.cpp ExtractFeatures.cpp Tile.cpp Shape.cpp RoadDynamic.cpp main.cpp
HDRS = constants.h Shape.h MathTools.h PointCloud.h ExtractFeatures.h Tile.h RoadDynamic.h
WIDGET = QWidget *
all: main
main: main.o ${OBJS}
${CC} ${CFLAGS} ${INCLUDES} -o $# main.o ${OBJS} ${LIBS}
.c.o:
${CC} ${CFLAGS} ${INCLUDES} -c $<
depend:
makedepend ${SRCS}
clean:
rm *.o core *~
tar:
tar cf code.tar Makefile *.c *.h testfile1
print:
more Makefile $(HDRS) $(SRCS) | enscript -2r -p listing.ps
I am wondering why when I run make the output is
g++ -g -O2 -fno-exceptions -W -Wall -Wno-unused -Wno-multichar -Woverloaded-virtual -I/usr/include/Inventor/annex -D_REENTRANT -I/usr/share/qt4/include -c -o main.o main.cpp
instead of:
gcc -g -O2 -W -Wall -Wno-unused -Wno-multichar ...
it seems the cxx variables are overriding the cc variables. Why is that?
also what does the "include =" do in this case? It doesn't seem to be set to anything.
Thank you
Because your object files are apparently built from .cpp files. You have no explicit rule for building .o files from .cpp files, so Make uses the implicit rule $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c.

Resources