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
    ${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 \ 


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

# 1.1 Build Trick-core
    @ echo ; echo "Trick libs compiled:" ; date

# 1.1.1 Build libTrick.a
    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)

    ar crs $@ $(ER7_UTILS_OBJS)

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

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

# Compile the objects in the specified er7_utils directories.
$(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.


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.


  • 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.