ddemidov / ev3dev-lang-cpp

C++ language bindings for http://ev3dev.org
MIT License
73 stars 38 forks source link

PThread functions not working #27

Closed ish-101 closed 7 years ago

ish-101 commented 7 years ago

When I try to compile a program that uses thread (for example, drive-test demo), it gives the following error: undefined reference to pthread_create for each time the function was called.

Thought it has no problem in the statement: #include <thread> Also, any program that doesn't use thread is working fine.

I'm compiling statically using g++-arm-linux-gnueabi compiler on Ubuntu with the following command: arm-linux-gnueabi-g++ -o main -static main.cpp ev3dev.cpp

ddemidov commented 7 years ago

What happens when you add -lpthread to your compilation flags?

ddemidov commented 7 years ago

Compilation flags are the options that you provide on command line:

arm-linux-gnueabi-g++ -o main -static main.cpp ev3dev.cpp -lpthread
ish-101 commented 7 years ago

After adding the -lpthread flag, the program compiled :smiley: But now, it's giving the following error on execution: terminate called after throwing an instance of '__gnu_cxx::__concurrence_broadcast_error' what(): __gnu_cxx::__concurrence_broadcast_error Aborted

ddemidov commented 7 years ago

Looks like -lpthread links to a pthread library that is incompatible with the one installed on ev3dev. I would try to cross-compile using docker, as is described in this tutorial: http://www.ev3dev.org/docs/tutorials/using-docker-to-cross-compile/

ish-101 commented 7 years ago

I've installed docker and downloaded the image. I've run it at the folder where my files are kept. Now when I compile, it gives me a huge set of errors. By huge, I mean it feels like there is an error at each line of main.cpp and ev3dev.h

ddemidov commented 7 years ago

I suspect you need to add -std=c++11 to your compile options. If that does not help, please provide your full command line and see least partyof the error log.

ish-101 commented 7 years ago

Should I run 1) arm-linux-gnueabi-g++ -o main -std=c++11 main.cpp OR 2) arm-linux-gnueabi-g++ -o main -std=c++11 main.cpp ev3dev.cpp

ddemidov commented 7 years ago

Second one.

ish-101 commented 7 years ago

On running the second one, I'm back to the pthread_create error: /tmp/ccBdGuxv.o: In function 'std::thread::thread<control::terminate_on_key()::{lambda()#1}>(control::terminate_on_key()::{lambda()#1}&&)': main.cpp:(.text+0x16b4): undefined reference to 'pthread_create' /tmp/ccBdGuxv.o: In function 'std::thread::thread<control::panic_if_touched()::{lambda()#1}>(control::panic_if_touched()::{lambda()#1}&&)': main.cpp:(.text+0x1808): undefined reference to 'pthread_create' collect2: error: ld returned 1 exit status

ish-101 commented 7 years ago

Ok, so now I ran the second one with -lpthread and it compiled. Let's see if it works by connecting the sensors and motors.

ish-101 commented 7 years ago

Thanks, now everything is working!