Ubuntu system with g ++ c ++ project the terminal ---

                                                                                Ubuntu system with g ++ c ++ project the terminal ---

table of Contents

A compiler tool (g ++ / gcc) and editor (vim / gedit)
Second, the C language compiler and run
three or compiled C ++ language and running
four detailed process gcc / g ++'s

"One, two, three" in under the current folder, compile a file as an example of an executable file. For compile multiple files into a single executable file, and how to create a project and compile a project (a big point of the project is more than one .cpp, there .h, ...), the follow-up in school, in taking notes.

 

text

A compiler tool (g ++ / gcc) and editor (vim / gedit)
for C / C ++ to compile first ensure the ubuntu system contains build tools (g ++ / gcc) and editor (vim) in ubuntu environment, C language and corresponding to the C ++ compiler: gcc -> C, g ++ -> C ++.
If the environment does not exist in the following command can be entered in the terminal for installation:

sudo apt-get install build-essential
sudo apt install vim
sudo apt-get install gcc
sudo apt-get install g++

Second, the C language compiler running
the steps of: (1) Write .c files; (2) .c files compile; (3) run the executable file
(1) Write .c file
    1) .c file code is as follows hello.c :

    #include<stdio.h>
    int main()
    {
            printf("Hello world !\n");
            return 0;
    }


(2) compile .c files
compile .c files in the current path:
  gcc -o hello hello.c
After compilation you can see there is a hello executable file in the same directory hello.c.

(3) Run the executable file
in the current path run the compiled executable file: ./hello (Note that we must add the path to run, hello dot represents the current path is not a direct path to add, can not run.)
  

 
Third, compile and run C ++ language
steps: (1) write .cpp file; (2) to compile .cpp file; (3) Run the executable file
(1) Write .cpp file

    # .Cpp file hello.cpp code is as follows:

#include <the iostream> the using namespace STD; int main () { COUT << " the Hello World! " << endl; return 0 ; }

(2) Compile the .cpp file
in the current path is compiled .cpp file:
  G ++ -o hello hello.cpp
After compilation you can see there is a hello executable file in the same directory hello.cpp.

(3) Run the executable file
in the current path run the compiled executable file:  ./hello (Note that we must add the path to run, hello dot represents the current path is not a direct path to add, can not run.)
 


四、gcc/g++的详细过程
  可以手动进行这四个步骤。
第一步:预处理。#号开头的代码全被解决掉(预编译,包含库,宏定义等等),产生一个后缀.i的文件以便下一步使用。
                gcc -E hello.c    (不会产生.i文件,文件内内容打印在了终端,所以需要将这些信息重定向到.i文件)
                gcc -E hello.c >> hello.i

第二步:编译。这一步主要检查语法错误。产生一个后缀.s的文件(汇编文件)
                gcc -S hello.i     (大写字母S)

第三步:汇编。产生后缀.o的object目标文件,二进制,但不可以运行,因为缺少库信息
                gcc -c hello.s

第四步:链接。添加库信息,产生一个后缀.o的可执行文件
                gcc hello.o -o hello

Guess you like

Origin www.cnblogs.com/carle-09/p/11269438.html