如何在 Visual Studio for C/C 中同时编译 32 位和 64 位应用程序?在 makefile 中?

发布于 2024-11-04 04:02:40 字数 1093 浏览 6 评论 0原文

我的问题类似于 针对 32 位以及在同一解决方案/项目中使用 Visual Studio 的 64 位

但是,我需要在 GNUmakefile 中完成此任务。

例如,如果我想通过 gcc 交叉编译 32 和 64 位应用程序,我可以使用 -m32 &编译和链接期间的 -m64 标志。此方法对于 Visual Studio 来说有所不同,因为我必须运行 vcvarsall.bat x86 来编译 32 位,并运行 vcvarsall.bat x64 来编译 64 位来设置编译环境。

all: foo.exe foo64.exe

foo.exe: obj32/foo.o
    link.exe /MACHINE:X86 $(OTHER_FLAGS) /out:$@ $^

foo64.exe: obj64/foo.o
    link.exe /MACHINE:X64 $(OTHER_FLAGS) /out:$@ $^

obj32/foo.o: foo.c
    cl.exe $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c $<

obj64/foo.o: foo.c
    cl.exe $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c $<

上面的示例不起作用,因为您需要在 32 位和 64 位编译之间重新运行 vcvarsall.bat 环境脚本。如果我在运行 vcvarsall.bat x86 后尝试编译上述示例 makefile,则在尝试链接 64 位可执行文件时会收到此错误:

 fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

是否有一种方法可以同时构建 32 位和 64 位应用程序调用make?

My question is similar to Targeting both 32bit and 64bit with Visual Studio in same solution/project.

However, I need to accomplish this in a GNUmakefile.

For instance, if i wanted to cross compile 32 and 64 bit applications via gcc, I can use the -m32 & -m64 flags during compilation and linking. This method is different for visual studio because I have to run vcvarsall.bat x86 to compile for 32 bit and vcvarsall.bat x64 for 64 bit to setup my environment for compilation.

all: foo.exe foo64.exe

foo.exe: obj32/foo.o
    link.exe /MACHINE:X86 $(OTHER_FLAGS) /out:$@ $^

foo64.exe: obj64/foo.o
    link.exe /MACHINE:X64 $(OTHER_FLAGS) /out:$@ $^

obj32/foo.o: foo.c
    cl.exe $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c 
lt;

obj64/foo.o: foo.c
    cl.exe $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c 
lt;

The above sample would not work because you need to rerun the vcvarsall.bat environment script in between compilation of 32 and 64 bit. If I try to compile the above sample makefile when after running vcvarsall.bat x86, I would get this error when trying to link the 64 bit executable:

 fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

Is there a way we can accomplish building both 32 and 64 bit applications with one invocation of make?

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

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

发布评论

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

评论(1

月亮邮递员 2024-11-11 04:02:40

您不必必须使用 vcvar。这些是用于在一种平台环境或另一种平台环境中构建的便捷脚本。通过显式调用适当的命令并向它们传递适当的选项,您可以编译两者,而无需调用两个不同的批处理文件。

以下是将“foo”程序示例编写为 GNU makefile 的一种方法:

BIN32 = $(VS100COMNTOOLS)..\..\vc\bin
BIN64 = $(VS100COMNTOOLS)..\..\vc\bin\amd64
LIB32 =
LIB64 = \
    /libpath:"$(VS100COMNTOOLS)..\..\vc\lib\amd64" \
    /libpath:"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64"

all: foo.exe foo64.exe

foo.exe: obj32/foo.obj
    "$(BIN32)/link.exe" /MACHINE:X86 $(OTHER_FLAGS) $(LIB32) /out:$@ $^

foo64.exe: obj64/foo.obj
    "$(BIN64)/link.exe" /MACHINE:X64 $(OTHER_FLAGS) $(LIB64) /out:$@ $^

obj32/foo.obj: foo.c
    "$(BIN32)/cl.exe" $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c 
lt;

obj64/foo.obj: foo.c
    "$(BIN64)/cl.exe" $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c 
lt;

这仍然假设 vcvarsall.bat x86 已预先运行,但需要更多工作,甚至可以消除这些工作。

You don't have to use vcvars. Those are convenience scripts for building in one platform environment or the other. By explicitly invoking the proper commands and passing them the appropriate options you can compile both without having to invoke two different batch files.

Here is one way to write your "foo" program example as a GNU makefile:

BIN32 = $(VS100COMNTOOLS)..\..\vc\bin
BIN64 = $(VS100COMNTOOLS)..\..\vc\bin\amd64
LIB32 =
LIB64 = \
    /libpath:"$(VS100COMNTOOLS)..\..\vc\lib\amd64" \
    /libpath:"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64"

all: foo.exe foo64.exe

foo.exe: obj32/foo.obj
    "$(BIN32)/link.exe" /MACHINE:X86 $(OTHER_FLAGS) $(LIB32) /out:$@ $^

foo64.exe: obj64/foo.obj
    "$(BIN64)/link.exe" /MACHINE:X64 $(OTHER_FLAGS) $(LIB64) /out:$@ $^

obj32/foo.obj: foo.c
    "$(BIN32)/cl.exe" $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c 
lt;

obj64/foo.obj: foo.c
    "$(BIN64)/cl.exe" $(CFLAGS) $(INCLUDE_DIRS) /Fo$@ /c 
lt;

This still assumes vcvarsall.bat x86 was run beforehand but with more work even that can be eliminated.

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