


How to cross-compile executable programs of different systems on ubuntu server?
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
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The complex type is used to represent complex numbers in C language, including real and imaginary parts. Its initialization form is complex_number = 3.14 + 2.71i, the real part can be accessed through creal(complex_number), and the imaginary part can be accessed through cimag(complex_number). This type supports common mathematical operations such as addition, subtraction, multiplication, division, and modulo. In addition, a set of functions for working with complex numbers is provided, such as cpow, csqrt, cexp, and csin.

prime is a keyword in C++, indicating the prime number type, which can only be divided by 1 and itself. It is used as a Boolean type to indicate whether the given value is a prime number. If it is a prime number, it is true, otherwise it is false.

std is the namespace in C++ that contains components of the standard library. In order to use std, use the "using namespace std;" statement. Using symbols directly from the std namespace can simplify your code, but is recommended only when needed to avoid namespace pollution.

The fabs() function is a mathematical function in C++ that calculates the absolute value of a floating point number, removes the negative sign and returns a positive value. It accepts a floating point parameter and returns an absolute value of type double. For example, fabs(-5.5) returns 5.5. This function works with floating point numbers, whose accuracy is affected by the underlying hardware.

Config represents configuration information in Java and is used to adjust application behavior. It is usually stored in external files or databases and can be managed through Java Properties, PropertyResourceBundle, Java Configuration Framework or third-party libraries. Its benefits include decoupling and flexibility. , environmental awareness, manageability, scalability.

The min function in C++ returns the minimum of multiple values. The syntax is: min(a, b), where a and b are the values to be compared. You can also specify a comparison function to support types that do not support the < operator. C++20 introduced the std::clamp function, which handles the minimum of three or more values.

There are three ways to find the absolute value in C++: Using the abs() function, you can calculate the absolute value of any type of number. Using the std::abs() function, you can calculate the absolute value of integers, floating point numbers, and complex numbers. Manual calculation of absolute values, suitable for simple integers.

Life cycle of C++ smart pointers: Creation: Smart pointers are created when memory is allocated. Ownership transfer: Transfer ownership through a move operation. Release: Memory is released when a smart pointer goes out of scope or is explicitly released. Object destruction: When the pointed object is destroyed, the smart pointer becomes an invalid pointer.
