Link error when compiling gcc atomic operation in 32-bit mode

前端 未结 2 1902
面向向阳花
面向向阳花 2020-12-31 04:52

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\'

2条回答
  •  耶瑟儿~
    2020-12-31 05:21

    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.

提交回复
热议问题