std::atomic library dependency (gcc 4.7.3)

假如想象 提交于 2019-11-27 18:47:30

问题


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

回答1:


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./


来源:https://stackoverflow.com/questions/25311073/stdatomic-library-dependency-gcc-4-7-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!