Search code examples
c++debuggingmakefilestatic-librariesvscode-debugger

How to debug a complex project source code which generates a static library?


I am dealing with a complex C++ project. (Simulation Environment). I have a source code which has multiple services and apps inside. The project mostly written in C++. I want to make changes on simulation services and debug them with gdb through vscode.

I have an executable which uses a static library libtrick.a and I want to debug a .cpp service where this library is generated from. This executable is made from 2 main pieces one from sim_services(the lib) and model source code(.cpp code which is your physics algorithms).

Here is a snippet of a section in the Makefile showing contents of the static lib:

#-------------------------------------------------------------------------------
# Specify the contents of: libtrick.a
#-------------------------------------------------------------------------------
TRICK_LIB = $(TRICK_LIB_DIR)/libtrick.a
SIM_SERV_DIRS = \
    ${TRICK_HOME}/trick_source/sim_services/ExternalApplications \
    ${TRICK_HOME}/trick_source/sim_services/Clock \
    ${TRICK_HOME}/trick_source/sim_services/CheckPointAgent \
    ${TRICK_HOME}/trick_source/sim_services/CheckPointRestart \
    ${TRICK_HOME}/trick_source/sim_services/Collect \ 
    .
    .
    .
    etc.

and

Here is the section about building libtrick.a and compiling object files.

#-------------------------------------------------------------------------------
# 1.1 Build Trick-core
no_dp: $(TRICK_LIB) $(TRICK_SWIG_LIB)
    @ echo ; echo "Trick libs compiled:" ; date

# 1.1.1 Build libTrick.a
$(TRICK_LIB): $(SIM_SERV_DIRS) $(UTILS_DIRS) | $(TRICK_LIB_DIR)
    ar crs $@ $(SIM_SERV_OBJS) $(UTILS_OBJS)

ifeq ($(USE_ER7_UTILS), 1)
ER7_UTILS_LIB = $(TRICK_LIB_DIR)/liber7_utils.a
no_dp: $(ER7_UTILS_LIB)

$(ER7_UTILS_LIB): $(ER7_UTILS_DIRS) | $(TRICK_LIB_DIR)
    ar crs $@ $(ER7_UTILS_OBJS)
endif

# 1.1.1.1 Compile the objects in the specified sim_services directories.
.PHONY: $(SIM_SERV_DIRS) 
$(SIM_SERV_DIRS): icg_sim_serv $(TRICK_LIB_DIR)
    @ $(MAKE) -C $@ trick

# 1.1.1.2 Compile the objects in the specified utils directories.
.PHONY: $(UTILS_DIRS)
$(UTILS_DIRS): icg_sim_serv
    @ $(MAKE) -C $@ trick

# 1.1.1.3 Compile the objects in the specified er7_utils directories.
.PHONY: $(ER7_UTILS_DIRS)
$(ER7_UTILS_DIRS): TRICK_CXXFLAGS += -Wno-unused-parameter
$(ER7_UTILS_DIRS): icg_sim_serv
    @ $(MAKE) -C $@ trick

My question is How am I supposed to debug the library's codes(sim_services) on vscode?

UPDATE: Solution; I mostly understand the concept about how to debug it through out the comments and the answers given here. Following their lead I have created a smaller project(helloWorld) which has the same scenario and I successfully make both source and static library's codes debuggable through vscode. I have added this line CXXFLAGS = -std=c++11 -g (as mentioned in the answers) to my helloWorld project's makefile and it solved debugging issue. Also adding this to the nasa/trick project is solved my debugging problem.

At this point my question about debugging a static library's codes has resolved.


References

You can see the source code here for further understanding of the project structure: https://github.com/nasa/trick

Note : I am new to asking questions so if I have mistakes on the question format please correct me and I will try to learn and fix.


Solution

  • How to debug a source code which generates a static library?

    First of all: You debug a executable and not a library nor the source code. The debugger itself attaches to the executable!

    I have an executable which uses a static library libtrick.a and I want to debug a .cpp service where this library is generated from.

    No, you debug the executable which was compiled from your source code. In case of static libraries it is the same as build directly from the source code. A static library is only a special file format which collects one or more object files.

    Your debugger needs the information which source file was used to generate the object files, independent if it was build directly from source or via the static library. To get this information, you need to compile with the debug option enabled -g, also for the files which you collect in your static library.

    If you now start your executable in the debug environment, you automatically will be pointed to the source which was used to create your executable. Also all symbols are available!

    Hint: Take care that you don't strip your object files before link them in the static library. Some build scripts are doing this by default! No idea if you use a hand crafted Makefile or some IDE generated bunch of obscurities :-)

    The projects makefile is not allowing me to use -g on its make all command but i will try to add -g when compiling .o files of services

    Most of the pre-generated Makefiles have the option to add additional flags via environment variable CXXFLAGS You may check out if you can simply set CXXFLAGS before compile the source. Maybe you can share the needed parts of your Makefile here to get some advice how to deal with it.