Communication error while compiling atomic gcc operation in 32-bit mode

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 am compiling this program using GCC 4.2.2 for Linux, running on an Intel 64-bit machine with several processors:

 ~/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 a program in 64-bit mode, it compiles and binds a 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 run on a 32-bit processor, I need a 32-bit executable, so I can link it to 32-bit libraries.

My 2 questions:

  • Why am I getting a communication error when compiling in 32-bit mode?

  • Is there a way for a program to compile and link while being able to link to a 32-bit library?

+16
gcc atomic linker
Sep 25 '08 at 0:11
source share
2 answers

On the GCC page on Atomic Builtins :

Not all operations support all target processors. If a specific cannot be implemented on the target processor, a warning will be generated and an external call will be generated function. the external function will carry the same name as the built-in one, with the optional suffix `_n ', where n is the size of the data type.

Judging by your compiler, which refers to __sync_add_and_fetch_4 , this is what happens. For some reason, GCC does not correctly generate an external function.

This is probably due to the fact that you get an error only in 32-bit mode - when compiling for 64-bit mode, it compiles more accurately for your processor. When compiling for the 32-bit version, a common arch (for example, i386), which does not support these functions, may well be used. Try specifying a specific architecture for the chip family (Xeon, Core 2, etc.) via -mcpu and see if this works.

If not, you need to find out why GCC does not include the corresponding function that it should generate.

+14
Sep 25 '08 at 0:18
source share

The answer from Dan Uday was close, close enough to let me find a real solution.

According to the man page, β€œ-mcpu” is an obsolete synonym for β€œ-mtune” and simply means β€œoptimize for a specific processor (but still run on older processors, albeit less optimally)”. I tried this and it did not solve the problem.

However, "-march =" means "generate code for a specific processor (and not run on older processors)." When I tried this, he solved the problem: by indicating the CPU i486 or better getting rid of the communication 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 
+23
Sep 25 '08 at 0:33
source share



All Articles