1

I have two source files that I'm currently compiling into one executable.

I use gcc -o ProgramName file1.c file2.c

I know gdb requires the -g flag when being compiled but I must be placing it incorrectly. I have tried several things but nothing along the lines of:

gcc -g -o ProgramName file1.c file2.c is working for me. When I run gdb -> run it says that no executable was found.

How do I compile this correctly so it will run in GDB?

5
  • 1
    You did start or load the program in GDB? Commented Sep 1, 2017 at 16:34
  • I'm not sure what you're asking? I did exactly what I described before and that's all I can say for sure. Commented Sep 1, 2017 at 16:37
  • 2
    start it like gdb ./ProgramName -> run. GDB requires an executable to run it Commented Sep 1, 2017 at 16:38
  • install eclipse, it will take this boring part. And will visualise the gdb output in the more convenient way. Command line gdb is the last resort Commented Sep 1, 2017 at 16:39
  • I appreciate the idea but I am quite familiar with NetBeans already. I'm being forced to use command line gdb for a class. Commented Sep 1, 2017 at 16:57

3 Answers 3

3

The proper command is

gdb ProgramName

followed by

run

in the prompt.

Sign up to request clarification or add additional context in comments.

4 Comments

I think the ./ is redundant. gdb should find ProgramName in the current directory. This is unlike the shell, which will only search the PATH unless the name contains a /.
@IanAbbott that's true, and fixed but I just realized that gdb seems to use path too :D
Yes, I'm not sure whether it checks the current directory first or the PATH first, but it should be easy to test.
@IanAbbott at least mine is current directory first (ln -s /bin/ls; gdb ls)
1

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

Comments

0

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

Now run the gdb file.exe

For further reference: Please go through: http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.