简体   繁体   English

gcc如何编译多个源文件以在GDB中使用?

[英]How can gcc compile multiple source files for use in GDB?

I have two source files that I'm currently compiling into one executable. 我有两个当前正在编译为一个可执行文件的源文件。

I use gcc -o ProgramName file1.c file2.c 我使用gcc -o ProgramName file1.c file2.c

I know gdb requires the -g flag when being compiled but I must be placing it incorrectly. 我知道gdb在编译时需要-g标志,但是我必须放置不正确。 I have tried several things but nothing along the lines of: 我已经尝试了几件事,但没有采取以下措施:

gcc -g -o ProgramName file1.c file2.c is working for me. gcc -g -o ProgramName file1.c file2.c正在为我工​​作。 When I run gdb -> run it says that no executable was found. 当我运行gdb > run它说找不到可执行文件。

How do I compile this correctly so it will run in GDB? 我如何正确地编译它以便它可以在GDB中运行?

The proper command is 正确的命令是

gdb ProgramName

followed by 其次是

run

in the prompt. 在提示中。

Try with: 尝试:

gdb -tui ProgramName 

This will open the GDB with the Text User Interface , which by default will load the source code in your screen. 这将使用文本用户界面打开GDB,默认情况下,它将在屏幕上加载源代码。 In this way you will know if the program was loaded. 这样,您将知道程序是否已加载。

The screen will be split, with the source code in the top and the command line in the bottom, like this: 屏幕将被拆分,源代码在顶部,命令行在底部,如下所示:

gdb-tui

Please compile the individual source files(cxx,c) using gcc -g -c file1.c file2.c (so on) This will generate the file1.o file2.o files (so on ...) now compile the object files into a formal executable. 请使用gcc -g -c file1.c file2.c (依此类推)编译单个源文件(cxx,c),这将生成file1.o file2.o文件(依此类推...),现在编译目标文件成正式的可执行文件。 gcc -g -o file.exe file1.o file2.o

On similar lines for c++ g++ -g -c file1.c file2.c g++ -g -o file.exe file1.o file2.o 在与c ++类似的行上g++ -g -c file1.c file2.c g++ -g -o file.exe file1.o file2.o

Now run the gdb file.exe 现在运行gdb file.exe

For further reference: Please go through: http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.7 有关更多参考:请访问: http : //www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM