对“__sync_val_compare_and_swap_4”的未定义引用编译时出错,对于 Sparc v8 目标使用 gcc 4.1.1 和 4.2.0
使用我在 crosstool 脚本rel="nofollow">Cygwin 用于 Sparc 架构的以下编译器 gcc-4.1.1 和 4.2.0 20061024(预发布):
$ ./sparc-unknown-linux-gnu-gcc -v
Using built-in specs.
Target: sparc-unknown-linux-gnu
Configured with: /crosstool-0.43/build/sparc-unknown-linux-gnu/gcc-4.1.1-glibc-2.3.6/gcc-4.1.1/configure --target=sparc-unknown-linux-gnu --host=i686-host_pc-cygwin --prefix=/opt/crosstool/gcc-4.1.1-glibc-2.3.6/sparc-unknown-linux-gnu --with-headers=/opt/crosstool/gcc-4.1.1-glibc-2.3.6/sparc-unknown-linux-gnu/sparc-unknown-linux-gnu/include --with-local-prefix=/opt/crosstool/gcc-4.1.1-glibc-2.3.6/sparc-unknown-linux-gnu/sparc-unknown-linux-gnu --disable-nls --enable-threads=posix --enable-symvers=gnu --enable-__cxa_atexit --enable-languages=c,c++ --enable-shared --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.1.1
并且
$ ./sparc-unknown-linux-gnu-gcc -v
Using built-in specs.
Target: sparc-unknown-linux-gnu
Configured with: /crosstool-0.43/build/sparc-unknown-linux-gnu/gcc-4.2-20061024- glibc-2.3.6/gcc-4.2-20061024/configure --target=sparc-unknown-linux-gnu --host=i 686-host_pc-cygwin --prefix=/opt/crosstool/gcc-4.2-20061024-glibc-2.3.6/sparc-un known-linux-gnu --with-headers=/opt/crosstool/gcc-4.2-20061024-glibc-2.3.6/sparc -unknown-linux-gnu/sparc-unknown-linux-gnu/include --with-local-prefix=/opt/cros stool/gcc-4.2-20061024-glibc-2.3.6/sparc-unknown-linux-gnu/sparc-unknown-linux-g nu --disable-nls --enable-threads=posix --enable-symvers=gnu --enable-__cxa_atex it --enable-languages=c,c++ --enable-shared --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.2.0 20061024 (prerelease)
对我来说,我可以在我的程序中使用它与 原子相关的 __sync_val_compare_and_swap
函数-Builtins,支持 4.1.* 版本的 gcc。
我尝试编译简单的 C 代码:
long cmpxchg( long* value, long comp_val, long new_val )
{
return __sync_val_compare_and_swap( value, comp_val, new_val );
}
int main()
{
return 0;
}
但出现以下错误:(在两个编译器上):
$ ./sparc-unknown-linux-gnu-gcc test_cas.c -o test_cas
/tmp/ccREXHsP.o: In function `cmpxchg':
test_cas.c:(.text+0x24): undefined reference to `__sync_val_compare_and_swap_4'
collect2: ld returned 1 exit status
有什么问题?可能是我构建的编译器错误?可能是Sparc架构(SPARC v8)不支持此功能?我尝试编译我的另一个程序 - 一切都很好(编译并执行)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如此处所述:http://gcc.gnu。 org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html
__sync_val_compare_and_swap
在某些目标上将导致函数调用(直接代码生成不可用或尚未实现)。你的情况就是这样。假设它本身对您来说不是问题,那么您需要链接定义__sync_val_compare_and_swap_4
和朋友的库,我猜是 libgcc_s (因此将-lgcc_s
添加到链接行)。As described here: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html
__sync_val_compare_and_swap
on some targets will result in a function call (where direct code generation is not available or not yet implemented). That is happening in your case. Assuming that itself is not a problem for you, you then need to link the library which defines__sync_val_compare_and_swap_4
and friends, which I am guessing is libgcc_s (so add-lgcc_s
to your link line).看起来有一个相关的 gcc bug:
http://gcc.gnu.org/ bugzilla/show_bug.cgi?id=40134
也许尝试更新的 gcc?
It looks like there's a related gcc bug:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40134
Maybe try a newer gcc?
我在ARMv5平台上编译NodeJS(基于V8引擎)时遇到了类似的问题。
基本上来说,你的GCC没有这个内置函数,要么是因为你使用的是旧版本,要么是这些函数还没有在你的平台上实现,所以“-lgcc_s”甚至可能没有帮助。
经过几个小时的谷歌,我找到了这个博客页面( http://vincesoft.blogspot.fr/2012/04/how-to-solve-undefined-reference-to.html),它相当清楚地解释了原因并给出了解决方案:
获取源代码您的平台使用 GCC 代码中的这些函数,将代码构建到库中,安装它,然后将您的应用程序链接到该库。
我没有遵循此博客中描述的确切过程,但想法是相同的,并且它有效。
希望有帮助。
I came across a similar problem when compiling NodeJS(which based on V8 engine) on ARMv5 platform.
Basically speaking, your GCC does not have this build-in functions, either because you are using an old version, or these functions are not implemented on your platform yet,so the "-lgcc_s" may even not help.
After Google for hours, I found this blog page( http://vincesoft.blogspot.fr/2012/04/how-to-solve-undefined-reference-to.html ), which explain the cause fairly clear and gave a solution:
Grab the source code of your platform with these functions from GCC code, build the code into a library, install it, and then link your applications against this library.
I did not follow the exact procedures described in this blog, but the idea is the same, and it works.
Hope it helps.
在 Android 上我可以使用以下标志解决问题
LOCAL_CFLAGS += -O3 -fopenmp
LOCAL_LDFLAGS += -O3 -fopenmp -lgcc -latomic -lgomp
on Android I was able to solve the issue with the following flags
LOCAL_CFLAGS += -O3 -fopenmp
LOCAL_LDFLAGS += -O3 -fopenmp -lgcc -latomic -lgomp
对我来说,上述失败意味着“你正在使用 gcc/mingw 交叉编译器,所以 -march=native 不起作用”(我猜)。请参阅 https://stackoverflow.com/a/24213278/32453 (基本上,您可以通过手动指定 -三月设置)。
For me, the above failure meant "you are using a gcc/mingw cross compiler, so -march=native doesn't work" (I guess). See https://stackoverflow.com/a/24213278/32453 (basically you can work around it by manually specifying the -march setting).