3

Using crosstool scripts i've built under Cygwin the following compilers gcc-4.1.1 and 4.2.0 20061024 (prerelease) for Sparc architecture:

$ ./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

and

$ ./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)

It is necessary for me that i can use in my programs __sync_val_compare_and_swap function related to the Atomic-Builtins, which supports whith 4.1.* version of gcc.

I try to compile simple C code:

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;
}

But i have the following error: (on both compilers):

$ ./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

What's the problem? May be i wrong built compilers? May be Sparc architecture (SPARC v8) doesn't support this feature? I tried to compile my another programs - all good (compiled and executed).

G-71
  • 3,626
  • 12
  • 45
  • 69

5 Answers5

6

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

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • adding -lgcc_s: $ ./sparc-unknown-linux-gnu-gcc test_cas.c -lgcc_s -o test_cas don't help me. The same error. But I try to find text "__sync_val_compare_and_swap_4" and the result is following files: sparc-unknown-linux-gnu\bits\stdc++.h.gch\O0g.gch, sparc-unknown-linux-gnu\bits\stdc++.h.gch\O2g.gch, gcc\builtins.def, cc1.exe, cc1plus.exe and etc. – G-71 Feb 17 '12 at 13:56
  • you have to put library link _after_ output : `./sparc-unknown-linux-gnu-gcc test_cas.c -o test_cas -lgcc_s` – Coren Feb 17 '12 at 14:53
  • How about running `nm` or `objdump` on all the libraries in the cross target's `/usr/lib` and grepping for that function? – John Zwinck Feb 18 '12 at 17:31
  • I try to find and have the following result: $ nm -A *.so | grep __sync_val_compare_and_swap_4 nm: libc.so: File format not recognized nm: libpthread.so: File format not recognized – G-71 Feb 20 '12 at 09:00
  • 2
    It's possible those files are linker scripts or something instead of actual shared libraries. libgcc_s.so ought to be included with gcc, and is unlikely to be in /lib or /usr/lib (instead in /usr/lib/gcc/[target]/[version]/, or something similar). Check to make sure that libgcc_s.so exists; sometimes distros miss creating a symlink to the actual library (libgcc_s.so.1, possibly). And of course check for .a in addition to .so; it may be available only as a static library. – bdow Feb 22 '12 at 14:48
2

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.

Jerry Tian
  • 3,439
  • 4
  • 27
  • 29
  • Don't suppose you could post how you got NodeJS working? I'm having this exact problem at the moment. – pms1969 Aug 11 '14 at 07:57
2

It looks like there's a related gcc bug:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40134

Maybe try a newer gcc?

zmccord
  • 2,398
  • 1
  • 18
  • 14
0

on Android I was able to solve the issue with the following flags LOCAL_CFLAGS += -O3 -fopenmp LOCAL_LDFLAGS += -O3 -fopenmp -lgcc -latomic -lgomp

0

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

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388