Programming in C with Linux

 < Day Day Up > 

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.

NOTE

In 1983, the American National Standards Institute (ANSI) established a committee to standardize the definition of C. The resulting standard is known as ANSI C, and it is the recognized standard for the language, the grammar, and a core set of libraries. The syntax is slightly different from that of the original C language, which is frequently called K&R, for Kernighan and Ritchie. There is also an International Standards Organization (ISO) C standard that is very similar to the ANSI C standard.


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) containing 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 38, "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.

Object-oriented languages such as C++ allow programmers to define new abstract data types (including associated functions) and then derive subsequent data types from them. The notion of a new class of data objects, in addition to the built-in classes such as integer, floating-point number, and character, goes beyond the familiar capability to define complex data objects in C.

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 functions with data structures, that do not require the use of class-based object-oriented techniques. For these reasons, the C++ language enables existing Unix programs to migrate toward the adoption of object orientation over time.

Support for C++ programming using Fedora 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.

NOTE

You can read the C++ Programming HOWTO, available at http://www.tldp.org, for more information on programming using C++ and Linux.


Getting Started with Linux C/C++ Programming

Linux is popular with C and C++ programmers because of gcc and its suite of related software-development tools, such as gdb, autoconf, and make. Complex C programs might have dozens or hundreds of separate object files that are created during the build process. One, more, or all these (compiled) steps can be carried out automatically by the gcc compiler system, which is run by the make utility according to project rules contained in a makefile. After a program is compiled, it can be executed over and over without recompilation.

Writing C programs for Linux requires that you understand C syntax, have some understanding of how to interface with the Linux kernel (by using one or more of 1,100 different 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 system-config-packages client; see Chapter 7, "Managing Software and System Resources") to support your project. Installation of basic development software requires more than 50 different software packages and 110MB of hard drive space. If you plan to build programs for the Linux kernel, you need the Kernel development packages and an additional 180MB of storage space (not counting temporary disk space required when building a new kernel).

Developing programs for the X Window System requires installation of 20 software packages and about 40MB of software libraries, programming support files, and documentation. If you want to build GNOME clients, you need more than 60MB of additional space for nearly 50 software packages. And if you intend to develop programs for KDE, you should plan on reserving 220MB or so of your hard drive to store KDE's development software, contained in 20 different packages. Don't forget that you will also need lots of space for temporary files when you're building programs, as many intermediate files will be created during your programming sessions.

You should also become familiar with a good text editor. Although GNU purists use the emacs editing environment to write, edit, and compile programs, many other developers do just fine with an editor such as vi or nano for editing source and then use the command line to run development tools.

When programming for GNOME or KDE, you might find it most convenient to use a graphical development project manager, such as GNOME's Glade (the GTK+ User Interface Builder, glade-2), TRolltech's Qt Designer (designer), or KDE's KDevelop (the KDE development environment, kdevelop). These clients allow you to prototype an application with graphical drawing tools, which frees you to concentrate on your client's interface. You can use either to automatically generate all necessary project and source code files for skeletal applications. See the section "Graphical Development Tools," later in this chapter, for more information.

C Documentation

You will learn some of the basics of working in C in this chapter, and you also have a wealth of other information about C available to you on your Fedora Core system. You can use the man command to read online manual pages for the gcc C compiler and installed programming tools. You can use the info command to read the online GNU info files. You can get a general overview of how to use gcc by using the man command like this:

 $ man gcc 

For detailed information about using gcc, you can use the info command like this:

 $ info gcc 

Linux kernel and system calls are documented in manual pages in section 2, and manual pages for general Linux programming and helpful C routines (known as library routines) are found in section 3, known as the Linux Programmer's Manual. Linux programs use a number of support libraries, but the main routines are documented in the manual pages. For example, to see how to use the printf() function, you could use the man command like this:

 $ man 3 printf  

You can also gain additional insight into Linux programming by reading Linux Programmer's Guide, which is somewhat dated, but still useful, and is available at http://www.tldp.org/LDP/lpg/index.html. Or you can examine the source code for Linux programs. Later in this chapter you'll be introduced to the basics of using command-line development software and graphical program development tools for Linux.


The Process of Programming

Writing and building C programs when using Fedora Core Linux can be as easy as following a few simple steps, using several software tools. The basic steps are

1.

Run a text editor, such as vi or a graphical programmer's editor, such as KDE's kate.

2.

Type in C source code.

3.

Save the changes.

4.

Compile the program using the gcc compiler.

5.

Run and test (debug) the program.

6.

Edit the source code to implement new features or fix problems.

On the other hand, building a large and complex application can involve the efforts of many programmers, and require installing, configuring, and maintaining a source code control system (such as CVS, discussed later in this chapter), tracking program changes, developing software manuals, and using more complex software tools in the development process (such as autoconf or the gdb debugger).

Many Linux C programmers start by writing a program that solves an immediate need, such as a simple command-line program to download or upload phone numbers or other data to a cellular phone or handheld computer. Many beginners get started by becoming proficient in using a Linux text editor, researching any required software routines and reading documentation, browsing available Linux source code, learning how to run a compiler (such as gcc, discussed later in this chapter), and then launching into the iterative process of editing, compiling, executing, and testing.

The following sections in this chapter discuss some of the required or helpful software tools included with Fedora Core. You also see how to create, compile, and run a simple C program. You'll also see how to use several graphical prototyping tools (such as Glade, Designer, or KDevelop) to shorten the process of developing a user interface for graphical programs; these tools can free up time and effort on those tasks, allowing you to concentrate on the core functions of your new program.

     < Day Day Up > 


    Red Hat Fedora 4 Unleashed
    Red Hat Fedora 4 Unleashed
    ISBN: 0672327929
    EAN: 2147483647
    Year: 2006
    Pages: 361

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net