pigbabez
pigbabez

Reputation: 23

ld error while compiling with pthread in aarch64

I am trying to compile & link a simple C++ program using threads on an aarch64 based linux host. The simple program is as follows:

#include <iostream>
#include <thread>
#include <atomic>

using namespace std;

#define ITERATIONS 1000

// to be called for multi threaded execution
void increment_atomic_thread (atomic<int>& a)
{
    for (int i = 0; i < ITERATIONS; i++)
    {
        a++;
    }
}

int main (int argc, char* argv[])
{ 
    atomic<int> a, b, c, d;

    thread t1 ( [&]() { increment_atomic_thread(a); } );
    thread t2 ( [&]() { increment_atomic_thread(b); } );
    thread t3 ( [&]() { increment_atomic_thread(c); } );
    thread t4 ( [&]() { increment_atomic_thread(d); } );

    t1.join();
    t2.join();
    t3.join();
    t4.join();

    return 0;
}

This code compile fine on an x86-64 machine, however I am getting an ld error in the aarch64 machine as follows (the output is last few lines when compiling with --verbose):

attempt to open /usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/crtn.o succeeded
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/crtn.o
libm.so.6 needed by /usr/lib/gcc/aarch64-linux-gnu/7/libstdc++.so
found libm.so at /usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libm.so
/usr/bin/ld: /usr/lib/aarch64-linux-gnu/libpthread.a(pthread_create.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__stack_chk_guard@@GLIBC_2.17' which may bind externally can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/lib/aarch64-linux-gnu/libpthread.a(pthread_create.o)(.text+0x9cc): unresolvable R_AARCH64_ADR_PREL_PG_HI21 relocation against symbol `__stack_chk_guard@@GLIBC_2.17'
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

The compile command is:

g++ -g -std=c++17 -lpthread -Xlinker --verbose -o pthread_basic.app pthread_basic.cpp /usr/lib/aarch64-linux-gnu/libpthread.a

Upvotes: 0

Views: 1922

Answers (1)

pigbabez
pigbabez

Reputation: 23

Thanks @Some programmer dude. Pasting his comment here:

You don't need both -lpthread and /usr/lib/aarch64-linux-gnu/libpthread.a. Remove the last and keep -lpthread but put it at the end of the command line (order matters for libraries).

Upvotes: 1

Related Questions