libstdc++ cannot find thread file on Mac - c++

Please take a look at this simple C++ program:
#include <iostream>
#include <thread>
void thread_test()
{
std::cout << "Thread test\n";
}
int main(int argc, const char * argv[])
{
std::thread t(thread_test);
t.join();
return 0;
}
I'm trying to compile it with Xcode on macOS Sierra. And it does not compile. It says 'thread' file not found.
In the build settings I'm using libstdc++ (GNU C++ standard library) option for C++ Standard Library. If I switch this setting to libc++ (LLVM C++ standard library with C++11 support) then it's ok, it compiles. But my goal is to compile it somehow by using libstdc++, not libc++. Is it even possible? Can you give me a piece of advice, please?

The version of libstdc++ shipped with Xcode is very old, and it doesn't not include thread. pthreads on macOS is missing some functions with timeouts (e.g. pthread_mutex_timedlock()).
You simply can't use std::thread and Xcode's supplied libstdc++ on macOS together. Either you must switch to libc++ or use a different library boost::thread does work. TinyThread++ is also an alternative that will work under macOS with libstdc++.

Related

Why Xcode 11 beta can't use C++17's <filesystem> header?

I need to use C++ 17's filesystem header for my project. As far as I know, Apple finally made it available with Xcode 11 and with macOS Catalina. I'm on the latest (beta 3) Xcode 11 and I use macOS Catalina public beta 2, so in theory it should work. But for some reason it's not, and Xcode gives errors like:
'~path' is unavailable: introduced in macOS 10.15
If I set the C++ standard library in Build Setting to libstdc++ from libc++ these error emssages gone and I got a warning:
include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead
and a ton of errors with missing iostream and cstddef in various files. What could I do to make filesystem work?
Edit: a minimal code example
#include <filesystem>
#include <iostream>
#include <string>
bool isPathDir(std::string pathString);
int main(int argc, char *argv[])
{
std::string pathString = "../test.jpg";
if (isPathDir(pathString)) {
std::cout << "This is a directory!" << std::endl;
} else {
std::cout << "This is not a directory" << std::endl;
}
}
bool isPathDir(std::string pathString)
{
std::filesystem::path path(pathString);
return std::filesystem::is_directory(path);
}
Promoting my comment into an answer:
Do you happen to have a back-deployment target older than macOS 10.15 specified? This would appear on your command-line as something like -mmacosx-version-min=<value>.
#LouisDionne Oh yes, that was the problem! As soon as I set the deployment target to 10.15 the code build perfectly! I've never heard of deployment targets before, thank you very much!
Just to explain what's going on here, the issue is that support for <filesystem> was only introduced in Mac OS 10.15. When you use -mmacosx-version-min=XYZ, you tell the compiler that your program should be able to run on versions of Mac OS all the way until version XYZ. If you use a version older than 10.15, we nicely tell you at compile-time that you can't use <filesystem>, because that would be a runtime error (likely symbol missing from libc++.dylib) if you tried running the program on a version of Mac OS older than 10.15.

Can't resolve namespace member 'thread'

I wanted to practice with standard C++ threads instead of UNIX ones, but soon encountered a problem, whenever I write std::thread CLion underlines it with red and says Can't resolve namespace member 'thread'. I checked my CMake file it's set for C++11. I reinstalled the latest version of MinGW (6.3.0) and ticked a box with G++ compiler. I have been told by my friend that he uses Cygwin and everything works. But is it still possible to make it work with MinGW?
#include <iostream>
#include <thread>
#define BUFFER_SIZE 3
#define PROD_NUM 3
#define CONS_NUM 2
void produce(){
//production
}
void consume(){
//consumption
}
int main() {
std::cout << "Hello, World!" << std::endl;
int i,j;
std::thread producer(produce);
std::thread consumer (consume);
return 0;
}
The code itself has literally nothing
EDIT
in thread library there is
#pragma GCC system_header
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
#include <chrono>
#include <functional>
#include <memory>
#include <cerrno>
#include <bits/functexcept.h>
#include <bits/functional_hash.h>
#include <bits/gthr.h>
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* #defgroup threads Threads
* #ingroup concurrency
*
* Classes for thread support.
* #{
*/
/// thread
class thread
{
public:
// Abstract base class for types that wrap arbitrary functors to be
// invoked in the new thread of execution.
struct _State
{
virtual ~_State();
virtual void _M_run() = 0;
};
can you make sure if the library is available in the CLion toolchain? For example Cygwin does have the include.
CLion shows things red when it can't link codes with the library.
It is possibly a host environment variable error. Make sure your CMakeLists.txt is working and your environment variables, standard library linkage is correct as well as your compiler setup.
Compiler version and and standard libraries compatible. (e.g. you are using a cross-compiler (RasPi, Android) but environment vars shows host library etc. will make it fail)
Check this relevant post, it may help.
C++11 std::threads vs posix threads
Ok, so I finally solved the problem. I installed Cygwin and in CLion Settings I manually linked C/C++ compilers (for some reason CLion was unable to auto-detect them). Cleared all and re-indexed the project. Now it shows no errors and code compiles.
Regarding MinGW, I read on cplusplus.com some posts regarding the issue but they were about previous versions of MinGW and it was said that they finally fixed it, however I tell: No, they didn't. Here there is a nice repository and its README file suggests that thread of win32 rely on gthreads, however i found gthread file in my libraries everything seemed ok... so still need to investigate the issue. Write your ideas and experience here if you know more.
As for now solution is Cygwin, use it instead of MinGW.
P.S. Thanks #KillzoneKid for links

C++ URDL Compile Errors with Xcode 6.1 / LLVM 6.0

I want to include the C++ Libary called URDL. I am developing a program on Xcode with Clang Compiler using Boost 1.56.
As a Sample Project I used this code here and include the Boost Libaries- and Include-Paths in the settings of my Xcode Project.
#include <iostream>
#define URDL_HEADER_ONLY 1
#include <boost/array.hpp>
#include <urdl/http.hpp>
#include <urdl/istream.hpp>
int main(int argc, const char * argv[])
{
urdl::istream is;
return 0;
}
Then I compile I get the following Errors:
in http.ipp from URDL
/Users/maximilian/XcodeProjects/Libaries/urdl-0.1/include/urdl/impl/http.ipp:25:23: Exception specification of overriding function is more lax than base version
and in handler_invole_helpers.hpp from URDL
/usr/local/Cellar/boost/1.56.0/include/boost/asio/detail/handler_invoke_helpers.hpp:37:3: Call to 'asio_handler_invoke' is ambiguous
My suggestion is that URDL is not compatible with this Version of Boost (1.56).
Thanks in Advance
lux_
PS: I must use URDL or at least boost::asio (which URDL is based on, but URDL makes my work much simpler) because it is a homework for an university course.
Problem Solved ! :) Download the URDL Version from Github instead of the Website think-async.com as I did before.

How do I get the new C++ threading support on Mac OS X with clang?

I just want to compile the following program on Mac OSX 10.8 using Apple clang version 4.1 (tags/Apple/clang-421.11.66):
#include <thread>
using namespace std;
int main() {
cout << "Hello world";
}
But I get:
../src/FirstCAgain.cpp:13:10: fatal error: 'thread' file not found
#include <thread>
I enabled c++11 support and I'm using the Eclipse C/C++ Development Tooling.
The question is: How do I get the new C++ threading support on Mac OS X ?
You need to use the new libc++, which isn't the default:
clang++ -stdlib=libc++ threadtest.cpp
(Of course you also need to include iostream, but I assume that wasn't you confusion.)

XCode std::thread C++

For a small project for school I need to create a simple client / server construction which will run on a router (with openWRT) and I am trying to do something with threads in this application.
My C++ skills are very limited, so I found this on the internet as an basic example.
#include <thread>
#include <iostream>
void doSomeWork( void )
{
std::cout << "hello from thread..." << std::endl;
return;
}
int main( int argc, char *argv[] )
{
std::thread t( doSomeWork );
t.join();
return 0;
}
When I try to run this in Xcode (4.5.2) I get the following error:
Attempt to use an deleted function
And it shows some code of:
__threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}
I think I need to do something with 'build settings' or 'link library' or something? But I am not quite sure what to do exactly. I thought I might need to set the following settings (which i found here)
In the Build Settings tab for your project, scroll down to "Apple LLVM Compiler 4.1 - Language"
Set the setting "C++ Language Dialect" to "C++11 [-std=c++11]"
Set the setting "C++ Standard Library" to "libc++ (LLVM standard C++ library with C++11 support)"
But those settings where already set.
Is there any flag / library or something I am missing?
Use G++ instead of LLVM in XCode. Don't forget to link thread libs (-lpthread - or -pthread, -lrt) in compiler build settings. And count with the differences of thread behaviour across Win/Mac/Linux OS (despite it's POSIX)