Rechercher

2.4. Building software from Natively Compiled Code

download PDF

This section shows how to build the cello.c program written in the C language into an executable.

cello.c

#include <stdio.h>

int main(void) {
    printf("Hello World\n");
    return 0;
}

2.4.1. Manual building

If you want to build the cello.c program manually, use this procedure:

Procédure

  1. Invoke the C compiler from the GNU Compiler Collection to compile the source code into binary:

    gcc -g -o cello cello.c
  2. Execute the resulting output binary cello:

    $ ./cello
    Hello World

2.4.2. Automated building

Large-scale software commonly uses automated building that is done by creating the Makefile file and then running the GNU make utility.

If you want to use the automated building to build the cello.c program, use this procedure:

Procédure

  1. To set up automated building, create the Makefile file with the following content in the same directory as cello.c.

    Makefile

    cello:
    	gcc -g -o cello cello.c
    clean:
    	rm cello

    Note that the lines under cello: and clean: must begin with a tab space.

  2. To build the software, run the make command:

    $ make
    make: 'cello' is up to date.
  3. Since there is already a build available, run the make clean command, and after run the make command again:

    $ make clean
    rm cello
    
    $ make
    gcc -g -o cello cello.c
    Note

    Trying to build the program after another build has no effect.

    $ make
    make: 'cello' is up to date.
  4. Execute the program:

    $ ./cello
    Hello World

You have now compiled a program both manually and using a build tool.

Red Hat logoGithubRedditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez leBlog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

© 2024 Red Hat, Inc.