Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::atomic library dependency (gcc 4.7.3)

I've been trying to compile with std::atomic, and I'm getting unresolved references to __atomic_load, __atomic_store, and __atomic_store_16.

I know in a later version of gcc (4.8+?) you include -latomic, but I'm compiling with gcc 4.7.3; I've tried adding -latomic_ops and -latomic_ops_gpl, but neither seem to do much.

I am installing gcc 4.8.1 now, but I do have a release platform that'll really need to be compiled for 4.7.3.

Many thanks.

Edit: Ok, here's some code that results in the problem I have:

atomics.cpp
#include <atomic>
#include <stdint.h>

struct dataStruct {
    int a;
    uint16_t b;
    float c;
    dataStruct(int ai, uint16_t bi, float ci)  noexcept : a(ai), b(bi), c(ci) {
    }
    dataStruct() noexcept : dataStruct(0,0,0) {
    }
};

int main() {
    std::atomic<dataStruct> atomicValue;

    atomicValue = dataStruct(10, 0, 0);

    return atomicValue.load().b;
}

With "g++-4.8.1 *.cpp -std=c++0x -latomic", this compiles fine.

With "g++-4.7.3 *.cpp -std=c++0x -pthread -lpthread -latomic_ops", it fails with the following:

/tmp/ccQp8MJ2.o: In function `std::atomic<dataStruct>::load(std::memory_order) const':
atomics.cpp:(.text._ZNKSt6atomicI10dataStructE4loadESt12memory_order[_ZNKSt6atomicI10dataStructE4loadESt12memory_order]+0x2f): undefined reference to `__atomic_load'
/tmp/ccQp8MJ2.o: In function `std::atomic<dataStruct>::store(dataStruct, std::memory_order)':
atomics.cpp:(.text._ZNSt6atomicI10dataStructE5storeES0_St12memory_order[_ZNSt6atomicI10dataStructE5storeES0_St12memory_order]+0x35): undefined reference to `__atomic_store'
collect2: error: ld returned 1 exit status
like image 764
Doug Avatar asked Feb 12 '23 06:02

Doug


1 Answers

Ok, finally found the answer at: https://gcc.gnu.org/wiki/Atomic/GCCMM

Turns out, 4.7 did not in fact have 'official' atomics support (just the header files). If you want to use atomics in 4.7 compilers, you must download the source code linked on that page and build it yourself

gcc -c -o libatomic.o libatomic.c
ar rcs libatomic.a libatomic.o

Then, you can build it using

g++-4.7.3 -std=c++0x atomics.cpp -latomic -L./
like image 97
Doug Avatar answered Feb 15 '23 12:02

Doug