Cygwin gcc - asm错误:
我有一个用C编写的项目,最初是在Linux上完成的,但现在必须在Windows上完成。部分代码在几个地方包含此行
I have a project written in C that originally was being done on Linux, but now must be done on Windows. Part of the code include this line in several places
asm("movl temp, %esp");
但是这会导致未定义引用`temp'错误。
But that causes an "undefined reference to `temp'" error.
在Linux上使用gcc 4.3.2编译器(在另一台机器上测试)编译没有问题,这是我在Cygwin上的版本。
This has no problem compiling on Linux using the gcc 4.3.2 compiler (tested on another machine), which is the version I have on Cygwin. Is there another way to accomplish what this line is doing?
您需要从
asm("movl temp, %esp");
到
asm("movl _temp, %esp");
是的,它是相同的编译器和汇编程序,但是它们的设置方式与主机系统兼容。
Yes, it's the same compiler and assembler but they are set up differently for compatibility with the host system.
您可以通过简单地告诉gcc使用特定的名称来隔离系统相关的符号前缀:
You can isolate the system-dependent symbol prefixing by simply telling gcc a specific name to use:
int *temp asm("localname");
...
__asm__("movl localname,%esp");
这避免了某种类型的#if,并删除了主机操作系统依赖,但添加了一个编译器依赖。说到编译器扩展,有些人会写(例如 info as
):
This avoids an #if of some sort and removes a host OS dependency but adds a compiler dependency. Speaking of compiler extensions, some people would write (see info as
) something like:
#ifdef __GNUC__
__asm__("movl %[newbase],%%esp"
:
: [newbase] "r,m" (temp)
: "%esp");
#else
#error haven't written this yet
#endif
这里的想法是,这种语法允许编译器帮助你,通过找到 temp
,即使它在说谎在寄存器或需要几个指令加载,还要避免与你冲突,它的情况下,它是使用一个注册你破烂。它需要这个,因为一个简单的 __ asm __()
不以任何方式被编译器解析。
The idea here is that this syntax allows the compiler to help you out, by finding temp
, even if it's lying about in a register or takes a few instructions to load, and also to avoid conflicting with you, it case it was using a register you clobbered. It needs this because a plain __asm__()
is not parsed in any way by the compiler.
,你似乎正在实现自己的线程包,所以没有一个真的很重要。 Gcc不打算使用%esp进行计算。 (但为什么不使用pthreads ...?)
In your case, you seem to be implementing your own threading package, and so none of this really matters. Gcc wasn't about to use %esp for a calculation. (But why not just use pthreads...?)