Question
Please help on Ubuntu/terminal in c Program Development Process The typical program development cycle consists of Edit/Compile/Run/Debug sessions that repeat until the program functions correctly.
Please help on Ubuntu/terminal in c
Program Development Process
The typical program development cycle consists of Edit/Compile/Run/Debug sessions that repeat until the program functions correctly.
Edit - there are several built-in editors available on the system. For example, vim (Vi IMproved) is an improved version of the venerable vi editor - arcane but available on every UNIX system. Likewise with the emacs text editor. The user interface also includes several simple, graphical text editors (e.g. Kate, Puma, gedit).
Compile - use either the GNU C compiler (gcc) or the GNU C++ compiler (g++). Check out the man pages for useful command line options. Always compile with full warnings (i.e. -Wall). You may also need to link in specific libraries if you use functions included in them (e.g. use -lm to link in the math library). One of the biggest differences between C and C++ or Java is the C Standard I/O library, so you should also do a man on printf() for output and scanf() for input to understand C-style I/O (most sample programs presented in the lab will be written in C). Note: several "MS Visual Studio"-like development environments are also available (e.g. Eclipse with CDT).
Run - unless specified otherwise by the "-o" (output) switch, a successful compilation will produce a file called a.out, which is your executable. Simply type the program name to execute the program. Depending on how your path is configured, you may need to path-qualify the name, as in "./a.out".
Debug - the EOS systems have the GNU debugger, gdb. Debuggers can be very useful, not only to determine the source of problems in a program but also to understand program execution better. Remember that you need to use the -g command line option when compiling in order to enable debugging. Note: the Data Display Debugger (ddd) debugger is a graphical version of the gdb debugger.
You can use the following shortcuts in gdb or type help to find out how to:
b - set a breakpoint (your program stops at the specified program line or function name) n - step to the next line in a program (steps over function calls) s - step to the next line in a program (steps into function calls) p - display (print) the value of a variable w - display (watch) the value of a variable whenever it changes bt - display (backtrace) the current program execution stack
Program Debugging
In order to find the exact location of a problem (i.e. a bug) in a program, it is often necessary to use a debugger. Debuggers can also be used to verify logic, by checking intermediate or temporary values of variables. Every UNIX installation includes the gdb debugger; this section includes a short exercise in using it.
Sample Program 1
#include
int main() { double num = 0.0; printf ("Hello, world. "); num = pow(2, 28); printf ("You are the %f person to write this program! ", num); return 0; }
6. Perform the following operations:
- create the above sample program (via copy/paste)
- start a script session
- compile the program (remember to include debugging information and link all necessary libraries)
- run the sample program
- start the debugger on your program (gdb a.out).
- set a breakpoint at the function main
- run your program within the debugger (run) and step through it
- use the debugger to print the value of num before and after it changes
- done - quit debugger and print/submit the script
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started