Based on the GCC compiler _ sync_sub_and_fetch_4 in Embedded Linux, the gcc compiler

Source: Internet
Author: User

Based on the GCC compiler _ sync_sub_and_fetch_4 in Embedded Linux, the gcc compiler

**************************************** **************************************** **************************************** *******************************
Author: EasyWave time: 2015.02.21

Category: Linux application-solution to the GCC compiler _ sync_sub_and_fetch_4 error in Embedded Linux

NOTE: If any error occurs, please correct it. These are my Learning Log articles ......

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

I. GCC atomic operations for variables under Multithreading

An atomic operation is an operation that cannot be further divided. In a multi-threaded program, suboperations are a very important concept and are often used to implement some synchronization mechanisms. In GCC in Linux or cross-compiling GCC in Embedded Linux, the _ sync _ * series built-in functions are provided from 4.1.2 to provide atomic operations for addition and subtraction and logical operations. The statement is as follows:


The difference between the two groups of functions is that the first group returns the value before the update, and the second group returns the updated value. Note: The type can be 1, 2, 4, or 8-byte int type, namely:

Int8_t/uint8_t

Int16_t/uint16_t

Int32_t/uint32_t

Int64_t/uint64_t

The extensible parameters (...) after the two groups of functions (...) it is used to indicate which variables need memory barrier, because GCC currently implements full barrier (similar to the mb () in linux kernel (), indicates that all memory operations prior to this operation will not be reordered to this operation), so this parameter can be omitted.


Ii. undefined reference to '_ sync_sub_and_fetch_4' problem Solution

In some embedded Linux cross-compiling GCC environments, there may be no atomic operation functions in such processes, or it may be caused by the old uClibc library. When I compile BlueZ, we found that when the uClibc and GCC compiler versions are too old, many system functions are not available, and many errors that have not been seen during BlueZ compilation. The most difficult thing to do is the undefined reference to '_ sync_sub_and_fetch_4' problem error. Later, I found a solution on a foreign website, that is, to download a linux_atomic.c file from GCC in a later version, this error can be solved by compiling it into a static library and linking it to your compiled project code. Because my GCC cross-compilation tool for Embedded Linux is relatively low, it is still in version 4.3, the undefined reference to '_ sync_sub_and_fetch_4' problem may occur because the atomic operation function under the thread is not implemented in the Cross-compilation tool:


For the corresponding embedded Linux thread atomic operation function, usually put in gcc/config/xxxx/linux-atomic.c, xxxx is a directory of different architectures, such as: ARM platform, is in the gcc/config/arm/linux-atomic.c, as shown below:


Using libtool to compile the linux-atomic.c file, you can get a static library, add this static library to your actual project can solve this problem, the specific approach is 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 and named it build. sh. Put the linux-atomic.c and build. sh in the same directory, and then execute./build. sh. For specific operations, see:


Finally add the liblinux-atmoic.a to your actual program LIBS to compile. You can add this option in LIBS = "-llinux-atmoic. Recompile the linux-atomic.c and add it to the project to be compiled to resolve errors such as undefined reference to '_ sync_sub_and_fetch_4' problem.


Related Article

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.