The solution of __sync_sub_and_fetch_4 error of GCC compiler based on embedded Linux

Source: Internet
Author: User

*************************************************************************************************************** ****************************************
Easywave Date: 2015.02.21

Category: Linux applications-embedded Linux under GCC compiler __sync_sub_and_fetch_4 Error Resolution Notice: Reproduced, please keep the link

Note: If there is an error, please correct me. These are the journal articles I studied ...

*************************************************************************************************************** ****************************************

One: GCC atomic operation of variables under multi-threading

Atomic operations are non-split operations, and atomic manipulation is a very important concept in multithreaded programs, and it is often used to implement some synchronization mechanisms. The __sync_* series built-in functions are provided from 4.1.2 in the cross-compilation of GCC or embedded Linux in the Linux system, providing atomic operations for addition and subtraction and logical operations. Its declaration is as follows:


The difference between these two sets of functions is that the first group returns the value before the update, and the second group returns the updated value. Note: type can be an int of type 1,2,4 or 8 byte length, i.e.:

int8_t/uint8_t

int16_t/uint16_t

int32_t/uint32_t

int64_t/uint64_t

these two sets of functions are followed by an extensible parameter (...). Used to indicate which variables require memory barrier, because the current GCC implementation is full barrier (similar to the MB () in Linux kernel, which means that all memory operations before this operation will not be reordered after this operation), so you can skip this parameter.


Two: undefined reference to ' __sync_sub_and_fetch_4 ' problem how to resolve

        in some embedded Linux cross-compiled GCC environments may not have atomic operation functions under this kind of process, or it may be that the UCLIBC library is older, because I compile BlueZ, It is found that when the UCLIBC and GCC compiler versions are too old, there are many system functions that are not available and there are many errors that are not seen when compiling BlueZ. The hardest to fix is undefined Reference to ' __sync_sub_and_fetch_4 ' problem error, later found a solution on the foreign site, that is, to the high version of the GCC download a linux_ ATOMIC.C file, compile it into a static library link to your specific compiled project code to resolve this error. Because my embedded linux gcc cross-compiler tool is relatively low, or 4.3 version, perhaps in the cross-compiler tool does not have the thread under the atomic operation function into the resulting undefined reference to ' __sync_sub_and_fetch_4 ' problem problem occurred in ftp://ftp.mirrorservice.org/sites/ sourceware.org/pub/gcc/releases/gcc-4.6.0/Download a 4.6 version of GCC down as follows:


For the corresponding embedded Linux under the thread of the atomic operation function, is generally placed in the gcc/ config /xxxx/< Span style= "Color:rgb (85,85,85); font-family:arial; font-size:14px; Line-height:26px ">LINUX-ATOMIC.C , XXXX is the directory of different schemas, such as: ARM platform, that is, in GCC/CONFIG/ARM/LINUX-ATOMIC.C, as follows:


Using the Libtool tool to compile the linux-atomic.c file, you can get a static library and add this static library to your actual project to solve this problem, as follows:

#!/bin/bashar= "Arm-linux-uclibcgnueabi-ar" ranlib= "Arm-linux-uclibcgnueabi-ranlib" CC= " ARM-LINUX-UCLIBCGNUEABI-GCC "Libtool--tag=cc--mode=compile arm-linux-uclibcgnueabi-gcc-g-o2-mt LINUX-ATOMIC.LO-MD -MP-MF linux-atomic. Tpo-c-o linux-atmoic.lo linux-atomic.clibtool--tag=cc--mode=link arm-linux-uclibcgnueabi-gcc-module-avoid-version -g-o2-o liblinux-atmoic.la Linux-atmoic.lo

I made a script here, named it: build.sh, put LINUX-ATOMIC.C and build.sh in the same directory, and then execute./build.sh. The specific operation is shown in the following:


Finally, add the LIBLINUX-ATMOIC.A to your actual program libs you want to compile. Specifically, you can add this option in libs= "-llinux-atmoic". recompiling LINUX-ATOMIC.C and adding it to the project to be compiled resolves errors such as undefined reference to ' __sync_sub_and_fetch_4 ' problem.


The solution of __sync_sub_and_fetch_4 error of GCC compiler based on embedded Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.