I believe many C programmers have had this question, what if a server has different gcc versions? Will they affect each other?
The answer is yes. When we generally use gcc to compile source files, this gcc is the first gcc found in the system environment variable PATH.
What if you want to cross-compile programs for different systems? This requires multiple sets of compilation tool chains, and the corresponding tool chains are used to compile the programs of the target system.
/usr/bin
directory. /usr/include
, and standard library header files are generally placed in /usr/local/include
. /usr/lib
and /lib
, and static libraries are usually placed here or /usr/local/lib
. Changing the environment variable for the compiler path is usually not necessary since /usr/bin
should already be in your PATH
environment variable. If you need to use a different compiler, you can directly use its full path or modify the PATH
environment variable to point to your compiler, for example:
export PATH=/path/to/your/compiler:$PATH
This will add the path you specify in front of the existing PATH
so that the system will first look for the executable file here.
I need to compile ARM architecture programs on Ubuntu, using the tool chain provided by Marvell. This toolchain contains gcc and other tools for cross-compiling ARM architecture programs, all with the same prefix.
arm编译:这样设置前缀后就可以使用交叉编译器 export CROSS_COMPILE=/home/zheng/marvell-tools-12006.0/bin/aarch64-marvell-linux-gnu- 使用环境变量进行编译: ${CROSS_COMPILE}gcc-o hello_arm hello_arm.c
View executable program system architecture
Then execute these two programs respectively:
Current system architecture: uname -m
Generally gcc will follow the system's default PATH path to find the corresponding tool components
required for compilation.
So if you want to compile programs with different architectures, you need multiple sets of tool chains. If you add the paths of these tool chains to the system environment variable PATH, there will be a conflict
, which will take precedence. Use the tool with the same name in the previous path, for example, the PATH is set to:
export PATH=/path/arm_toolchain/bin:/usr/bin:/path/x86_toolchain/bin
/path/arm_toolchain/bin and /usr/bin, /path/x86_toolchain/bin all have tools named gcc, then the former ones will overwrite the following
, and each execution will take priorityUse the earliest matching gcc tool
.
Disadvantages: Compilation tool chains of different architectures cannot be dynamically specified, and the PATH environment variable needs to be modified frequently.
Improvement method: Specify the corresponding environment variables for each tool chain
, so that different compilers can be dynamically distinguished.
This method is suitable for temporarily switching tool chains and compiling programs with different architectures. If it is a large C program, the path to the compiler is generally specified in the configuration file, and then built using make, meson and other build tools.
The above is the detailed content of How to cross-compile executable programs of different systems on ubuntu server?. For more information, please follow other related articles on the PHP Chinese website!