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

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.

Related

CLion C++17 boost: No member named xxx in namespace

I'm setting up a C++17 + boost 1.76.0 project in Clion 2018.3, with this minimal code:
#include <boost/asio/ip/tcp.hpp>
int main(int argc, char **argv)
{
auto const address = boost::asio::ip::make_address(argv[1]);
return 0;
}
In my CMakeLists.txt I have correctly setup the C++ version:
add_definitions(-std=c++17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
And the project compiles and run fine. But Clion shows the following error on the make_address line:
No member named 'make_address' in namespace 'boost::asio::ip'
CTRL-hover on the method name correctly shows the doc, and CTRL-click correctly opens the header file.
Are we supposed to do something else to tel Clion which C++ version are we using?
Looks like the IDE and compiler don't agree in the version of boost. These functions are not specific toi a language version, but I think were introduced in a relatively recent Boostt release
It turned out that I had 2 Boost versions installed on the system, one shipped with ROS (I didn't know that) and v1.76.0 that I compiled to get boost::asio. Looks like Clion was using the one of ROS which older.
Uninstalling v1.76.0 system-wide and adding it back as a header library in the project fixed these false errors, though Clion now takes forever to anaylise the code (despite I excluded the libs folder). Using only the version shipped with ROS would be the best solution for someone not needed the latest additions of Boost (but I do).

Xcode 11.1: iostream' file not found

I just updated my MacBook Pro to macOS Catalina 10.15, and tried to compile and run a C++ command line program, but I had a problem which didn’t exist on previous versions;
This is simply the code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}
The code compiles and outputs the expected, but still the Xcode says:
fatal error: 'iostream' file not found
I tried changing the Build Settings/C++ Standard Library to libstdc++, but a warning says:
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead
And the same iostream error still exists.
I'm compiling from the command line, and none of the answers listed here (or elsewhere) worked for me.
What does seem to work (so far) is to add the following to .profile or whatever script your terminal uses to start up: (zsh, csh, bash, etc.)
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
You will probably have to change MacOSX10.15.sdk whenever you upgrade your operating system.
C_INCLUDE_PATH and CPLUS_INCLUDE_PATH are options for the clang toolchain rather than MacOS environment, so hopefully this solution will work long-term, unlike xcode-select --install (which won't fix the include directories on an upgrade) or ln -s ... /usr/include (which is now forbidden by System Integrity Protection).
I had the same problem and used the following youtube video to fix it.
https://www.youtube.com/watch?v=hrPm7tWC-BI&feature=youtu.be
or you can follow this path. Make sure to include the quotation marks
Project - Build Settings - Search Paths - Headers Search Paths, and add the following path:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/"
So, I restarted my laptop and everything seems to be fine right now, thanks for those who tried to help.
libstdc++ is not OK for Xcode Build & Compile time,
libstdc++ is OK for iPhone Run Time
From answer recommended by #Alan Birtles
libstdc++ Support was removed from the iOS 12.0 Simulator runtime, but
it remains in the iOS 12.0 (device) runtime for binary compatibility
with shipping apps.
I encountered this when declaration in .hpp file.
#include <iostream>
#include <string>
OK with
#ifdef __cplusplus
#include <iostream>
#include <string>
// usage code
#endif
I tried a fresh Catalina install with Xcode. I copied and pasted your code into "test.cpp" and then ran:
clang++ test.cpp
in the same directory as the "test.cpp" file from Terminal. The result was an "a.out" file which when run:
./a.out
output the required "Hello, World!" result. Hopefully that is of some use (as a point of reference).

libstdc++ cannot find thread file on Mac

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

macOS Clang C++17 filesystem header not found

I need to write a program using the (experimental) C++17 filesystem library but clang on my Mac (macOS 10.12.03) doesn't seem to have the filesystem header included.
Since I'm required to use the C++17, I cannot use alternatives like the Boost library.
When I try to compile a sample program that just includes filesystem and iostream (and writes to cout)
#include <filesystem>
#include <iostream>
using namespace std;
int main(){
cout << "test" << endl;
}
I get the following error message:
>clang test.cpp -std=c++1z
test.cpp:2:10: fatal error: 'filesystem' file not found
#include <filesystem>
^
1 error generated.
When I try the same using GCC 6.3 (installed via homebrew) I get:
>gcc-6 test.cpp -std=c++17
test.cpp:2:22: fatal error: filesystem: No such file or directory
#include <filesystem>
^
compilation terminated.
I also tried using experimental/filesystem instead which compiles using gcc but seems to try to compile for iOS leading to another error which seems to be related to iostream
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccd5QiVt.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccd5QiVt.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
The version of my clang is:
>clang --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I'm grateful for any helpful input since I couldn't find anything that solved my problem so far (although I might have been searching for the wrong terms).
If you need more information I'll gladly provide it but I hope to have included everything.
Libc++, which is the C++ standard library on OS X, has not moved <experimental/filesystem> to <filesystem> yet because the specification is not stable.
Hopefully <filesystem> will be a part of the Clang 6.0 release. (We missed 5.0)
Xcode 11 Beta now includes <filesystem>. Unlike the other answers indicating beta support in Xcode 10, Apple has mentioned this in the release notes.
Also mentioned in the release notes, is this is only supported by iOS 13, macOS 10.15, watchOS 6, and tvOS 13. You will only be able to use std::filesystem for projects targeting these versions or later.
In reply to Max Raskin: I've installed Xcode 10 Beta 4, from July 17, 2018, and this version does not have "#include <experimental/filesystem>" or "#include <filesystem>".
The release notes also do not mention libc++17 <filesystem>. The release notes do mention that the following are in Xcode 10: <any>, <optional>, and <variant>.
Example include file location:
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/experimental
EDIT
As mentioned in another answer <filesystem> is available in Xcode 11 Beta according to the release notes:
Clang now supports the C++17 <filesystem> library for iOS 13, macOS
10.15, watchOS 6, and tvOS 13. (50988273)
Here's hoping it's meant to stay this time!
OLD ANSWER
Just checked Xcode 10.2 Beta 4 and it has regular <filesystem>! For the curious, it's in /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/.
EDIT:
Downloaded Xcode 10.2 (10E125) aaaaand ... <filesystem> is gone again. No mention whatsoever in the release notes. If you happen to have an Xcode version that contains <filesystem> lying around (like the Beta 4 I mentioned earlier) copying the file over seems to work okay:
$ sudo cp /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/filesystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/
Mind you, of course, every Xcode update will very likely break this workaround and make another copy necessary. Also, there is probably a good reason why the beta implementation didn't make it into the release. Proceed with caution...
Including gets you the declarations, but to
get the definitions you also have to link with -lstdc++fs (for
libstdc++) or I don't know (for libc++). If someone knows, maybe they
could update this answer?
For libc++ you need to link with -lc++experimental
If anyone still interested, Xcode 10 Beta ships with libc++ that has experimental/filesystem
UPDATE one of Xcode 10 betas used to ship with it, perhaps by accident, Xcode 10.1 unfortunately, doesn't have it :(
I installed XCode 9.4 - no <filesystem>
But Homebrew came to the rescue with LLVM 6
brew update
brew install llvm
And with a change in PATH, I was away.
Recursive directory walk using ftw in c, more details here.
On, -std=c++17 for macOS version 10.xx, filesystem header is not available.
#include <ftw.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
int list(const char *name, const struct stat *status, int type)
{
if (type == FTW_NS)
{
return 0;
}
if (type == FTW_F)
{
printf("0%3o\t%s\n", status->st_mode&0777, name);
}
if (type == FTW_D && strcmp(".", name) != 0)
{
printf("0%3o\t%s/\n", status->st_mode&0777, name);
}
return 0;
}
int main(int argc, char *argv[])
{
if(argc == 1)
{
ftw(".", list, 1);
}
else
{
ftw(argv[1], list, 1);
}
return 0;
}
output looks like following:
0755 ./Shivaji/
0644 ./Shivaji/20200516_204454.png
0644 ./Shivaji/20200527_160408.png
0644 ./Shivaji/20200527_160352.png
0644 ./Shivaji/20200520_174754.png
0644 ./Shivaji/20200520_180103.png
0755 ./Saif/
0644 ./Saif/Snapchat-1751229005.jpg
0644 ./Saif/Snapchat-1356123194.jpg
0644 ./Saif/Snapchat-613911286.jpg
0644 ./Saif/Snapchat-107742096.jpg
0755 ./Milind/
0644 ./Milind/IMG_1828.JPG
0644 ./Milind/IMG_1839.JPG
0644 ./Milind/IMG_1825.JPG
0644 ./Milind/IMG_1831.JPG
0644 ./Milind/IMG_1840.JPG

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