Skip to content
Physics and Astronomy
Home Our Teaching Resources Software Linuxcc
Back to top

Compiling and debugging under Linux

Contents

Linux distributions come with the GNU Compiler Collection (GCC), and the GNU development tools, the same as the Macs. The difference is which front-end you want to use. This being Linux, you have a choice!

Traditional setup

Traditionally, Unix/Linux programmers use a text editor such as Emacs or nedit to edit their program files and a program called make to handle compilation.

Compilation is handled by creating a control file, traditionally called 'Makefile', listing the source files, compiler options and the exact circumstances under which programs should be recompiled. Typing 'make' at the command line then recompiles all the files that have changed since the last compilation thus avoiding recompiling every single file every single time whether it needs it or not. You may also run make from within Emacs or nedit which has the advantage that the editor will warn you about any unsaved changes to your C files. Basically, the makefile is the equivalent of the 'project' in XCode or other Integrated Development Environments (IDEs).

We've created a simple example (basically the world's worst "Hello, world" program) for you to download.

  • Unpack the archive by typing: tar xvf make-example.tar

  • The things you may need to change are all in the first four uncommented lines in the Makefile (comments in makefiles start with '#'):
    # For a simple project you need to specify four things:
    # 1. The name you would like your executable to be called
    TARGET = myprog
    
    # 2. A list of your C source files
    CFILES = main.c file2.c
    
    # 3. The flags you wish to use to compile with
    CFLAGS = -O0 -g -Wall
    
    # 4. Libraries you need (-lm is the maths library)
    LIBS = -lm
    
    • CFLAGS are your compiler options. Here we have specified:
      • -O0 ( dash-OH-zero) turns off Optimisation to aid debugging; -O2 is a reasonable choice when you want your program to run faster.
      • -g tells the compiler to put information into the program to aid debugging. Keep this!
      • -Wall (Warn all) turns on warnings for things that are technically legal but probably a mistake. You should make sure your code compiles without warnings.
    • The rest of the file tells make to recompile every C file if the Makefile changes (so that if your change you compilation options they take effect immediately) and to create files filename.d and make.deps for reasons we won't go into at the moment.

Integrated development Environments

If Emacs/nedit and make are not to your taste and you prefer an IDE consider: Be warned, there is no support for Linux IDEs within the School.

Debuggers

[ You may wish to come back to this session after you have covered using the debugger in class. ]

As mentioned above, Linux and Mac OS X share the same debugger (gdb). On the Mac, gdb has a user-friendly front-end inside XCode. On Linux you can run gdb from inside of Emacs (they were originally written by the same person) but it is probably easier to use one of the available front-ends:

Which you use will probably depend on which is installed on your machine. Whichever you chose, learn the basics of seeing the values of variables and setting breakpoints and watchpoints. It's usually a combination of an item from a menu called "View" and right-clicking on a code statement or variable displayed in a window although some front-ends require you to do it within a specific window.
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top