0

edit: My question is not related to Link errors using <filesystem> members in C++17 at all! -lstdc++fs is not available on clang14 for macOS 10.13

edit2: Latest Xcode for macOS 10.13 is clang10, that is another reason for using clang14 from llvm. Also I can not update Xcode because my macOS Is stuck at high-serria

I am compiling the minimal example below with clang14.

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    std::cout << "Current path is " << fs::current_path() << '\n';       
}

as follows:

clang-14++ path.cpp -std=c++20

which results in a linking error:

ld64.lld: error: undefined symbol: std::__1::__fs::filesystem::__current_path(std::__1::error_code*)                                                                                                     
>>> referenced by /var/folders/_z/1q84ymcj36dgx7d2z7ybs6h40000gn/T/path-201e1b.o                                                                                                                         
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

running -v outputs the following: https://pastebin.com/G1PDDkTJ

My understanding is that -lc++ is linking against the system's default libc++ (the one shipped with apple-clang and not the one with shipped clang14)

I verified that by appending -L/path/to/clang14/lib/folder before the -lc++ and it did compile and work however, generated the warning:

ld64.lld: warning: /Users/alia/.local/clang_llvm_apple/lib/libc++.dylib has version 11.6.0, which is newer than target minimum of 10.13.0                                                                
ld64.lld: warning: /Users/alia/.local/clang_llvm_apple/lib/libunwind.dylib has version 11.6.0, which is newer than target minimum of 10.13.0

any way to tell clang to link against its own default libc++? or should I just alias some command to compile first and then append -L as above and link? also, does this have any impact or possible gotchas that I should be aware of? I am using clang14 because apple-clang doesn't support address sanitizers.

Serilena
  • 66
  • 8
  • Can you change the target minimum to 11.6.0? – Eljay Jul 10 '22 at 20:25
  • on MacOs i recommend to you using a gcc not clang – Eriss69 Jul 10 '22 at 20:31
  • @Eljay would you mind to tell how to do that? – Serilena Jul 10 '22 at 20:34
  • @Eriss69 I apologize, there are many reasons I am using clang, so I can not switch. – Serilena Jul 10 '22 at 20:35
  • 1
    _I am using clang14 because apple-clang doesn't support address sanitizers._ It does, actually. The following works for me (with the Xcode command line tools installed): `g++ -fsanitize=address foo.cpp`. Note that, normally speaking, `g++` redirects to Apple clang on MacOS. – Paul Sanders Jul 10 '22 at 20:39
  • `clang++ -std=c++20 path.cpp` works fine. If you have installed `clang-14++` at your own risk, try `clang-14++ -std=c++20 path.cpp -stdlib=c++` – 273K Jul 10 '22 at 20:39
  • @Eriss69 And why would that be? Apple play and recommend their own tools. – Paul Sanders Jul 10 '22 at 20:40
  • Also, if you're looking to trap and debug ASAN errors, building and debugging under Xcode provides much nicer error reporting than 'naked' ASAN. – Paul Sanders Jul 10 '22 at 20:41
  • 1
    `clang++ -fsanitize=address -std=c++20 path.cpp` works as well. – 273K Jul 10 '22 at 20:41
  • @273K Just checked that, and yes, that would be preferable. – Paul Sanders Jul 10 '22 at 20:42
  • @PaulSanders no, apple clang does not support sanitizers, not sure if you are running a newer version of macOS, running -fsanitize=address, undefined literally generating the output not supported as seen here https://pastebin.com/u5hbNDzq – Serilena Jul 10 '22 at 20:46
  • @273K tried running your command and got: invalid library name in argument '-stdlib=c++' if you meant libc++ then it is the default passed one as seen in the -v output – Serilena Jul 10 '22 at 20:49
  • Ah, well. _`detect_leaks`_ is not supported, that's true, but that's not what you asked about. The way to detect leaks on MacOS is by using the 'Leaks' tool in Instruments. Instruments is installed when you install Xcode (you'll find it in `/Applications/Xcode.app/Contents/Applications/`). – Paul Sanders Jul 10 '22 at 20:50
  • Works fine for me. Maybe you need to (re-)install the Xcode command line tools. – Paul Sanders Jul 10 '22 at 20:54
  • @PaulSanders as far as I know, newer Xcode versions require newer macOS, the latest Xcode supported for macOS 10.13.6 (my version of macOS) is clang 10 which is pretty old, I would prefer to use an up-to-date compiler when possible. For that, I have avoided Xcode so far. and used the clang14 from llvm – Serilena Jul 10 '22 at 20:56
  • @PaulSanders Just to confirm, macOS 10.15 started to ship filesystem with its own libc++, (I posted in the title my macOS is 10.13) so I will ask again, are you sure you are not using a newer version of macOS? – Serilena Jul 10 '22 at 20:57
  • `--sysroot` is likely what you need to use: https://stackoverflow.com/questions/25920130/when-running-clang-built-from-source-how-to-specify-location-of-libc-or-som – Alan Birtles Jul 10 '22 at 21:01
  • Sorry, I missed that question. Yes, I am on Monterey. So is your development machine stuck on High Sierra? You can build on newer versions of MacOS (with newer versions of Xcode / clang) and still target that. @AlanBirtles suggestion didn't seem to work out, from a quick scan of that link. – Paul Sanders Jul 10 '22 at 21:15
  • @PaulSanders NP! and yes, my development machine is stuck on HighSerria it is a bit old, + I tried the --sysroot, however it keeps generating an error on #include_next ctype.h not found, I tried adding -isystem -idirafter and also -isysroot all point to the path to include (include/c++/v1) or to the (path/to/custom/clang/build), full output is here: https://pastebin.com/Fv2XDBnM – Serilena Jul 10 '22 at 21:30
  • OK. I don't know anything about Alan's suggestion so I'm afraid I can't help there, sorry. How badly do you need `std::filesystem`? It's not totally indispensable - I don't use it, for example. – Paul Sanders Jul 10 '22 at 21:32
  • @PaulSanders NP + I don't need that much for now, (already rolled my own solution for that problem) but I used it as an example to find a way to link against the newer c++ lib, as my question is not about filesystem by itself ( I already mentioned a solution to the problem by appending -L manually) I just wanted a way to make clang14 defaults to its own library and not using the clang10 lib. – Serilena Jul 10 '22 at 21:36
  • Changing the deployment target from within Xcode is easy. (Well "easy" is relative, depending if you like or dislike Xcode's interface.) From the command line, I'm not sure. – Eljay Jul 10 '22 at 23:02

0 Answers0