I installed Eclipse (actually the Xilinx SDK, but based on Eclipse) and g ++ 4.9.2, to compile a project that uses stand-alone ASIO, and I used -std = C ++ 11 in Properties -> C / C ++ Build -> Settings -> Tool settings β Other flags so that it can be compiled using all the features of C ++ 11.
I also set ASIO_HAS_STD_THREAD, ASIO_STANDALONE , etc. in common C / C ++ characters, and I expect the ASIO header to use std::thread instead of pthread . However, I still see Error from make:
undefined reference to pthread_create, ..asio-1.10.6\include\asio\detail\impl\posix_thread.ipp and posix_tss_ptr.hpp
so the problem is that I am using C ++ 11 and ASIO_HAS_STD_THREAD is ASIO_HAS_STD_THREAD , but not ASIO_HAS_PTHREADS , posix_thread.ipp should not even be enabled (via posix_thread.hpp), according to thread.hpp in ASIO:
#if !defined(ASIO_HAS_THREADS) # include "asio/detail/null_thread.hpp" #elif defined(ASIO_WINDOWS) # if defined(UNDER_CE) # include "asio/detail/wince_thread.hpp" # else # include "asio/detail/win_thread.hpp" # endif #elif defined(ASIO_HAS_PTHREADS) # include "asio/detail/posix_thread.hpp" #elif defined(ASIO_HAS_STD_THREAD) # include "asio/detail/std_thread.hpp" #else # error Only Windows, POSIX and std::thread are supported! #endif
Suspect 1 -pthread
Unlike most people, C ++ 11 does not need -pthread , and I tried to compile a simple project without -pthread in Eclipse. However, if I am mistaken, you can correct me. When I put -pthread into the linker option, it compiles, however I felt that I didnβt need pthread if not needed.
Suspect 2 - ASIO File
When I look through posix_tss_ptr.hpp, I also found in Makefile.am. I wonder if this affects the error?
So what is the cause of the problem? If not above two suspects? I hope the solution can still use the clean C ++ 11 method, rather than using pthread if my reasoning is correct.
Update
I found that ASIO_HAS_PTHREADS is not determined by me and why ASIO uses POSIX streams somewhere, but the linker needs the -pthread option. Then I traced to asio / detail / signal_blocker.hpp using the #error directive. There are only two places that they are defined and they are in ASIO config.hpp
# if defined(ASIO_HAS_BOOST_CONFIG) && defined(BOOST_HAS_PTHREADS) # define ASIO_HAS_PTHREADS 1 # elif defined(_POSIX_THREADS) # define ASIO_HAS_PTHREADS 1
ASIO is still responding to POSIX THREADS or Windows for signal_blocker.hpp shown below. This is why ASIO still needs pthread.
#if !defined(ASIO_HAS_THREADS) || defined(ASIO_WINDOWS) \ || defined(ASIO_WINDOWS_RUNTIME) \ || defined(__CYGWIN__) || defined(__SYMBIAN32__) typedef null_signal_blocker signal_blocker; #elif defined(ASIO_HAS_PTHREADS) typedef posix_signal_blocker signal_blocker; #endif
And _PTHREADS is determined from the gnu cross-compiler (arm-xilinx-linux-gnueabi), including files like features.h, posix_opt.h, etc. I'm not going to keep track of what the macro really defined, but ASIO is the source that uses _POSIX_THREADS, and therefore there must be a -pthread linker option.
Again, non ASIO C ++ 11 thread is not needed -pthread for g ++ 4.9.2, but stand-alone ASIO needs it. The following code is built correctly without -pthread in g ++ 4.9.2 (Xilinx SDK based on Eclipse):
#include <thread> void test() { for(int i=0;i<100;i++); } int main() { std::thread thread1(test); thread1.join(); return 0; }