Issue with std::thread from c++11 - c++

I have some troubles trying to compile a program with multi-threading from the standard template library.
It return me a obscure error when i try to compile the following program :
#include <iostream>
#include <thread>
void foo()
{
std::cout << "Thread 1\n";
}
int main(int argc, char** argv)
{
std::thread tr(foo);
std::cout << "Main thread\n";
tr.join();
return 0;
}
I don't understand the error :
/tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » :
file.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x21) : undefined reference to « pthread_create »
collect2: error : ld has return 1 execution status code
I compile it with :
g++ -std=c++14 file.cpp -o test -Wall
Can anyone help me please ?

Pass -pthread to the compiler. This flag combines what is necessary to compile and link the pthread library (-lpthread is not always enough). See this question.

Related

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

segmentation fault using static libraries with std::jthread (g++-10)

I'm developing a library that works with std::jthread (new in C++20) using g++ 10.0.1. The library works fine if I compile it using share libraries, but If I compile it with static libraries I got a segmentation fault at the end of the program (thread destructor). I have narrowed down my test case to a simple thread creation and join:
#include <iostream>
#include <thread>
int main(int argc, const char** argv) {
auto t = std::jthread([]() { std::cout << "OK\n"; });
t.join();
return 0;
}
To compile I use:
g++-10 -o test --static -std=c++20 test.cc -lpthread
And running it:
% ./test
zsh: segmentation fault (core dumped) ./test
There any one has an idea what this problem could be?
Update:
Following #JérômeRichard suggested reference, I was able to compile and run my little test program without problem
g++-10 -o test --static -std=c++20 test.cc -lrt -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive
However, changing the code to use request_stop instead of join the program is segmenting fault again (https://godbolt.org/z/obGN8Y).
#include <iostream>
#include <thread>
int main() {
auto t = std::jthread([]{ std::cout << "OK" << std::endl; });
t.request_stop();
}
The problem was reported to libstdc++ (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95989) and a patch has been generated to solve the problem.

Compile failed when using boost thread

this is my code:
#include <boost/thread.hpp>
#include <iostream>
using namespace boost;
void task1()
{std::cout << "This is task1!" << std::endl;}
void task2()
{std::cout << "This is task2!" << std::endl;}
int main ()
{
thread thread_1 = thread(task1);
thread thread_2 = thread(task2);
thread_2.join();
thread_1.join();
return 0;
}
and I compile with:
g++ test.cc -o test -lboost_thread -lpthread -lboost_system
compile failed:
/tmp/ccN9cPiI.o: In function `boost::system::generic_category()':test.cc(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x7):
undefined reference to `boost::system::detail::generic_category_instance'
collect2: error: ld returned 1 exit status
What's wrong with that?
My system is Ubuntu 18.04,boost is 1_68.0.
I just tried your example and it works. I tried with boost_1_66_0 and boost_1_65_1 on Ubuntu.
The actual problem is boost itself, not your installation. You can check open bug here:
https://github.com/boostorg/system/issues/24

Vector of threads in C++

I am trying to use a vector of threads. I will receive an int as a parameter, which will be the number of threads I will have to create.
I tried something like this:
#include <iostream>
#include <thread>
#include <vector>
void credo()
{
std::cout << "threads" << std::endl;
}
void threads_creation()
{
int i;
std::vector<std::thread> threads_tab;
i = 0;
while(i < 2)
{
threads_tab.push_back(std::thread(credo));
i++;
}
}
int main()
{
threads_creation();
return (0);
}
But I get this compilation error:
/tmp/ccouS4PY.o: In function `std::thread::thread<void (&)()>(void (&)())':
threads.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x21): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
by compiling using the following command:
g++ -W -Wall -Werror -Wextra threads.cpp
What's wrong here?
The std::thread class utilizes pthreads in Linux, so you need to add the -pthread compiler flag to your command;
g++ -W -Wall -Werror -Wextra -pthread threads.cpp

undefined reference to 'myNamespace::MyClass::myFunc()' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am having a problem calling functions from my test_dll.dll in an outside c++ main. Working on eclipse cdt luna sr2 64, Windows 7. Using the MinGW toolchain for compilation.
The dll .cpp code :
#include <iostream>
#include "MyClass.h"
namespace myNamespace {
MyClass::MyClass() :a(1) {
std::cout << "MyClass():a(" << this->a << ")"<<std::endl;
}
MyClass::~MyClass() { }
void myFunc() {
std::cout << "myFunc() has been called !" << std::endl;
}
}
and it is compiled within eclipse with the following lines :
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o MyClass.o "..\\MyClass.cpp"
g++ -shared -o libtest_dll.dll MyClass.o
My main code is the following :
#include <iostream>
#include "MyClass.h"
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
myNamespace::MyClass *instance = new myNamespace::MyClass;
instance->myFunc();
return 0;
}
and it is compiled with the following
g++ "-IC:\\Users\\nxa02192\\Desktop\\MY_WORKSPACE\\test_dll" "-includeC:\\Users\\nxa02192\\Desktop\\MY_WORKSPACE\\test_dll\\MyClass.h" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\test_main.o" "..\\src\\test_main.cpp"
g++ "-LC:\\Users\\nxa02192\\Desktop\\MY_WORKSPACE\\test_dll\\Debug" -o test_main.exe "src\\test_main.o" -llibtest_dll
src\test_main.o: In function `main':
C:\Users\nxa02192\Desktop\MY_WORKSPACE\test_main\Debug/../src/test_main.cpp:20: undefined reference to `myNamespace::MyClass::myFunc()'
collect2.exe: error: ld returned 1 exit status
However, what i don't get is that if i just instanciate the class, it will output the correct a=1 value, as specified in the constructor.
I already specified the include paths to the compiler and the library path and file to the linker, as you can see in the compiling commands. Any Ideas ? Thanks !
Change
void myFunc() {
std::cout << "myFunc() has been called !" << std::endl;
}
to
void MyClass::myFunc() {
std::cout << "myFunc() has been called !" << std::endl;
}