Home > Computer Tutorials > Computer Knowledge > How to cross-compile executable programs of different systems on ubuntu server?

How to cross-compile executable programs of different systems on ubuntu server?

WBOY
Release: 2024-03-06 13:34:02
forward
397 people have browsed it

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.

Default path

  • Compiler path: usually placed in the /usr/bin directory.
  • Header file path: System-level header files are usually placed in /usr/include, and standard library header files are generally placed in /usr/local/include.
  • Library path: System dynamic libraries are often located at /usr/lib and /lib, and static libraries are usually placed here or /usr/local/lib.

Manually set the path

Compiler path

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
Copy after login

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.

Set up the cross compiler

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
Copy after login

How to cross-compile executable programs of different systems on ubuntu server?

How to cross-compile executable programs of different systems on ubuntu server?View executable program system architecture

Then execute these two programs respectively:

How to cross-compile executable programs of different systems on ubuntu server?

Current system architecture: uname -m

How to cross-compile executable programs of different systems on ubuntu server?

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
Copy after login

/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!

Related labels:
source:mryunwei.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template