在32位模式下编译gcc原子操作时出现链接错误 [英] Link error when compiling gcc atomic operation in 32-bit mode

查看:221
本文介绍了在32位模式下编译gcc原子操作时出现链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程式:

 〜/ test> cat test.cc 
int main()
{
int i = 3;
int j = __sync_add_and_fetch(& i,1);
返回0;

$ / code>

我正在使用GCC 4.2.2编译这个程序,多CPU 64位Intel机器:

 〜/ test> UNAME --all 
的Linux注定2.6.9-67.ELsmp#1 SMP星期三11月7日13时56分44秒EST 2007 x86_64的x86_64的x86_64的GNU / Linux的

当我以64位模式编译程序时,它编译并链接正常:

 〜/试验> /share/tools/gcc-4.2.2/bin/g++ test.cc 
〜/ test>

当我在32位模式下编译时,出现以下错误:

 〜/ test> /share/tools/gcc-4.2.2/bin/g++ -m32 test.cc 
/tmp/ccEVHGkB.o(.text+0x27):在函数'主:
:未定义参考`__sync_add_and_fetch_4'
collect2:ld返回1退出状态
〜/ test>

尽管我永远不会在32位处理器上运行,但我确实需要一个32位可执行文件所以我可以链接一些32位库。



我的2个问题是:
$ b


  1. 为什么我在32位模式下编译时出现链接错误?

    有没有一些方法可以让程序编译并链接,而仍然能够链接到一个32位的图书馆?
  2. 上原子GCC页Builtins

    lockquote
    不是所有的操作都被
    全部目标处理器所支持。如果在
    目标处理器上无法执行特定的
    操作,则会生成警告
    ,并且会生成一个外部
    函数的调用。
    外部函数将带有与内建函数相同的
    名称,
    附加后缀`_n',其中n是数据类型
    的大小。


    '/块引用>

    这是你的编译器的输出,这是指 __ sync_add_and_fetch_4 ,这是发生了什么事来看。由于某些原因,GCC没有正确生成外部函数。



    这可能是为什么你只在32位模式下出错 - 编译为64位模式时,它为您的处理器编译更紧密。当编译为32位时,它可能会使用通用的arch(例如i386),它本身不支持这些功能。尝试通过-mcpu为您的芯片系列(Xeon,Core 2等)指定一个特定的体系结构,并查看是否有效。



    如果不是,您将不得不弄清为什么GCC不包括它应该产生的适当功能。


    I have the following program:

    ~/test> cat test.cc
    int main()
    {
      int i = 3;
      int j = __sync_add_and_fetch(&i, 1);
      return 0;
    }
    

    I'm compiling this program using GCC 4.2.2 on Linux running on a multi-cpu 64-bit Intel machine:

    ~/test> uname --all
    Linux doom 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
    

    When I compile the program in 64-bit mode, it compiles and links fine:

    ~/test> /share/tools/gcc-4.2.2/bin/g++ test.cc
    ~/test>
    

    When I compile it in 32-bit mode, I get the following error:

    ~/test> /share/tools/gcc-4.2.2/bin/g++ -m32 test.cc
    /tmp/ccEVHGkB.o(.text+0x27): In function `main':
    : undefined reference to `__sync_add_and_fetch_4'
    collect2: ld returned 1 exit status
    ~/test>
    

    Although I will never actually run on a 32-bit processor, I do need a 32-bit executable so I can link with some 32-bit libraries.

    My 2 questions are:

    1. Why do I get a link error when I compile in 32-bit mode?

    2. Is there some way to get the program to compile and link, while still being able to link with a 32-bit library?

    解决方案

    From the GCC page on Atomic Builtins:

    Not all operations are supported by all target processors. If a particular operation cannot be implemented on the target processor, a warning will be generated and a call an external function will be generated. The external function will carry the same name as the builtin, with an additional suffix `_n' where n is the size of the data type.

    Judging from your compiler output, which refers to __sync_add_and_fetch_4, this is what's happening. For some reason, GCC is not generating the external function properly.

    This is likely why you're only getting an error in 32-bit mode - when compiling for 64-bit mode, it compiles for your processor more closely. When compiling for 32-bit, it may well be using a generic arch (i386, for example) which does not natively support those features. Try specifying a specific architecture for your chip family (Xeon, Core 2, etc.) via -mcpu and see if that works.

    If not, you'll have to figure out why GCC isn't including the appropriate function that it should be generating.

    这篇关于在32位模式下编译gcc原子操作时出现链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆