如何为 32 位和 64 位创建单个 makefile?

发布于 2024-09-30 10:56:32 字数 272 浏览 2 评论 0原文

我有一个可以在 Linux (x86_64) 和 OS X Intel (x86_64) 上透明工作的 makefile。这使用 64 位特定的 GCC 选项。

有没有办法调整 makefile,以便我可以构建 32 位和 64 位 OS X PPC(ppcppc64),而无需维护单独的 arch -特定的makefiles——也许像预处理器指令之类的东西可以在构建之前确定架构?

I have a makefile that works transparently for Linux (x86_64) and OS X Intel (x86_64). This uses 64-bit specific GCC options.

Is there a way to adjust the makefile so that I could build for 32-bit and 64-bit OS X PPC (ppc, ppc64) without having to maintain separate, arch-specific makefiles — perhaps something like a pre-processor directive that can determine the architecture before building?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我偏爱纯白色 2024-10-07 10:56:32
ARCH := $(shell getconf LONG_BIT)

CPP_FLAGS_32 := -D32_BIT ...  Some 32 specific compiler flags ...
CPP_FLAGS_64 := -D64_BIT

CPP_FLAGS := $(CPP_FLAGS_$(ARCH))  ... all the other flags ...
ARCH := $(shell getconf LONG_BIT)

CPP_FLAGS_32 := -D32_BIT ...  Some 32 specific compiler flags ...
CPP_FLAGS_64 := -D64_BIT

CPP_FLAGS := $(CPP_FLAGS_$(ARCH))  ... all the other flags ...
月亮坠入山谷 2024-10-07 10:56:32

尝试包含文件。这不是标准 Makefile 语法(Single Unix v3 规范中的语法)的一部分,但受到广泛支持。使用 GNU make,它看起来像这样:

include otherfile

有了这个,您可以拥有一个如下所示的 x86 Makefile:

ARCHFLAGS = -m64 -mtune=core2
include common.mk

和一个 PowerPC Makefile:

ARCHFLAGS = -mcpu=g3
include common.mk

并且大部分编译规则将位于一个文件中 (common.mk) ,在必要时使用 $(ARCHFLAGS)

Try file inclusion. This is not part of the standard Makefile syntax (the one in the Single Unix v3 specification) but is widely supported. With GNU make, this looks like this:

include otherfile

With this, you could have an x86 Makefile like this:

ARCHFLAGS = -m64 -mtune=core2
include common.mk

and a PowerPC Makefile:

ARCHFLAGS = -mcpu=g3
include common.mk

and the bulk of your compilation rules will be in one file (common.mk), using $(ARCHFLAGS) where necessary.

苍白女子 2024-10-07 10:56:32

我认为通过使用某种构建系统(例如 cmake 或 GNU 自动工具),您可以通过更少的工作(和痛苦)来实现您的目标。

I think that you would achieve your goals with less work (and pain) by employing some kind of a build system such as cmake or GNU autotools.

话少情深 2024-10-07 10:56:32

实现此目的的一种方法是:

  • 为每个体系结构提供 makefile 片段(可能是累积的):posixwin32win32-gcc 等。
  • 有一个脚本可以运行并确定为什么片段对于主机来说是足够的
  • 将这些 makefile 片段包含到主 makefile 中
  • 允许使用环境变量进行手动覆盖(因为事情有时会很复杂;例如,考虑交叉编译器)

我在中小型项目(2 或 3 人编写代码几年),它非常适合。

One way to work this is to:

  • have makefile fragments for each architecture (possibly cumulative): posix, win32, win32-gcc, etc.
  • have a script that runs and determines why fragments are adequate for the host
  • include those makefile fragments into the main makefile
  • allow manual override using environment variables (because things can sometimes be complicated; e.g., think of cross-compilers)

I've used this in small-to-medium size projects (2 or 3 persons working on code for a few years), and it fits the bill well.

开始看清了 2024-10-07 10:56:32

看看这个:

http://mad-scientist.net/make/multi-arch .html

它有点过时,但我认为有很好的信息可能对您有用。

Have a look at this:

http://mad-scientist.net/make/multi-arch.html

It is somewhat dated but has good info that could be useful for you I suppose.

寂寞美少年 2024-10-07 10:56:32

虽然它没有明确回答 CPP 编译,但我刚刚为汇编语言创建的这个方法可以很容易地适应。

然后,

# -----------------------------------------
PROJECT = helloworld
# Expected source files: *-32.s and *-64.s
# -----------------------------------------
BITS := $(shell getconf LONG_BIT)
OBJS = $(PROJECT).o
ifdef DEBUG
    DEBUGFLGS = -g
else
    DEBUGFLGS =
endif

%.o : %-$(BITS).s
    as $(DEBUGFLGS) -o $@   
lt;

$(PROJECT): $(OBJS)
    ld  -o bin/$(PROJECT)   $(OBJS)

我将 helloworld-32.shelloworld-64.s 添加到项目文件夹中,然后通过在文件夹中运行 make 来解决所有问题。

Although it doesn't explicitly answer the CPP compilation, this method I've just created for assembly language could be easily adapted to fit.

makefile

# -----------------------------------------
PROJECT = helloworld
# Expected source files: *-32.s and *-64.s
# -----------------------------------------
BITS := $(shell getconf LONG_BIT)
OBJS = $(PROJECT).o
ifdef DEBUG
    DEBUGFLGS = -g
else
    DEBUGFLGS =
endif

%.o : %-$(BITS).s
    as $(DEBUGFLGS) -o $@   
lt;

$(PROJECT): $(OBJS)
    ld  -o bin/$(PROJECT)   $(OBJS)

I then add helloworld-32.s and helloworld-64.s to the project folder and everything works out by running make in the folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文