the c language : compiling and running -...

17
1/17 Practical Programming The C Language : The C Language : David Bouchet David Bouchet [email protected] Compiling and Running Compiling and Running

Upload: others

Post on 16-Oct-2019

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

1/17

Practical Programming

The C Language :The C Language :

David BouchetDavid Bouchet

[email protected]

Compiling and RunningCompiling and Running

Page 2: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

2/17

Compiling and Running Processes (1)

Page 3: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

3/17

Compiling and Running Processes (2)

Static LibrariesStatic Libraries● Set of routines that are included in the executable file.● The executable file is larger.● The user does not have to own the required libraries.● If several executable files use the same library, each file

contains its own version of the library, which cannot be shared.

Page 4: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

4/17

Compiling and Running Processes (3)

Dynamic LibrariesDynamic Libraries● Set of routines that are not included in the executable file.● The executable file is smaller.● The user has to own the required libraries.● If several executable files use the same library, they all

share the same version of the library.

Page 5: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

5/17

Compiling and Running Processes (4)

● PreprocessorPreprocessor: Allows inclusion of files, macro expansions and conditional compilation. (Preprocessor instructions are prefixed with #.)

● Core compilerCore compiler: Translates C language into assembly language.

● AssemblerAssembler: Translates assembly language into native machine code (object files).

● LinkerLinker: Links different object files and libraries (if required) and generates an executable file.

● LoaderLoader: Loads an executable file into memory, links it to the dynamic libraries (if required) and executes it.

Page 6: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

6/17

Compiling Process

Each stage of the compiling process can be done separately.Each tool can be invoked one at a time.

But usually, they are invoked implicitly by the compiler. But usually, they are invoked implicitly by the compiler.

Page 7: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

7/17

Your First Program (1)

$ lshello_world.c$ gcc hello_world.c$ lsa.out hello_world.c$ ./a.outHello World!

hello_world.c

““a.out”a.out” is the default filename for the executable file.is the default filename for the executable file.

Page 8: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

8/17

Your First Program (2)

Some options can be used:Some options can be used:$ lshello_world.c$ gcc -Wall -Wextra -Werror -O3 -o hello hello_world.c$ lshello hello_world.c$ ./helloHello World!

● WallWall: Enables all warnings.

● WextraWextra: Enables extra warnings.

● WerrorWerror: Makes all warnings into error.

● 0303: Enables all optimizations.

● oo: Specifies the output filename.

Page 9: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

9/17

Multiple Files (1)

hello.c blank.c world.c

blank.h world.h

Header FilesHeader Files.h = .header.h = .header

Page 10: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

10/17

Multiple Files (2)

$ gcc -c blank.c # Generate blank.o$ gcc -c world.c # Generate world.o$ gcc -c hello.c # Generate hello.o

$ gcc hello.o blank.o world.o

Generate the object files (preprocessor, core compiler, assembler)Generate the object files (preprocessor, core compiler, assembler)

Link the object files and generate the executable file (linker)Link the object files and generate the executable file (linker)

$ ./a.outHello World!Good Bye!

Execute the executable file (loader)Execute the executable file (loader)

Page 11: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

11/17

Multiple Files (3)

Let us modify “world.c”Let us modify “world.c”

Page 12: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

12/17

Multiple Files (4)

$ gcc -c world.c # Update world.o

$ gcc hello.o blank.o world.o

Generate the object files (preprocessor, core compiler, assembler)Generate the object files (preprocessor, core compiler, assembler)

Link the object files and generate the executable file (linker)Link the object files and generate the executable file (linker)

$ ./a.outHello World!Ciao!

Execute the executable file (loader)Execute the executable file (loader)

We do not have to update We do not have to update “hello.o”“hello.o” and and “blank.o”“blank.o” because because “hello.c”“hello.c” and and “blank.c”“blank.c” have not been modified. have not been modified.

Page 13: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

13/17

GNU Make – First Makefile (1)

Makefile

$ makegcc -c hello.cgcc -c blank.cgcc -c world.cgcc hello.o blank.o world.o -o hello$ ./helloHello World!Ciao!

Page 14: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

14/17

GNU Make – First Makefile (2)

Let us modify “world.c”Let us modify “world.c” $ makegcc -c world.cgcc hello.o blank.o world.o -o hello$ ./helloHello World!Arrivederci!$ makeMake: Nothing to be done for 'all'.

Page 15: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

15/17

GNU Make – First Makefile (3)

Makefiles can be much smarter than that.Makefiles can be much smarter than that.

To know more about Makefiles, read the following page:

https://slashvar.github.io/2017/02/13/using-gnu-make.html

Page 16: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

16/17

Some Terminology

Declare a function

Define a function

Define a function

To declare a function, we use its prototype. For instance:

void print_good_bye();

is the prototype of the print_good_bye() function.

Page 17: The C Language : Compiling and Running - debug-pro.comdebug-pro.com/epita/prog/s3/lecture/compiling_and_running.pdfThe C Language : David Bouchet david.bouchet.epita@gmail.com Compiling

17/17

Header Files

● They can be included only once.● They contain declarations.

Include Guards.Prevent from being included several times.

Function declarations.

● Compiling (generating object files) needs declarations.● Linking (generating executable files) needs definitions.

Note that:Note that: