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

前端 未结 2 1901
面向向阳花
面向向阳花 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:03

    The answer from Dan Udey was close, close enough in fact to allow me to find the real solution.

    According to the man page "-mcpu" is a deprecated synonym for "-mtune" and just means "optimize for a particular CPU (but still run on older CPUs, albeit less optimal)". I tried this, and it did not solve the issue.

    However, "-march=" means "generate code for a particular CPU (and don't run on older CPUs)". When I tried this it solved the problem: specifying a CPU of i486 or better got rid of the link error.

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

提交回复
热议问题