c_in_linux

Upload: rams

Post on 09-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 c_in_linux

    1/4

    HowTo build simple C programs on Linux

    1. Introduction

    This document will step you though compiling and running simple C programs underthe Linux operating system. It is meant as a guide for beginners who may know how

    to program in C but don't know how to build a C program on Linux. This documentuses simple C programs to illustrate how to build the programs under Linux. It

    covers writing a basic C program but it is not meant as a guide to teach the

    language. We will also step through the basics of writing a Makefile for your project.

    This document also assumes that you know how to create/edit files on Linux and thatyou have the GNU C compiler installed. An easy way to tell if you have a C compiler

    installed is by issuing the command 'which gcc'. We are also assuming that thecompile is executed from the command line. There are far too many GUI/IDE type

    environments to cover otherwise.

    2. A Simple program

    2.1 Writing the source

    The source of our first program is below:code:#include

    main()

    {

    printf("Linuxquestions.org\n");

    }

    Save the program above and call it simplelq.c

    Here is the breakdown of the program above.

    The first line #include is a preprocessor directive. This basically tells thecompiler that we are using the functions that are in the stdio library. The stdio library

    contains all the basic functions needed for basic input and output for our program.

    The second line main() is a required function for every C program. main() is thestarting point for the program. Like all functions the body begins with a { (open curly

    brace) and ends with a } (close curly brace).

    The body of our main function printf ("Linuxquestions.org\n"); is a function

    call to the printf function. printf stands for print formatted and has complex rules forprinting text, numbers to specific formats. Here we are just displaying the test

    "Linuxquestions.org" to the screen. The \n at the end of string is a newline. It tellsprintf to end the line and start any additional text on the next line. All function calls

    in C must end in a ;

    2.2 Compiling the Source

    The compile is done from the command line.code:

  • 8/7/2019 c_in_linux

    2/4

    $ gcc -o simplelq simplelq.c

    $

    gcc is the GNU C compiler. The -o option tells it what to name the output file and thesimplelq.c is the source file.

    The output from the compiler will be a binary file called simplelq

    2.3 Running the executable

    In order to run our sample executable we will need to apply the execute permissionto the file. The we will execute it after.code:$ chmod 744 simplelq

    $ ./simplelq

    Linuxquestions.org

    The output of our sample program produced the text "Linuxquestions.org" to theconsole (or screen). Try to add some more text to the sample program, recompileand watch the output.

    3. Dealing w ith multiple sources

    In most projects that are more that a couple functions you will most likely want to

    split out the source into multiple files. Splitting the code allows the source to be

    more manageable by avoiding huge source files and to group like functions together.Here is an example that has multiple sources:

    3.1 The source filescode:/* File: appendall.h */

    /* below is a forward deceleration of a function. It differs from a

    function header by the semicolon at the end. Any source that wants to

    use the appendall function needs to include this header file. */

    void appendall( int iArgCount,

    char * iArgs[],

    char * szReturnBuffer,

    int iSize );

    code:/* File: appendall.c */

    #include

    void appendall( int iArgCount,

    char * iArgs[],

    char * szReturnBuffer,

    int iSize )

  • 8/7/2019 c_in_linux

    3/4

    {

    int i = 0;

    for ( i = 0; i < iArgCount; i++ ) /* Loop through all the arguments

    */

    {

    /* Test to see if the added length of the new arg will exceed */

    /* the length of our buffer */

    if (( strlen( szReturnBuffer ) + strlen( iArgs[i] )) < iSize )

    {

    strcat( szReturnBuffer, iArgs[i] ); /* concatenate */

    strcat( szReturnBuffer, " " ); /* add a string */

    }

    else

    {

    printf( "Error: exceeded buffer size\n");

    break;

    }

    }

    }

    code:/* File appendallmain.cpp */

    */ This include will pull in the function declaration for appendall.h

    */

    #include "appendall.h"

    #define SIZE 500

    main( int argc, char * argv[])

    {

    char szNewString[SIZE]; /* declare a character array of size 500 */

    appendall( argc, argv, szNewString, SIZE ); /* Call the function */

    printf("%s", szNewString );

    }

    3.2 Compiling the Source

    The compile is done from the command line.

    There are a couple ways to do this. One is to compile and link in one step and the

    other is to build the objects separately and then link.

    Compile and link multiple sources in one step.code:$ gcc -o appendall appendall.c appendallmain.c

    $

    Compile and link in multiple steps:code:

  • 8/7/2019 c_in_linux

    4/4

    $ gcc -c appendall.c

    $ gcc -c appendallmain.c

    $ gcc -o appendall appendall.o appendallmain.o

    $

    The -c flag tells the compiler to compiler only and not call the linker.

    It is easier to build and link in one step, but if you are using Makefiles to manageyour project the separate compile and link makes building much quicker.

    Here is a simple Makefile for building the above sources:code:all: appendall

    appendall.o: appendall.c appendall.h

    gcc -c appendall.c

    appendallmain.o: appendallmain.c appendall.h

    gcc -c appendallmain.c

    appendall: appendall.o appendallmain.ogcc -o appendall appendall.o appendallmain.o

    clean:

    rm *.o appendall

    Run ake to build the sources:mcode:$ make

    gcc -c appendall.c

    gcc -c appendallmain.c

    gcc -o appendall appendall.o appendallmain.o

    $

    Here is the basic breakup of a Makefile rule:

    target ... : prerequisites ...command......

    There must be a character before all the commands after a rule. Spaces

    instead of tabs will result in errors.

    4. Suggest Links

    http://www.gnu.org/software/gcc/gcc.htmlhttp://www.gnu.org/manual/make/html...r/make_toc.html

    http://www.linuxquestions.org/questions/search.php?s=