Makefile 使用gdb进行调试-正确地对多个文件使用-g标志

Makefile 使用gdb进行调试-正确地对多个文件使用-g标志,makefile,gdb,Makefile,Gdb,我试图使用gdb通过emacs运行它进行调试,但没有成功。我不断得到众所周知的“未找到调试符号”。我假设问题出在我使用-g标志编译的方式上,但是我不知道我的错误在哪里。以下是生成文件: all: tree tree: main.o OctTree.o my_vec.o runge_kutta.o initialize.o global.o g++ -g -o tree main.o OctTree.o my_vec.o runge_kutta.o initialize.o global

我试图使用gdb通过emacs运行它进行调试,但没有成功。我不断得到众所周知的“未找到调试符号”。我假设问题出在我使用-g标志编译的方式上,但是我不知道我的错误在哪里。以下是生成文件:

all: tree

tree: main.o OctTree.o my_vec.o runge_kutta.o initialize.o global.o
    g++ -g -o tree main.o OctTree.o my_vec.o runge_kutta.o initialize.o global.o -lm

main.o: main.cpp OctTree.h my_vec.h global.h
    g++ -g -o3 main.cpp -o main.o 

my_vec.o: my_vec.cpp my_vec.h
    g++ -g -o3 my_vec.cpp -o my_vec.o

OctTree.o: OctTree.cpp OctTree.h my_vec.h global.h
    g++ -g -o3 OctTree.cpp -o OctTree.o

runge_kutta.o: runge_kutta.cpp OctTree.h my_vec.h global.h
    g++ -g -o3 runge_kutta.cpp -o runge_kutta.o

initialize.o: initialize.cpp my_vec.h OctTree.h global.h
    g++ -g -o3 initialize.cpp -o initialize.o

global.o : global.cpp global.h
    g++ -g -o3 global.cpp -o global.o

clean: 
    rm main.o my_vec.o OctTree.o runge_kutta.o initialize.o global.o-f
当我尝试运行gdb时,我收到以下消息: 正在使用计算机/final/tree从/home/alexander/physics读取符号…(未找到调试符号)…完成


多谢各位

您最好利用make的功能,使您的make文件更易于更改。例如:

# all cpp files (add files here if you add more to your project)
SRC_FILES = main.cpp my_vec.cpp OctTree.cpp runge_kutta.cpp initialize.cpp global.cpp
OBJ_FILES = $(addsuffix .o,$(basename $(SRC_FILES))) # autogenerated

# flags, options, and other crap
OUTPUT = tree
CXX_FLAGS = -c -g # -O3 # disable optimization for now
CXX_FLAGS_LINK = -g
LIBS = -lm

# first target
all: tree

# tree target: requires all object files, links them into output executable
tree: $(OBJ_FILES)
    g++ $(CXX_FLAGS_LINK) -o $(OUTPUT) $(OBJFILES) $(LIBS)

# generic target for any cpp file
%.o: %.cpp
    g++ $(CXX_FLAGS) -o %.o %.cpp

# clean target
clean: 
    rm main.o my_vec.o OctTree.o runge_kutta.o initialize.o global.o -f

# specific dependencies for each file
# you can generate these with g++ -MM <filename.cpp>
main.o: main.cpp OctTree.h my_vec.h global.h
my_vec.o: my_vec.cpp my_vec.h
OctTree.o: OctTree.cpp OctTree.h my_vec.h global.h
runge_kutta.o: runge_kutta.cpp OctTree.h my_vec.h global.h
initialize.o: initialize.cpp my_vec.h OctTree.h global.h
global.o : global.cpp global.h
 cd /home/alexander/physics/computer/final
 readelf -wl tree | grep 'runge_kutta\.cpp'
#所有cpp文件(如果向项目添加更多文件,请在此处添加文件)
SRC_FILES=main.cpp my_vec.cpp OctTree.cpp runge_kutta.cpp initialize.cpp global.cpp
OBJ_文件=$(addsuffix.o,$(basename$(SRC_文件)))#自动生成
#旗帜、选项和其他垃圾
输出=树
CXX#u FLAGS=-c-g#-O3#暂时禁用优化
CXX_标志_链接=-g
LIBS=-lm
#第一目标
所有:树
#树目标:需要所有对象文件,将它们链接到输出可执行文件中
树:$(OBJ_文件)
g++$(CXX\u标志\u链接)-o$(输出)$(OBJFILES)$(LIBS)
#任何cpp文件的通用目标
%.o:%.cpp
g++$(CXX_标志)-o%.o%.cpp
#清洁目标
清洁:
rm main.o my_vec.o OctTree.o runge_kutta.o initialize.o global.o-f
#每个文件的特定依赖项
#您可以使用g++-MM生成这些
main.o:main.cpp OctTree.h my_vec.h global.h
my_vec.o:my_vec.cpp my_vec.h
o:OctTree.cpp OctTree.h my_vec.h global.h
runge_kutta.o:runge_kutta.cpp OctTree.h my_vec.h global.h
initialize.o:initialize.cpp my_vec.h OctTree.h global.h
global.o:global.cpp global.h
我怀疑您遇到的问题与编译器将在O3时提高代码效率的长度有关。在没有优化的情况下再试一次,看看这是否适合你

我根本没有测试过这个makefile

编辑: 好的,您已经尝试禁用优化。您已经尝试过清理
并在关闭优化的情况下重新编译了吗?如何启动调试器?关闭优化,即使GDB现在有不同的抱怨,它也不能很好地进行调试。你忘了重建你的资源了吗

当我试着运行gdb时,我得到这样一条消息:从/home/alexander/physics/computer/final/tree读取符号…(未找到调试符号)…完成

很明显,您的构建实际上包含调试信息。因此,必须是您调试的不是您构建的东西(树的其他副本)

你应该

  • 确认二进制文件具有调试信息。例如:

    # all cpp files (add files here if you add more to your project)
    SRC_FILES = main.cpp my_vec.cpp OctTree.cpp runge_kutta.cpp initialize.cpp global.cpp
    OBJ_FILES = $(addsuffix .o,$(basename $(SRC_FILES))) # autogenerated
    
    # flags, options, and other crap
    OUTPUT = tree
    CXX_FLAGS = -c -g # -O3 # disable optimization for now
    CXX_FLAGS_LINK = -g
    LIBS = -lm
    
    # first target
    all: tree
    
    # tree target: requires all object files, links them into output executable
    tree: $(OBJ_FILES)
        g++ $(CXX_FLAGS_LINK) -o $(OUTPUT) $(OBJFILES) $(LIBS)
    
    # generic target for any cpp file
    %.o: %.cpp
        g++ $(CXX_FLAGS) -o %.o %.cpp
    
    # clean target
    clean: 
        rm main.o my_vec.o OctTree.o runge_kutta.o initialize.o global.o -f
    
    # specific dependencies for each file
    # you can generate these with g++ -MM <filename.cpp>
    main.o: main.cpp OctTree.h my_vec.h global.h
    my_vec.o: my_vec.cpp my_vec.h
    OctTree.o: OctTree.cpp OctTree.h my_vec.h global.h
    runge_kutta.o: runge_kutta.cpp OctTree.h my_vec.h global.h
    initialize.o: initialize.cpp my_vec.h OctTree.h global.h
    global.o : global.cpp global.h
    
     cd /home/alexander/physics/computer/final
     readelf -wl tree | grep 'runge_kutta\.cpp'
    
    应该会产生一些产出。及

     readelf -w tree
    
    应该会产生大量的产出

  • 确认您可以在emacs之外调试此二进制文件:

    gdb ./tree
    
    这不应产生
    未找到调试符号

  • 现在在emacs中调试此二进制文件:

    M-x gdb
    
    完成命令成为
    gdb/home/alexander/physics/computer/final/tree

    如果步骤2起作用,则步骤3也不会生成
    未找到调试符号


  • 如果您使用更具动态性的makefile,这会有所帮助。另外,禁用优化。我尝试禁用优化,但没有帮助。你说的“更动态的生成文件”是什么意思?我用一个例子给出了答案。你是如何运行调试器的?谢谢,但它仍然不能工作。我正在通过emacs与gdb共进午餐,这是我得到的第一条评论。我是emacs和gdb的新手,所以我不知道你所说的“保持清洁”是什么意思。你的意思是我应该在文件末尾的“clean”部分添加“$(MAKE)clean”?如果是这样,它也不起作用。