Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

c++ - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...