IN THIS CHAPTER
. Programming in C with Linux
. Elements of the C/C++
Language
. Using the C Programming
Project Management Tools
Provided with Ubuntu
. Using the GNU C Compiler
. A Simple C Program
. Graphical Development Tools
. Reference
CHAPTER 30
C/C++ Programming
Tools for Ubuntu
Learning how to use a computer language such as C or
C++ can be more complex and difficult than learning shell
scripting. You must learn to think in terms of machine
resources and the way actions are accomplished within the
computer, taking into account the operating system,
language syntax, and assorted development tools, rather
than thinking in terms of user-oriented commands.
Of course, the main benefit of learning C or C++ is that
Linux and nearly all its related software is open source—if a
program doesn’t work the way you want it to, you can
change it! You can also find a lot of “hidden” documenta-
tion in programmer comments inside the source to nearly
all Linux programs.
This chapter focuses on C- and C++-related tools for Linux,
such as the GNU C Compiler Collection (
gcc) system, and
prototyping environments for GNOME and the K Desktop
Environment (KDE). The information presented here intro-
duces some of the commands used to compile C and C++
programs, along with information about related program-
ming tools.
Programming in C with Linux
C is the programming language most frequently associated
with UNIX-like operating systems such as Linux or BSD.
Since the 1970s, the bulk of the UNIX operating system
and its applications have been written in C. Because the C
language doesn’t directly rely on any specific hardware
architecture, UNIX was one of the first portable operating
systems. In other words, the majority of the code that
makes up UNIX does not know and doesn’t care which
computer it is actually running on. Machine-specific features are isolated in a few
modules within the UNIX kernel, which makes it easy for you to modify them when you
are porting to different hardware architectures.
C is a compiled language, which means that your C source code is first analyzed by the
preprocessor and then translated into assembly language first and then into machine
instructions that are appropriate to the target CPU. An assembler then creates a binary, or
object, file from the machine instructions. Finally, the object file is linked to any required
external software support by the linker. A C program is stored in a text file that ends with
a
.c extension and always contains at least one routine, or function, such as main(),
unless the file is an include file (with a
.h extension, also known as a header file) contain-
ing shared variable definitions or other data or declarations. Functions are the commands
that perform each step of the task that the C program was written to accomplish.
NOTE
The Linux kernel is mostly written in C, which is why Linux works with so many different
CPUs. To learn more about building the Linux kernel from source, see Chapter 35,
“Kernel and Module Management.
Programming in C++
C++ is an object-oriented extension to C. Because C++ is a superset of C, C++ compilers
compile C programs correctly, and it is possible to write non–object-oriented code in C++.
The reverse is not true: C compilers cannot compile C++ code.
C++ extends the capabilities of C by providing the necessary features for object-oriented
design and code. C++ also provides some features, such as the capability to associate func-
tions with data structures, that do not require the use of class-based object-oriented tech-
niques. For these reasons, the C++ language enables existing UNIX programs to migrate
toward the adoption of object orientation over time.
Support for C++ programming is provided by
gcc, which you run with the name g++
when you are compiling C++ code. KDE includes some intermediate C++ tools, such as
moc (the Meta Object Compiler) for use when building KDE applications.
Getting Started with Linux C/C++ Programming
Writing C programs for Linux requires that you understand C syntax, have some under-
standing of how to interface with the Linux kernel (by using one or more of 1,100 differ-
ent C functions, known as system calls), and know how to use Linux programming tools
such as
gcc and make. You’ll learn about each of these concepts and processes in this
chapter.
To get started, you need to make sure that you have installed all the necessary software
development packages (perhaps by using the
synaptic client; see Chapter 7, “Managing
Software”) to support your project. Installation of basic development software requires
CHAPTER 30 C/C++ Programming Tools for Ubuntu
694

Get Ubuntu Unleashed, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.