Results 1 to 6 of 6

Thread: Compilation and modules in C and C + +

  1. #1
    Join Date
    May 2008
    Posts
    33

    Compilation and modules in C and C + +

    This article aims to introduce the basic notions of compiling C and C + + programming and modular.

    It helps to understand the error messages compiler. The concepts discussed here are independent of the fact that it uses Windows or Linux. However, it is rather under linux to see more detail all that happens when compiling a C or C + +.

    Introduction

    In general, a compiler aims to convert a text file (containing source code) into a binary file (eg an executable). Once the executable built, it is launched as any program. This program can start without the availability of source code.

    A compiled language (like C or C + +) is against an interpreted language (eg a shell script) or pseudo-interpreted (eg a python).

    Installing a compiler C and C + +

    Under Linux

    It uses gcc and g + + in general. To install it through its usual package manager. Such as debian (or a distribution based on debian) just type in a root or sudo:

    aptitude update
    aptitude safe-upgrade
    aptitude install gcc g + +

    You can also install the same way a development environment such as kdevelop (KDE) or Anjuta (in gnome).

  2. #2
    Join Date
    May 2008
    Posts
    33

    Re: compilation and modules in C and C + +

    Phases of compilation

    The first step is to write the source code in C or C + + (files. C and. H C and. Cpp and. Hpp in C + +). Then we start compiled, for example with gcc (C) or g + + (C + +). The compilation (meaning the wave of the term) involves three main phases.

    1. The precompiler

    The compiler begins with each instruction applied to past precompiler (all lines that begin with a #, including # define). These instructions are very simple because they consist of only large copy or delete sections of code without trying to compile.

    It is in that time that the # define in a source file (. C or. Cpi) or in a header (. H. Hpp) are replaced with code C / C + +. At the end of this stage, there is more to the compiler of instruction beginning with a #.

    2. The compilation

    Then, the compiler compiles each source file (. And c. Cpi), ie it creates a binary file (. O) the source file, except for the file containing the main function. This phase is the compilation itself.

    These first two steps are performed by DC when using gcc / g + +. Finally, the compiler switches to containing the hand.

    The linkage

    Finally, the compiler assemble with each file. O and any binary files that you use libraries (files. And has. So under linux. Dll file under windows).

    It checks in particular that every function called in the program is not only declared (this is done at compile time) but also implemented (something he had not reviewed here). It also checks that function is not implemented in several files. O.

    This phase, also called link edition, is the final phase to get an executable (noted. Exe in windows, usually no extension Linux).

    This step is provided by ld when using gcc / g + +.

    Warnings and error

    Obviously, in a development environment, simply click on "build" and these three phases are conducted in a transparent manner. However, it is important to keep in mind to properly interpret the error messages or warning tone compiler when there a.

    I recall a warning means that the code is ambiguous and that the code can be interpreted differently from one compiler to another, but the executable can be created.

    Conversely, an error means that the code could not be fully compiled and executable could not be (re) established. If a code can compile with warnings and must compile without errors, it is better to try to code so that has not error or warning.

    The major steps to write a program in C or C + +

    Write the source code

    A simple notepad will suffice, for example, you can write to the file plop.c:

    Code:
     # include <stdio.h> 
    
      int main () ( 
          printf ( "plop! \ n"); 
          return 0; 
      )

  3. #3
    Join Date
    May 2008
    Posts
    33

    Re: compilation and modules in C and C + +

    Compiler
    Call Linux directly gcc (-W-Wall can display more messages to check whether the code is clean, the o-plop.exe to say that the executable must create ' call plop.exe):

    Code:
     gcc-W-Wall-o plop.exe plop.c
    Implicitly the compiler made three steps that I described just before thee.

    1) Pre -

    Code:
     / * Everything that is defined by <stdio.h>, including printf () * / 
    
      int main () ( 
          printf ( "plop! \ n"); 
          return 0; 
      )
    2) Compilation (it is well printf because it is stated in <stdio.h>)

    3) Edit link (it is well printf in the binary lib c). You can also check Linux ldd:

    Code:
    ldd plop.exe
    .. which provides:

    Code:
     Linux-gate.so.1 => (0xb7f2b000) 
              libc.so.6 => / lib/i686/cmov/libc.so.6 (0xb7dbb000) 
              / lib/ld-linux.so.2 (0xb7f2c000)
    On the second line we can see it uses the lib v. Then he creates plop.exe. It also verifies that there was no error or warning.

  4. #4
    Join Date
    May 2008
    Posts
    33

    Re: Compilation and modules in C and C + +

    Execution

    It remains only the start:

    Code:
    . / plop.exe
    This gives as expected:

    Code:
     plop!
    If an error occurs at the time (seg fault, not enough memory ... etc.) must often use a debugger (eg gdb or ddd), review the source code etc ... In any case, this is not a compilation.

    Attention, big windows trap

    To run a program under windows, two methods are possible.

    Method 1: You can start a program from the MS-DOS (by clicking start and run by typing "cmd"). It then goes in the right directory with the cd and run the program. In this case everything will be alright.

    Method 2: If we run the program from the Explorer, Windows launches the MS-DOS, which runs the program, it ends, makes out to MS-DOS, Windows and firm orders ms-dos. Specifically we have nothing but time to see. We therefore need a "pause" before the end of the program if we want to start your executable this way:

    Code:
    # include <stdio.h> 
    
      int main () ( 
          printf ( "plop! \ n"); 
    
          getchar (); / * not put the program until it supports more than a touch * / 
          return 0; 
      )

  5. #5
    Join Date
    May 2008
    Posts
    33

    Re: Compilation and modules in C and C + +

    The errors

    Compile Error

    Suppose my source code forgets to include the file <stdio.h> (which is declared the printf), or ";" I will then an error message compilation (syntax error, parse error. ..). These are the classic mistakes.

    Error linkage

    These errors are more subtle because they do not syntax, but how is structured and compiled the program. They are easy to recognize when using gcc and g + + because messages about errors ld (the link).

    Example of Multidefination

    A linkage error can occur when you write the code of a program using multiple files. We will now illustrate this kind of mistake.

    Suppose that my program is written through 3 files ah, ac, and main.c. The AH header is included by both files and source main.c ac main.c The file contains the main ().

    1) If I just compile ac, does not file containing hand, we must specify the compiler (-c option in GCC) otherwise it does not know how to create an executable, since there are no as a starting point. That is why the file containing the main (no-c option) and other sources compiling files differently. These are:

    Code:
    gcc-Wall-W ac-c 
      gcc-W-Wall-o plop.exe main.c ao
    The options-W-Wall can display more messages of warning.
    - The first order ao built from ac
    - The second generation combined with the binary main.c, the assembling ao, and produces an executable (plop.exe)

    We can see immediately that if the program has an error in action, the compiler will trigger an error when compiling This action will cause cascading errors in other files. That is why when a program does not compile, it starts with the first error messages, they are resolved, so you compile ... until all errors are resolved.

    2) Remember that in normal times, it said the function in the header (eg ah):

    Code:
    void foo ();
    and that implements the source file (eg ac):

    Code:
     # include "ah" 
      # include <stdio.h> 
    
      void foo () ( 
        printf ( "plop! \ n"); 
      )
    Suppose now that implement function foo () in ah (ie the feature is not just declared in ah). In other words, the file contains ah

    Code:
    # include <stdio.h> 
    
      void foo () ( 
        printf ( "plop! \ n"); 
      ) 
    
    ... ac and the file contains, for example: 
      # include "ah" 
    
      void f () ( 
        foo (); 
      )
    The file is included by ah main.c and ac Thus ah content is copied to ac and main.c. Thus, these two files each with an implementation of the function foo () (the same course!), But the compiler will not know which make and trigger an error multi definition when linkage:

    Code:
    (mando @ Aldur) (~) $ gcc-Wall-W main.c ao 
      ao: In function `plop ': 
      ac: (. text +0 x0): multiple definition of `plop ' 
      / tmp / ccmRKAvQ.o: main.c: (. text +0 x0): first defined here 
      collect2: ld returned 1 exit status
    This justifies why the implementation of a function must be generally in a source file (. C or. Cpi) and not in a header (. H. Hpp). It will just two exceptions to this rule in C + +: inline functions and functions templates (or methods of a class template).

  6. #6
    Join Date
    May 2008
    Posts
    33

    Re: Compilation and modules in C and C + +

    Modular programming: multi definitions, loops inclusion ..

    Suppose I have 3 files: main.c file and ah, and ah Suppose bh bh s'incluent and each other. These two files s'incluent each other forever! This is where the pre compiler comes to our rescue because we can avoid the # include not indefinitely be put in ah:

    Code:
     # ifndef A_H 
      # define A_H 
      # include "bh" 
      / * The contents of the header ah * / 
    
      # endif
    and bh:

    Code:
      # ifndef B_H 
      # define B_H 
      # include "ah" 
      / * The contents of the header bh * / 
    
      # endif
    What happens will happen in practice? The compiler will start with a file (such ah). As at this stage A_H is not defined, it argues, then it sets ah and continues to read the header ah, including bh At this stage B_H is not defined, so the same way we go in the header bh and on active A_H.

    It now reads the contents of which wants to include bh ah It comes again ah but this time, A_H is defined so we ignore the contents of the header. We finish reading the header bh, which solves the # include "bh" that was being addressed in ah Then we ended the header bh

    This may seem twisted, but it must be understood that when a header is included several times, the multidefination may appear (especially if structures are reported in the headers). Therefore, this mechanism systematically lock in all headers.

    Function declared ... but not found

    If a function is declared, used, but not implemented, a linkage error occurs. This typically occurs in two cases:
    - Was declared a function but it has not yet implemented
    - We want to use a library, it was properly included the corresponding headers, but we forgot to enter those compiler setting bookstores.

    Further with the compilation: makefile

    If a development environment permits through a "new project" to manage your code in order to compile full, you must compile doubts that each file. C and assemble everything. When does not have development environment to avoid manually typing a "gcc" by source file, there is a "makefile" which deals with describe how to build the executable.

    In practice, it is sensible to write one more of your files C / C + + so that the code is easy to compile. Indeed, if the file is written correctly, launch the makefile to build the full program. Linux on tape then the command:

    Code:
    make

Similar Threads

  1. How to do static compilation in VLC
    By Virginia in forum Windows Software
    Replies: 6
    Last Post: 09-07-2011, 10:38 AM
  2. What does conditional compilation mean in programming
    By Mithun Seth in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 02:20 PM
  3. Compilation of a library (FFTW)
    By Zool in forum Software Development
    Replies: 3
    Last Post: 27-11-2009, 12:27 PM
  4. Set nero compilation to udf
    By pratikag in forum Windows Software
    Replies: 3
    Last Post: 07-08-2009, 11:42 AM
  5. Itunes Compilation problem
    By Gruss in forum Windows Software
    Replies: 3
    Last Post: 27-05-2009, 02:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,964,158.44008 seconds with 17 queries