Linux 对 sync_fetch_and_add_4 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7994614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined reference to sync_fetch_and_add_4
提问by MetallicPriest
Whenever I try to use __sync_fetch_and_addwith -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?
每当我尝试在 64 位机器上使用带有 -m32 的__sync_fetch_and_add时,我都会收到以下错误,而它在正常的 64 位上编译得很好。我正在使用 gcc 编译器 4.1.2。这里可能有什么问题,解决方案是什么?
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
采纳答案by MetallicPriest
Using -march=i486flag did the trick for me.
使用-march=i486标志对我有用。
回答by Basile Starynkevitch
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.
尝试使用更新的 GCC 编译器(例如 GCC 4.6)。我尝试使用gcc -S -O3 -m32 -fverbose-asm sync-3.c
测试文件进行编译,gcc/testsuite/gcc.c-torture/compile/sync-3.c
并且可以正常工作。我的gcc
(在 Debian/Sid/AMD64 上)是系统 gcc 4.6.2 编译器。
回答by toey knuwho
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.
哈哈!如果你问我,有 5 个以上的“标准原子库”(+内核支持)几乎不是原子 ic。但无视它,都是适时的分心。
so your building, ie glibc and get that error (i did)
所以你的建筑,即 glibc 并得到那个错误(我做了)
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)
glibc-2.11.x 期望 gcc-4.4.x 在内部定义它,并且您有 gcc sans 内置原子,可能您没有指定 gcc 接受的 arch (由于缺乏方向)。glibc 喜欢 786,gcc 想要 386 和数字 786 也许。使用“native e”应该可以。opt(march) 和 opt(mtune) 是非可选的 gcc 在没有它们的情况下构建错误(可能)
you won't find a header or libfoo that defines it (per say)
您不会找到定义它的标头或 libfoo(据说)
for linux-gnu you might use (a simple for moi build)
对于 linux-gnu 你可能会使用(一个简单的 moi 构建)
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)
重要提示:如果你构建 gcc w/o mtune 行军,gcc 不会定义 sync_fetch_and_add (ps glibc sync_fetch_and_add_4 只是 sync_fetch_and_add 的宏,同样,glibc 期望定义)
also if you replace gcc-3.foo with gcc-4.4.foo and are compiling you may need:
此外,如果您将 gcc-3.foo 替换为 gcc-4.4.foo 并正在编译,您可能需要:
[ -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
我新需要这个 (newgnu) 来构建 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 !!
感谢荷兰人发布 --std 这可能是 gcc 升级者的问题!
不,感谢委员会不断改变并创建“标准”,使 gcc 中出现依赖问题:( 适当地使用 .h 或 .c 为 foo“内置”添加您想要的功能,就像其他人一样!
have fun :)
玩得开心 :)