Cygwin gcc - asm 错误:

发布于 2024-08-05 00:40:44 字数 236 浏览 1 评论 0原文

我有一个用C编写的项目,最初是在Linux上完成的,但现在必须在Windows上完成。部分代码在多个地方包含此行,

asm("movl temp, %esp");

但这会导致“对‘temp’的未定义引用”错误。

使用 gcc 4.3.2 编译器(在另一台机器上测试)在 Linux 上编译没有问题,这是我在 Cygwin 上的版本。还有其他方法可以完成这条线正在做的事情吗?

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");

But that causes an "undefined reference to `temp'" error.

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?

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

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

发布评论

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

评论(1

新雨望断虹 2024-08-12 00:40:44

您需要将 cygwin 版本从 更改

asm("movl temp, %esp");

asm("movl _temp, %esp");

是,它是相同的编译器和汇编器,但它们的设置不同以与主机系统兼容。

您可以通过简单地告诉 gcc 要使用的特定名称来隔离系统相关的符号前缀:

int *temp asm("localname");
...
__asm__("movl localname,%esp");

这避免了某种 #if 并删除了主机操作系统依赖项,但添加了编译器依赖项。说到编译器扩展,有些人会写(参见 info as):

#ifdef __GNUC__
    __asm__("movl %[newbase],%%esp"
                :
                : [newbase] "r,m" (temp)
                : "%esp");
#else
#error haven't written this yet
#endif

这里的想法是,这种语法允许编译器通过查找 temp 来帮助您,即使它位于寄存器中或需要一些指令来加载,并且为了避免与您发生冲突,它可能使用的是您破坏的寄存器。它需要这个,因为编译器不会以任何方式解析普通的 __asm__()。

就您而言,您似乎正在实现自己的线程包,因此这些都不重要。 Gcc 不打算使用 %esp 进行计算。 (但为什么不直接使用 pthreads...?)

You need to change the cygwin version from

asm("movl temp, %esp");

to

asm("movl _temp, %esp");

Yes, it's the same compiler and assembler but they are set up differently for compatibility with the host system.

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");

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

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.

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...?)

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