To do debug for C++ codes with cmake
, I have a trick to add the following lines before the project (myProjectYY)
line of the CMakeLists.txt
file in the root directory of the source code.
set(CMAKE_BUILD_TYPE "Debug")set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
So, after cmake
and make
, I have obtained the executable file mainYY
, and I can simply run gdb mainYY
to do the debug as I should be able to see the source codes of mainYY.cpp
.
I have 2 separated projects that were already cmake
and make
by the others, namely a simple project (i.e. myProject1
) and a complicated project (i.e. myProject2
). For myProject1
, the above trick works for me, i.e. after I added the above 3 lines and re-do the cmake
and make
again to obtain main1
, I can see the source code of main1.cpp
by simply executing l
in gdb
.
But for myProject2
, I do same, i.e. I added the above 3 lines and re-do the cmake
and make
again to obtain main2
, but in gdb
there is no source code for main2.cpp
. That is, gdb main2
firstly gave me Reading symbols from main2...(no debugging symbols found)...done
. And then if I run l
in gdb
, I have No symbol table is loaded. Use the "file" command
.
What are the most probable reasons for such differences between myProject1
and myProject2
? How can I find out those probable reasons, and how can I do the fix such that I can debug for myProject2
?
Thanks.