1. Abi (Application binary Interface): In intel or AMD architecture, the 32-bit Solaris program is based onI386 Abi, 64-bit program based onAmd64 AbiThe 32-bit program is based onIscsi V8 Abi, 64-bit program based onIscsi V9 Abi. Run the following command to view the features supported by the current Solaris platform:
Uname-a view the operating system version
Isainfo-V: view the ABI supported by the operating system
Isainfo-B view the number of bits in the operating system kernel
Isainfo-X: view the instruction sets supported by the operating system
As for how to differentiate platforms in the source code, you can use include and then check the following macro definitions:
Macro definition |
Description |
_ X86 |
All x86 processor architectures define this macro, including 386,486, Pentium, IA-32, amd64, em64t. |
_ I386 |
This macro is defined when the output program uses 32-bit i386 Abi, which cannot be defined together with _ amd64. |
_ Amd64 |
This macro is defined when the output program uses 64-bit amd64 Abi, which cannot be defined together with _ i386. |
_ ISCSI |
This macro is defined on all the Linux, Linux, and Linux systems. |
_ Sparcv8 |
This macro is defined when the output program uses a 32-bit PostgreSQL V8 Abi. It cannot be defined together with _ sparcv9. |
_ Sparcv9 |
This macro is defined when the output program uses a 64-bit iSCSI V9 Abi, which cannot be defined together with _ sparcv8. |
In addition, the InDel and AMD platforms define_ Little_endian, Which will be defined by_ Big_endian.
2. data Model: Like other UNIX and Linux systems, 32-bit Solaris uses ilp32 and 64-bit Solaris uses the lp64 model (only the pointer type and long type change to 64-bit in the basic data type ), when GCC is used for compilation, it is defined separately_ Ilp32And_ Lp64(You do not need to include any header files ). For the differences between several data models, you can view relevant information. We will not discuss it too much here. The only thing worth noting is that since win64 uses the llp64 model (the pointer is 64-bit, but the long type is still 32-bit), so if you want to write cross-platform applications, you must be careful with the length difference of long variables in the two data models. You can use the following command to query the specific type definition:
MAN 3 types
It is recommended to include. the header file defines int8_t, uint8_t ,..., Int64_t, uint64_t, and other cross-platform types, and define int8_min, uint8_min ,..., Uint64_max and other extreme values. In the lp64 model, pointers are all 64-bit, so in some algorithms that need to convert pointers into integer values for calculation, if you convert a pointer to an int or long type, it is recommended that you convert the pointer to intptr_t or uintptr_t, Which is the value type that can ensure the pointer can be saved in any platform.
3. character constants: when using 64-bit programming, pay special attention to the use of L, UL, ll, ull or int8_c (C ),..., int64_c (C), uint8_c (C ,..., uint64_c (c) to declare const literal. Otherwise, an unsigned number is prone to errors when assigned to a 64-bit variable. For example:
Unsigned long I =-1u; // In lp64, I is assigned 0 xffffffff instead of 0 xffffffffffffff.
However, unsigned long I =-1; in lp64, I is assigned 0 xffffffffffffff,-1 is int32, upgraded to int64 first (then the symbol bit will be extended), and then converted to uint64, therefore, it is F.
4. default compilation parameters: to maintain compatibility with a large number of existing 32-bit programs, if no additional parameters are attached, the GCC compiler compiles the code with a 32-bit Abi by default, the generated binary file in elf32 format (even in 64-bit Solaris), for example:
GCC test. c
If it is on Intel or AMD, the above command will generate a binary file using i386 Abi; if it is on the intel or AMD platform, it will generate a binary file using the iSCSI V8 Abi. Note: Although GCC can cross-compile 32-bit programs in a 64-bit environment, it is not allowed to generate the iSCSI Abi object on Intel or AMD platforms, and vice versa.
To generate an ELF64 object, you must add-Xarch = generic64Or-M64For example:
Gcc-M64 test. c
Note: A 32-bit compiler can also generate 64-bit programs, but it cannot be run in a 32-bit system. In addition, not all 32-bit programs can be compiled on a 64-bit platform, or even if it has been compiled, it may not be consistent with the features of the original 32-bit platform, for example, some libraries and programs using the/proc interface.
5. Link search directory:Elf(Executable and linking format) must be consistent. If a 32-bit program is generated, all intermediate files and static libraries must be in elf32 format. If a 64-bit program is generated, all intermediate files and static libraries are required to be in the ELF64 format. If they are mixed, the compiler reports an error. If the program is connected to a dynamic library, you must search for the dynamic library in the corresponding format when the program is executed. Otherwise, an error occurs during execution. The search directory for the compiler and execution isLD_LIBRARY_PATHAndLd_library_path_64Two environment variables to control. Ld_library_path_64 is only valid for 64-bit programs. If this environment variable is not set, LD_LIBRARY_PATH is used, which may be confused with 32-bit libraries, for details about the location of 32-bit and 64-bit databases, refer to the system documentation and configuration.
6. 64-bit debugging: Generally, 32-bit GDB (gdb32) and 64-bit GDB (gdb64) are installed on 64-bit machines ), 64-bit programs can only be debugged using gdb64. Therefore, pay attention to the gdb version during debugging.
7. execute file distribution: if another project named MyApp has compiled multiple versions, including i386, amd64, sparc_v7, and sparc_v9, to facilitate one-time distribution, to execute different programs on different platforms, we 'd better place the execution files of different versions in several subdirectories, such:
./Amd64/MyApp
./I386/MyApp
./Sparcv9/MyApp
./Sparcv7/MyApp
Then write the following script in the base directory. during the execution of the following script, isalist will be used to obtain the features supported by the platform, traverse each subdirectory, and execute the first program to be found:
#! /Bin/sh Cmd = 'basename $0' Dir = 'dirname $0' Exec = For ISA in '/usr/bin/isalist'; do If [-x $ {dir}/$ {ISA}/$ {cmd}]; then Exec =$ {dir}/$ {ISA}/$ {cmd} Break Fi Done If [-z "$ {exec}"]; then Echo 1> & 2 "$0: No executable for this architecture" Exit 1 Fi Exec $ {exec} "$ {@}" |
In addition, the function of this script can also be passed in the C codeIsaexec ()Function.
Reference: http://docs.sun.com/app/docs/doc/816-5138/preface-1? L = ZH-tw & A = View