c++ 编译时未定义的对“__sync_val_compare_and_swap_4”错误的引用,对于Sparc v8目标使用gcc 4.1.1和4.2.0

使用crosstool脚本我在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函数中使用与Atomic-Builtins相关的函数,它支持whith 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)不支持这个功能吗?我试图编译我的另一个程序 – 一切都很好(编译和执行).

如下所述:某些目标上的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添加到你的链接行).
https://stackoverflow.com/questions/9329020/undefined-reference-to-sync-val-compare-and-swap-4-error-at-compilation-usi

转载注明原文:c++ 编译时未定义的对“__sync_val_compare_and_swap_4”错误的引用,对于Sparc v8目标使用gcc 4.1.1和4.2.0