2

I am learning linux, and my first step is to adapt my project for running on linux. Here is simple makefile (in educational purposes mostly), which generates out file:

#------------------------BUILD VARIABLES-----------------------------
#   Directories, containing headers
INCLUDE_DIR = ../Include/
#   Output directory which will contain output compiled file
OUTPUT_DIR = ../Bin/Debug/

SOURCES = EngineManager.cpp Geometry.cpp Main.cpp Model.cpp \
      Shaders.cpp TGAImage.cpp 

HEADERS = EngineManager.h Geometry.h Line.h Model.h Shaders.h \
      TGAImage.h Triangle.h

#------------------------BUILD_RULES---------------------------------
TinyRenderBuilding : $(addprefix $(INCLUDE_DIR), $(HEADERS)) $(SOURCES)
    mkdir -p  $(OUTPUT_DIR)
    g++ -std=c++14 -o $(OUTPUT_DIR)TinyRender.out -g -I$(INCLUDE_DIR) $(SOURCES)

I cannot understand, why does g++ not generate debug symbols? -g option is presented

Alex Aparin
  • 4,393
  • 5
  • 25
  • 51
  • Try using -ggdb flag instead of just -g. – Sv Sv Nov 05 '17 at 10:10
  • How are you sure that debug information in [DWARF](https://en.wikipedia.org/wiki/DWARF) format is not generated? What is your [GCC](http://gcc.gnu.org/) version? Try using `g++ -v`. **Edit your question** to improve it. – Basile Starynkevitch Nov 05 '17 at 10:13
  • Did you remove all object files before build (try `make clean`) ? – Basile Starynkevitch Nov 05 '17 at 10:14
  • @BasileStarynkevitch, no I don't use make clean. I checked debug symbols in following way - just sarched pdb files (in WINDOWS analogue) – Alex Aparin Nov 05 '17 at 10:16
  • Your `Makefile` in github is very incomplete. Read about [make](https://www.gnu.org/software/make/manual/html_node/index.html) (so spend a few hours reading that). No need to put the object files in some other directory. See [this](https://stackoverflow.com/a/14180540/841108) – Basile Starynkevitch Nov 05 '17 at 10:16
  • @BasileStarynkevitch, it seems like `-g` works, As I understand pdb information is embedded into `.out` file? (this behaviour is different from Windows) – Alex Aparin Nov 05 '17 at 10:17
  • 2
    Linux is not Windows, sorry about that. You need to learn more. Don't adapt your Windows build to Linux, but write a fresh `Makefile` from scratch, perhaps with a simpler source directory organization. Having `Source` and `Includes` subdirectories is just confusing you. Consider, for a small project like yours, a flat source file tree. Look into other free software projects for inspiration. – Basile Starynkevitch Nov 05 '17 at 10:19
  • Of course, once you've got a big project (of nearly a million lines of C++ code) you'll organize it more wisely and you'll spend days to improve its build. But you are not there yet (you'll need many years to reach such a project size). – Basile Starynkevitch Nov 05 '17 at 10:24
  • 1
    Linux don't use any `pdb` file (and I never heard of that). The DWARF debug information is often inside the `.o` object files (there is some weird way to get it elsewhere, I never did that) so inside your ELF executable. – Basile Starynkevitch Nov 05 '17 at 10:28
  • @BasileStarynkevitch, Yes it is likely so. I repeated process, and gdb tells that debug information is loaded. It is very unusual for me. I need learn more about linux – Alex Aparin Nov 05 '17 at 10:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158253/discussion-between-basile-starynkevitch-and-lmtinytoon). – Basile Starynkevitch Nov 05 '17 at 10:31

3 Answers3

4

To include debug symbols when compiling with g++ you need to pass the -g option.

In a make make file this usually means adding it to to CXXFLAGS.

Also make sure you pass the -g option when you create the executable: when you compile you turn .cpp files into .o files, when you do the linking you turn those .o files into your executable).

If you change the options before running make again be sure to run a make clean cause otherwise it won't get recompiled.

Finally, make sure that you do not have additional steps like strips command run on the executable (which would remove debugging symbols).

you can use

objdump --syms <executable-file>

to check if an executable have symbols.

when it doesn't have symbols it will say something like:

SYMBOL TABLE:
no symbols

(I'm no experto of C / C++ programming, I just run into this while I was trying to debug someone else code)

Daniele Segato
  • 12,314
  • 6
  • 62
  • 88
2

I'm not entirely sure, but you can try -g or -ggdb.You can do some research on these. We were using these parameters to debug the C program with the gdb tool.

Hakan B.
  • 50
  • 2
2

According to your makefile g++ should produce debug symbols (-g option is presented). To confirm this you can run file on resulting binary:

$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=9fe588c18099ef418daf288931bb033cc287922e, with debug_info, not stripped

(Note with debug_info string in output)

ks1322
  • 33,961
  • 14
  • 109
  • 164