Compiling C++ Code

Discussion in 'C++' started by BiplabKamal, Apr 30, 2016.

  1. In the earlier chapters we discussed that we write programs in human readable text and then the compiler converts it to machine readable format so that the CPU can execute the program. Now I will give an overview of the compilation process. It is not mandatory to know how compilers compile your source code but if you know it will help you. For example compiler will generate different kind of errors and warnings. It will be helpful to identify the errors if you have ideas how a compiler works. Compilers also does optimization. For example if you define a function and never invoke it, compiler will not include that function in the executable. C++ compilers also include pre-processor which process the files before the compilation starts. For example, in the previous chapter, we added following line in the program we created:

    #include<stdio.h>

    The above line is for the pre-processor to process. What pre-processor does is it expand the source code by including the text from the file stdio.h in the source code and removes text which are not to be compiled like comments. Compilation also involves 3 steps:
    1. The expanded source code produced by the preprocessor is compiled and assembly code is generated for the platform
    2. Assembly code generated by the compiler is assembled and object code for the platform is generated
    3. Object code files generated by the assembler is linked together and executable file is generated
    Above steps are hidden from the user but you can stop the compiler at any step using command line option.

    Compiling the sample program



    We will now compile our sample program written in the previous chapter. We will see do it step by step to understand the whole process. First of all we need to install compiler package in our machine. There are lot of compilers available in the market. There are IDE(Integrated Development Emnvironmet) tools for windows and Linux. For Windows You can use Microsoft Visual Studio 2015 if you already bought it. If not you can use Microsoft Visual Studio Community 2015 which is free and can be downloaded from “https://www.visualstudio.com”. For Linux lot of open source IDEs are available, you can search and research for best one for you. Eclipse is a popular free IDE for linux provided by http://www.eclipse.org/. To write programs fast the IDEs are a great help when you are already familiar with compilation process. But for our learning process we will use a command line tool option of Clang which can be downloaded from http://llvm.org/releases/download.html for specific platform. I am using Windows 7. Download per-built binary applicable for your platform and install. I downloaded LLVM-3.8.0-win64.exe for my Windows 7 64-bit OS. Do not forget to select option to add the LLVM to system path. If the installation is success, you will be able to run the command ‘clang’ in the command line window.

    To compile our code we have to create a textfile with .cpp extension like sample.cpp. We can use any text editor to create the text file. Let us first create the MyFirstProgram.cpp with the minimum code in the file to be compiled. The minimum code a function definition which will be used as the entry function. C++ requires the entry function to be named as ‘main’ and only one function with that name can exist in the whole program. C++ compiler uses this function as the entry function in the executable. The main function can have any of the following signature:
    Code:
    int main(void); // void means no argument
    int main();
    int main(int argc, char **argv);
    int main(int argc, char *argv[]);
    
    Actually the main function can have arguments or no arguments. When arguments are there they capture the command line arguments when started by the OS. We will create the no argument version. So the content of the file is
    Code:
    int main()
    {
    	return 0;
    }
    
    Now go to the command prompt, change the current directory to the directory where the MyFirstProgram.cpp file is saved and run the command “clang MyFirstProgram.cpp”.

    The above command will create a exe file in the same folder and the name of the exe is a.exe. You can change the output executable file name with -o option. Following command line command will create the MyFirstApp.exe file.
    clang MyFirstProgram.cpp -o MyFirstApp.exe

    Now let copy and paste code from the previous chapter above the main function in the MyFirstProgram.cpp file and modify the main function to call the UIFunction() like and save it. The main function will like like-
    Code:
    int main()
    {
    	UIFunction();
    	return 0;
    }
    
    Compile it with command clang MyFirstProgram.cpp -o MyFirstApp.exe

    Compiler will show 2 warnings but will generate the executable. The warnings are for the library functions we used which are obsolete. The printf() and scannf() are C style function and deprecated, means we should not use it any more. Later we will use C++ style input output methods. Now run the application with following command and interact with the application.

    MyFirstApp.exe

    The command we used for compilation actually did all four steps – Pre-process, Compile, Assemble and Link. Normally we get error from compilation and linking process. To generate compilation error remove any of the semicolons and try to compile. To generate link error remove the definition of the Add function and provide only the declaration and try to compile.

    If you want to see the intermediate out of each step use following command-
    1. To see the expanded file generated by the pre-processor run clang -E MyFirstProgram.cpp It will display the out put in the command prompt. To save the expanded code in a file (Preprocessed.cpp) run the command clang -E MyFirstProgram.cpp>>Preprocessed.cpp
    2. To see the assembly code generated by the compiler run clang -S MyFirstProgram.cpp This command will generate MyFirstProgram.s file with assembly code
    3. To see object code generated by the assembler run clang -c MyFirstProgram.cpp command. It will create MyFirstProgram.o file with object code.
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice