Linux 在 C 中检测 64 位编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5272825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting 64bit compile in C
提问by Daniel
is there a C macro or some kind of way that i can check if my c program was compiled as 64bit or 32bit at compile time in C?
是否有 C 宏或某种方法可以检查我的 C 程序在编译时是在 C 中编译为 64 位还是 32 位?
Compiler: GCC Operating systems that i need to do the checks on: Unix/Linux
编译器:GCC 我需要检查的操作系统:Unix/Linux
Also how could i check when running my program if the OS is capable of 64bit?
另外,如果操作系统支持 64 位,我在运行程序时如何检查?
采纳答案by Anomie
Since you tagged this "gcc", try
既然你标记了这个“gcc”,试试
#if __x86_64__
/* 64-bit */
#endif
回答by Alex B
Use a compiler-specific macro.
I don't know what architecture you are targeting, but since you don't specify it, I will assume run-of-the-mill Intel machines, so most likely you are interested in testing for Intel x86and AMD64.
我不知道你的目标是什么架构,但由于你没有指定它,我将假设普通的 Intel 机器,所以你很可能对测试Intel x86和AMD64感兴趣。
For example:
例如:
#if defined(__i386__)
// IA-32
#elif defined(__x86_64__)
// AMD64
#else
# error Unsupported architecture
#endif
However, I prefer putting these in the separate header and defining my own compiler-neutral macro.
但是,我更喜欢将它们放在单独的头文件中并定义我自己的编译器中立宏。
回答by pmg
The same program source can (and should be able to) be compiled in 64-bit computers, 32-bit computers, 36-bit computers, ...
相同的程序源可以(并且应该能够)在 64 位计算机、32 位计算机、36 位计算机、...
So, just by looking at the source, if it is any good, you cannot tell how it will be compiled. If the source is not so good, it may be possible to guess what the programmer assumed would be used to compile it under.
因此,仅通过查看源代码,如果它有任何好处,您无法知道它将如何编译。如果源代码不太好,则可能会猜测程序员假定将用于编译它的内容。
My answer to you is:
我给你的答案是:
There is a way to check the number of bits needed for a source file only for bad programs.
有一种方法可以检查源文件所需的位数,仅针对坏程序。
You should strive to make your programs work no matter on how many bits they will be compiled for.
无论将编译多少位,您都应该努力使您的程序工作。
回答by R.. GitHub STOP HELPING ICE
Here is the correct and portable test which does not assume x86 or anything else:
这是正确且可移植的测试,它不假设 x86 或其他任何东西:
#include <stdint.h>
#if UINTPTR_MAX == 0xffffffff
/* 32-bit */
#elif UINTPTR_MAX == 0xffffffffffffffff
/* 64-bit */
#else
/* wtf */
#endif
回答by Patrick Schlüter
An easy one that will make language lawyer squeem.
一个会让语言律师尖叫的简单方法。
if(sizeof (void *) * CHARBIT == 64) {
...
}
else {
...
}
As it is a constant expression an optimizing compiler will drop the test and only put the right code in the executable.
由于它是一个常量表达式,优化编译器将放弃测试,只将正确的代码放入可执行文件中。
回答by Shelby Moore III
The question is ambiguous because it doesn't specify whether the requirement is for 64-bit pointers or 64-bit native integer arithmetic, or both.
这个问题是模棱两可的,因为它没有指定要求是 64 位指针还是64 位本机整数算术,或两者兼而有之。
Some other answers have indicated how to detect 64-bit pointers. Even though the question literally stipulates "compiled as", note this does not guarantee a 64-bit address space is available.
其他一些答案指出了如何检测 64 位指针。即使问题字面上规定“编译为”,请注意,这并不能保证 64 位地址空间可用。
For many systems, detecting 64-bit pointers is equivalent to detecting that 64-bit arithmetic is not emulated, but that is not guaranteed for all potential scenarios. For example, although Emscripten emulates memory using Javascript arrays which have a maximum size of 232-1, to provide compatibility for compiling C/C++ code targeting 64-bit, I believe Emscripten is agnostic about the limits(although I haven't tested this). Whereas, regardless of the limits stated by the compiler, Emscripten always uses 32-bit arithmetic. So it appears that Emscripten would take LLVM byte code that targeted 64-bit int
and 64-bit pointers and emulate them to the best of Javascript's ability.
对于许多系统,检测 64 位指针等同于检测到 64 位算术未被模拟,但这并不能保证适用于所有潜在场景。例如,虽然 Emscripten 使用最大大小为 2 32-1 的Javascript 数组来模拟内存,以提供编译面向 64 位的 C/C++ 代码的兼容性,但我相信 Emscripten 不知道这些限制(尽管我还没有测试这个)。而不管编译器规定的限制如何,Emscripten 始终使用 32 位算术。因此,Emscripten 似乎会采用针对 64 位int
和 64 位指针的LLVM 字节代码,并以 Javascript 的最佳能力模拟它们。
I had originally proposed detecting 64-bit "native" integers as follows, but as Patrick Schlüter pointed out, this only detects the rare case of ILP64:
我最初提议检测 64 位“本机”整数如下,但正如 Patrick Schlüter 指出的那样,这只能检测ILP64的罕见情况:
#include <stdint.h>
#if UINT_MAX >= 0xffffffffffffffff
// 64-bit "native" integers
#endif
So the correct answer is that generally you shouldn't be making any assumptions about the address space or arithmetic efficiency of the nebulous "64-bit" classification based on the values of the limits the compiler reports. Your compiler may support non-portable preprocessor flags for a specific data modelor microprocessor architecture, but given the question targets GCC and per the Emscripten scenario (wherein Clang emulates GCC) even these might be misleading (although I haven't tested it).
所以正确的答案是,通常您不应该根据编译器报告的限制值对模糊的“64 位”分类的地址空间或算术效率做出任何假设。您的编译器可能支持特定数据模型或微处理器架构的非便携式预处理器标志,但鉴于问题针对 GCC 和每个 Emscripten 场景(其中 Clang 模拟 GCC),即使这些可能会产生误导(尽管我还没有测试过)。
Generally speaking none of these scenarios can be relied upon to give any reliable indication of whether a 64-bit address space and non-emulated 64-bit arithmetic is available, thus they are basically useless (w.r.t. to said attributes) except in the context of a build system that is not agnostic. Thus for said attributes, it is preferred to set build macros that so the build system can select which variant is compiled.
这些场景的一般说来没有可依靠得到的任何可靠指示一个64位的地址空间和非模拟64位运算是否可用,因此它们基本上是无用的(WRT到所述属性),除了在上下文一个不可知的构建系统。因此,对于所述属性,最好设置构建宏,以便构建系统可以选择编译哪个变体。
回答by Jonathon Reinhart
GLIBC itself uses this (in inttypes.h
):
GLIBC 本身使用这个(在inttypes.h
):
#if __WORDSIZE == 64
回答by HaSeeB MiR
Use this UINTPTR_MAXvalue to check build type.
使用此UINTPTR_MAX值来检查构建类型。
#include <stdio.h>
#include <limits.h>
#if UINTPTR_MAX == 0xffffffffffffffffULL
# define BUILD_64 1
#endif
int main(void) {
#ifdef BUILD_64
printf("Your Build is 64-bit\n");
#else
printf("Your Build is 32-bit\n");
#endif
return 0;
}
回答by DarkDust
A compiler and platform neutral solution would be this:
编译器和平台中立的解决方案是这样的:
// C
#include <stdint.h>
// C++
#include <cstdint>
#if INTPTR_MAX == INT64_MAX
// 64-bit
#elif INTPTR_MAX == INT32_MAX
// 32-bit
#else
#error Unknown pointer size or missing size macros!
#endif
Avoid macros that start with one or more underscores. They are not standard and might be missing on your compiler/platform.
避免以一个或多个下划线开头的宏。它们不是标准的,可能会在您的编译器/平台上丢失。