如何将混合(asm、C++)源代码编译成 32 位程序?
How can I compile a hybrid (asm, C++) source code into a 32-bit program?
我在 64 位机器的 Win7 下使用 Cygwin 32 位。
下面的程序
生成文件:
runme: main.cpp asm.o
g++ main.cpp asm.o -o executable
asm.o: asm.asm
nasm -f elf 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;
}
给我以下错误:
$ make
nasm -f elf asm.asm -o asm.o
g++ main.cpp asm.o -o executable
/tmp/cc3F1pPh.o:main.cpp:(.text+0x26): undefined reference to `GetValueFromASM'
collect2: error: ld returned 1 exit status
make: *** [makefile:2: runme] Error 1
我不明白为什么会产生这个错误。
如何解决这个问题?
您必须按照 Windows/Cygwin:
中的惯例,在符号前加上 _
section .data
section .bss
section .text
global _GetValueFromASM
_GetValueFromASM:
mov eax, 9
ret
您的其余代码应该可以正常工作。
另一种方法是使用 -fno-leading-underscore
进行编译。但是,这可能会中断与其他(Cygwin 系统)库的链接。如果对其他平台的可移植性对您来说不重要,我建议使用第一个选项。
引自 GNU 在线文档:
-fleading-underscore
This option and its counterpart, -fno-leading-underscore
, forcibly change the way C symbols are represented in the object file. One use is to help link with legacy assembly code.
Warning: the -fleading-underscore
switch causes GCC to generate code that is not binary compatible with code generated without that switch. Use it to conform to a non-default application binary interface. Not all targets provide complete support for this switch.
我在 64 位机器的 Win7 下使用 Cygwin 32 位。
下面的程序
生成文件:
runme: main.cpp asm.o
g++ main.cpp asm.o -o executable
asm.o: asm.asm
nasm -f elf 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;
}
给我以下错误:
$ make
nasm -f elf asm.asm -o asm.o
g++ main.cpp asm.o -o executable
/tmp/cc3F1pPh.o:main.cpp:(.text+0x26): undefined reference to `GetValueFromASM'
collect2: error: ld returned 1 exit status
make: *** [makefile:2: runme] Error 1
我不明白为什么会产生这个错误。
如何解决这个问题?
您必须按照 Windows/Cygwin:
中的惯例,在符号前加上_
section .data
section .bss
section .text
global _GetValueFromASM
_GetValueFromASM:
mov eax, 9
ret
您的其余代码应该可以正常工作。
另一种方法是使用 -fno-leading-underscore
进行编译。但是,这可能会中断与其他(Cygwin 系统)库的链接。如果对其他平台的可移植性对您来说不重要,我建议使用第一个选项。
引自 GNU 在线文档:
-fleading-underscore
This option and its counterpart,
-fno-leading-underscore
, forcibly change the way C symbols are represented in the object file. One use is to help link with legacy assembly code.Warning: the
-fleading-underscore
switch causes GCC to generate code that is not binary compatible with code generated without that switch. Use it to conform to a non-default application binary interface. Not all targets provide complete support for this switch.