MetallicPriest
MetallicPriest

Reputation: 30765

undefined reference to sync_fetch_and_add_4

Whenever I try to use __sync_fetch_and_add with -m32 on a 64 bit machine, I get the following error, while it compiles fine with normal 64 bit. I am using gcc compiler 4.1.2. What can be the problem here and what is the solution?

replication.cpp:(.text+0xb3b): undefined reference to `__sync_fetch_and_add_4'
replication.cpp:(.text+0xb82): undefined reference to `__sync_fetch_and_add_4'
replication.cpp:(.text+0xcc2): undefined reference to `__sync_fetch_and_add_4'
/tmp/cc7u9FLV.o: In function `potential_barrier_leader(unsigned int, pthread_barrier_t*)':
replication.cpp:(.text+0xd3f): undefined reference to `__sync_fetch_and_add_4'
replication.cpp:(.text+0xd54): undefined reference to `__sync_fetch_and_add_4'
/tmp/cc7u9FLV.o:replication.cpp:(.text+0xdb0): more undefined references to `__sync_fetch_and_add_4' follow
collect2: ld returned 1 exit status
make: *** [all] Error 1

Upvotes: 3

Views: 13640

Answers (4)

NorbertM
NorbertM

Reputation: 1336

With old GCC versions (4.5 for example) you might encounter the problem of __sync_fetch_and_add() being undeclared when using the g++ driver instead of gcc for linking a shared library.

Background: __sync_fetch_and_add is defined within static libgcc and missing from shared library libgcc_s.

So simply adding -lgcc might solve the problem. But there is a more elegant solution.

See here for full information and workaround(s):
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46563

Upvotes: 0

toey knuwho
toey knuwho

Reputation: 31

hahah! there are 5+ "standard atomic libs" (+kernel support) that's hardly atom ic if you ask me. but ignore it, is all a timely distraction.

so your building, ie glibc and get that error (i did)

glibc-2.11.x expects gcc-4.4.x to define it internally, and you have gcc sans bu ilt-in atomic, likely you didnt specify arch that gcc accepts (due to lacky dire ctions). were glibc likes 786, gcc wants 386 and figures 786 maybe. use "nativ e" should do it. opt(march) and opt(mtune) ARE NON OPTIONAL gcc builds wrong w/ o them (likely)

you won't find a header or libfoo that defines it (per say)

for linux-gnu you might use (a simple for moi build)

cd gcc-4.4.foo
./configure --with-glibc-version=2.11 --enable-threads=posix \
--disable-cloog --disable-ppl --disable-libssp --enable-__cxa_atexit \
--disable-rpath --disable-nls --disable-bootstrp --disable-multilib \
--with-system-libunwind

IMPORTANT: if you build gcc w/o mtune march right, gcc wont define sync_fetch_and_add (p.s. glibc sync_fetch_and_add_4 is just macro for sync_fetch_and_add which, aga in, glibc expects is defined)

also if you replace gcc-3.foo with gcc-4.4.foo and are compiling you may need:

[ -n "$newgnu" ] && CFLAGS="$CFLAGS -march=native -mtune=native "
[ -n "$newgnu" ] && \
CFLAGS="$CFLAGS -std=gnu89 " && CPPFLAGS="$CPPFLAGS -std=gnu89 "

i newly need this (newgnu) to build binutils-ver/: -Wstrict-aliasing=0

cd glibc-2.foo/
./configure  --with-headers=/usr/src/linux/usr/include \
--enable-kernel=2.2.foo \
--disable-profile --disable-sanity-checks --with-tls \
--disable-rpath --disable-nls
  • thanks guy in holland for posting --std that might be an issue for gcc upgraders !

  • no thanks to comittees continually changing and also creating "standards" that make depends problems in gcc :( use .h or .c appropriately for foo "builtin" to add features you want in your code like everyone else !!

have fun :)

Upvotes: 1

Try using a more recent GCC compiler (e.g. GCC 4.6). I tried to compile with gcc -S -O3 -m32 -fverbose-asm sync-3.c the test file gcc/testsuite/gcc.c-torture/compile/sync-3.c and it works. My gcc (on Debian/Sid/AMD64) is the system gcc 4.6.2 compiler.

Upvotes: 1

MetallicPriest
MetallicPriest

Reputation: 30765

Using -march=i486 flag did the trick for me.

Upvotes: 7

Related Questions