gpt4 book ai didi

c - 如何将混合(asm、C)源代码编译成 32 位程序?

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:43 24 4
gpt4 key购买 nike

我在 64 位机器上使用 Win7 下的 Cygwin。

以下程序在 64 位模式下编译没有任何问题。

生成文件

runme: main.cpp asm.o
g++ main.cpp asm.o -o executable

asm.o: asm.asm
nasm -f elf64 asm.asm -o asm.o

asm.asm

section .data
section .bss
section .text
global GetValueFromASM

GetValueFromASM:
mov eax, 9
ret

main.cpp

#include <iostream>

using namespace std;

extern "C" int GetValueFromASM();

int main()
{
cout<<"GetValueFromASM() returned = "<<GetValueFromASM()<<endl;

return 0;
}

但是,我想在 32 位模式下编译它。因此,我将 elf64 更改为 elf,我的新 makefile 如下所示:

生成文件

runme: main.cpp asm.o
g++ main.cpp asm.o -o executable

asm.o: asm.asm
nasm -f elf asm.asm -o asm.o

但是,我收到以下错误:

$ make
nasm -f elf asm.asm -o asm.o
g++ main.cpp asm.o -o executable
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: i386
architecture of input file `asm.o' is incompatible with i386:x86-64 output
collect2: error: ld returned 1 exit status
make: *** [makefile:4: runme] Error 1

可能是什么原因?

我怎样才能解决这个问题?

Edit-1:我在 g++ 中添加了 -m32 选项。现在,错误如下:

$ make
g++ -m32 main.cpp asm.o -o executable
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libstdc++.dll.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libstdc++.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libstdc++.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libstdc++.dll.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libstdc++.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libstdc++.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libstdc++.dll.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libstdc++.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libstdc++.dll.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libstdc++.a when searching for -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lstdc++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../libcygwin.a when searching for -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../libcygwin.a when searching for -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../libcygwin.a when searching for -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -ladvapi32
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lshell32
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -luser32
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lkernel32
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status
make: *** [makefile:4: runme] Error 1

最佳答案

正如我在评论中指出的那样,[SO]: Can you run a 32 bit Cygwin application in a 64 bit installation? (@CristiFati's answer) 包含很多有用的信息。

我想从 2 个观察开始:

  • gcc 64bit 需要 -m32 标志(反之亦然:gcc 32bit 需要 -m64),否则他们将生成与其 CPU 架构相匹配的二进制文件。 32 位64 位 对象 (.o) 文件不兼容(当传递给链接器时),它将失败
  • Cygwin(包括 gcc)使用 Win 可执行文件格式或 PE( [Wikipedia]: Portable Executable)。 nasm 的输出格式(elf32elf64)生成 ELF([Wikipedia]: Executable and Linkable Format ).我不知道这是如何工作的(很明显,格式转换发生在某个地方,但我不知道确切的位置)。为了严谨起见,我将使用它们的 Win 对应版本(win32win64),它们也能正常工作(检查 nasm -hf )

我在 Cygwin 32 上也遇到了相同的行为(在您编辑问题之前):

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q055497459]> ~/sopr.sh
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> uname -a
CYGWIN_NT-10.0-WOW cfati-5510-0 2.11.2(0.329/5/3) 2018-11-08 14:30 i686 Cygwin
[prompt]>
[prompt]> ls
asm.asm asm.o builds main.cpp Makefile
[prompt]> make
MAKE Version 5.2 Copyright (c) 1987, 1998 Inprise Corp.
g++ -o main.o -c main.cpp
g++ -o executable main.o asm.o -lstdc++
main.o:main.cpp:(.text+0x5a): undefined reference to `GetValueFromASM'
collect2: error: ld returned 1 exit status

** error 1 ** deleting executable

我必须提一下,我没有在这里安装 nasm,所以我构建了(32 位)asm.o Cygwin 64。由于在终端之间切换很烦人,我完全切换到 Cygwin 64,其中有 32bit gcc (i686-pc-cygwin -gcc) 安装。

我还修改了你的两个文件(Makefile 可以改进很多,但这不是现在的重点)。

main.cpp:

#include <iostream>

using namespace std;

extern "C" int GetValueFromASM();


int main() {
cout << "sizeof(void*): " << sizeof(void*) << endl;
cout << "GetValueFromASM() returned: " << GetValueFromASM() << endl;

return 0;
}

生成文件:

.PHONY: all clean executable

objects = main.o asm.o
cpp = g++
cpp = i686-pc-cygwin-gcc
#m32_flag = -m32
asm_out_format = win32
#link_verbose_flag = -v

all: executable

clean:
rm -f $(objects) executable

executable: $(objects)
$(cpp) $(link_verbose_flag) $(m32_flag) -o $@ $(objects) -lstdc++

asm.o: asm.asm
nasm -f $(asm_out_format) -o $@ $?

main.o: main.cpp
$(cpp) $(m32_flag) -o $@ -c $?

经过几次尝试,我确定了问题的原因:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q055497459]> ~/sopr.sh
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> uname -a
CYGWIN_NT-10.0 cfati-5510-0 3.0.5(0.338/5/3) 2019-03-31 11:17 x86_64 Cygwin
[prompt]>
[prompt]> ls
asm.asm builds main.cpp Makefile
[prompt]> make
i686-pc-cygwin-gcc -o main.o -c main.cpp
nasm -f win32 -o asm.o asm.asm
i686-pc-cygwin-gcc -o executable main.o asm.o -lstdc++
main.o:main.cpp:(.text+0x5a): undefined reference to `GetValueFromASM'
collect2: error: ld returned 1 exit status
make: *** [Makefile:17: executable] Error 1
[prompt]>
[prompt]> nm -S asm.o | grep GetValueFromASM
00000000 T GetValueFromASM
[prompt]>
[prompt]> nm -S main.o | grep GetValueFromASM
U _GetValueFromASM

所以(注意 GetValueFromASM vs. _GetValueFromASM 不匹配),这是发生在Win(32bit).
[SO]: Adding leading underscores to assembly symbols with GCC on Win32? (@ephemient's answer) 成功了(还有其他解决方案,但看起来不太好)。您需要做的就是GetValueFromASM 的声明更改为:

extern "C" int GetValueFromASM() asm ("GetValueFromASM");

在构建之后(在 Cygwin 64 上):

[prompt]> uname -a
CYGWIN_NT-10.0-WOW cfati-5510-0 2.11.2(0.329/5/3) 2018-11-08 14:30 i686 Cygwin
[prompt]>
[prompt]> file executable.exe
executable.exe: PE32 executable (console) Intel 80386, for MS Windows
[prompt]> ./executable.exe
sizeof(void*): 4
GetValueFromASM() returned: 9

关于c - 如何将混合(asm、C)源代码编译成 32 位程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55497459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com