如何在 64 位机器上将 C++ 程序编译为 64 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11078915/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:49:37  来源:igfitidea点击:

How to compile a C++ program as 64-bit on 64-bit machine?

c++compilationmakefileg++64-bit

提问by xyz

Perhaps a very trivial question:

也许是一个非常微不足道的问题:

I need to compile a program as 64-bit (earlier makefile written to compile it as 32-bit).

我需要将程序编译为 64 位(早期编写的 makefile 将其编译为 32 位)。

I saw the option -m32 appearing in command line parameters with each file compilation. So, I modified the makefile to get rid of -m32 in OPTFLAG , but again when the program compiles, I still see -m32 showing up and binaries are still 32-bit. Does this m32 come from somewhere else as well?

我看到选项 -m32 出现在每个文件编译的命令行参数中。所以,我修改了 makefile 以去掉 OPTFLAG 中的 -m32 ,但是当程序编译时,我仍然看到 -m32 出现并且二进制文件仍然是 32 位的。这个 m32 也来自其他地方吗?

回答by Jonathan Wakely

-m32can only be coming from somewhere in your makefiles, you'll have to track it down (use a recursive grep) and remove it.

-m32只能来自您的 makefile 中的某个地方,您必须对其进行跟踪(使用递归 grep)并将其删除。

When I am able to force -m64, I get "CPU you selected does not support x86-64 instruction set".Any clues?. uname -a gives x86_64

当我能够强制使用 -m64 时,我得到“您选择的 CPU 不支持 x86-64 指令集”。有任何线索吗?。uname -a 给出 x86_64

That error means there is an option like -march=i686in the makefiles, which is not valid for 64-bit compilation, try removing that too.

该错误意味着有一个类似于-march=i686makefile 中的选项,它对 64 位编译无效,也尝试将其删除。

If you can't remove it (try harder!) then adding -march=x86-64after it on the command line will specify a generic 64-bit CPU type.

如果您无法删除它(请更努力!)然后-march=x86-64在命令行上添加它后将指定一个通用的 64 位 CPU 类型。

回答by Andrejs Cainikovs

If the software you are trying to build is autotools-based, this should do the trick:

如果您尝试构建的软件是基于 autotools 的,这应该可以解决问题:

./configure "CFLAGS=-m64" "CXXFLAGS=-m64" "LDFLAGS=-m64" && make

Or, for just a plain Makefile:

或者,对于一个普通的 Makefile:

env CFLAGS=-m64 CXXFLAGS=-m64 LDFLAGS=-m64 make

回答by Jayhello

If you are using cmake, you can add m64 compile options by this:

如果您使用的是 cmake,则可以通过以下方式添加 m64 编译选项:

add_compile_options(-m64)