@Foreword
Due to the project requirements, C and C + + codes need to be compiled to generate executable files. During the process, I contacted and learned the download of gcc, g + + and cross compiler 1 And use methods, according to my understanding, I now make the following summary. If you have any problem, please leave a message at any time to correct it!
@What do gcc and g + + tools do?
To put it simply, gcc and g + + generate executable files (machine code that can be run by the machine) from your C and C + + code. In most cases, gcc is used to compile c code, and g + + is used to compile c + + code, but it is not limited to this. gcc can also compile c + + code, and g + + can also compile c code. See the reference link for the specific difference between gcc and g + + 2
@gcc and g + + tools under X86 architecture Ubuntu 18.04
X86 framework Ubuntu18.04 Yes Win10 The computer runs in a virtual machine.
1. Installation and use of gcc tools:
Install gcc compiler
max@ubuntu:~/Documents$ sudo apt-get install gcc
Create. c file
max@ubuntu:~/Documents$ touch main.c
Edit main.c
max@ubuntu:~/Documents$ gedit main.c
Enter the following code in the main.c file:
#include <stdio.h> int main() { printf("Hello World!\n"); return 0; }
Compile the main.c file using gcc
max@ubuntu:~/Documents$ gcc main.c
Run the generated a.out executable file (the file name generated by default in the current directory)
max@ubuntu:~/Documents$ ./a.out
Output:
Hello World!
2. Installation and use of g + + tools:
Install gcc compiler
max@ubuntu:~/Documents$ sudo apt-get install g++
Create a. cpp file
max@ubuntu:~/Documents$ touch main.cpp
Edit main.cpp
max@ubuntu:~/Documents$ gedit main.cpp
Enter the following code in the main.cpp file:
#include <stdio.h> int main() { printf("Hello World!\n"); return 0; }
Compile the main.cpp file with g + +
max@ubuntu:~/Documents$ g++ main.cpp
Run the generated a.out executable file (the file name generated by default in the current directory)
max@ubuntu:~/Documents$ ./a.out
Output:
Hello World!
@gcc and g + + tools under ARM architecture Ubuntu 18.04
Arm framework Ubuntu18.04 Yes Arm The chip runs in the development board.
1. Installation and use of gcc tools:
Install gcc compiler
root@ubuntu:~/Documents$ sudo apt-get install gcc
Create and edit. c files consistent with X86 platform
The results are as follows:
Hello World!
2. Installation and use of g + + tools:
Install g + + compiler
root@ubuntu:~/Documents$ sudo apt-get install g++
Create and edit. cpp files consistent with X86 platform
The results are as follows:
Hello World!
@Cross compiler for cross platform compilation of C and C + + code
The function of cross compiler is to compile code under different architecture platforms. For example, we have written C or C + + code under x86 architecture Ubuntu. If we want to directly compile the executable files in ARM architecture Ubuntu in x86 architecture Ubuntu system, we need to install cross compiler in our X86 platform, Compile the code using a cross compiler. Let's show around this example.
arm-linux-gcc / arm-linux-g++ The cross compiler is x86 Architecturally Ubuntu(linux)Next implementation code ARM Compilation of schema executables.
1. Installation and use of arm linux GCC cross compiler
To install arm linux GCC:
max@ubuntu:~/Documents$ sudo apt-get install gcc-arm-linux-gnueabihf
Use the same main.c file and compile with arm linux GCC. The name of the compiled executable file is "1"
max@ubuntu:~/Documents$ arm-linux-gnueabihf-gcc -o 1 main.c
Executable after running
max@ubuntu:~/Documents$ ./1
Get an error!
bash: ./1: cannot execute binary file: Exec format error
This is because we edited the files that can run under the ARM architecture Ubuntu (linux), which can not run on the X86 platform.
Copy the 1 file to the ARM board and run it. It is found that:
Hello World!
2. Installation and use of arm linux G + + cross compiler
To install arm-linux-g + +:
max@ubuntu:~/Documents$ sudo apt-get install g++-arm-linux-gnueabihf
Use the same main.cpp file and compile with arm-linux-g + +. The name of the compiled executable file is "2"
max@ubuntu:~/Documents$ arm-linux-gnueabihf-g++ -o 2 main.cpp
Executable after running
max@ubuntu:~/Documents$ ./2
Get an error!
bash: ./2: cannot execute binary file: Exec format error
This is because we edited the files that can run under the ARM architecture Ubuntu (linux), which can not run on the X86 platform.
Copy the 2 file to the ARM board and run it. It is found that:
Hello World!
3. Operation reference diagram of cross compiler
Operation interface under X86 architecture platform:
Operation interface under ARM architecture platform:
@Reference documents
If you have any problem, please leave a message at any time to correct it!