1

I am just getting started with the Zig. I am using Zig 0.9.0 on Windows 10. One of the features that attracts me is using Zig as a C or C++ compiler or cross-compiler. I have used Zig successfully for some toy programs, but my luck ran out when I tried to use C++17's standard library filesystem facilities. Here is a minimal example;

#include <stdio.h>
#include <filesystem>

int main( int argc, char *argv[] )
{
    std::string s("src/zigtest.cpp");
    std::filesystem::path p(s); 
    bool exists = std::filesystem::exists(p);
    printf( "File %s %s\n", s.c_str(), exists ? "exists" : "doesn't exist" );
    return 0;
}

If I attempt to build this with the following command;

zig c++ -std=c++17 src\zigtest.cpp

I get the following link error;

lld-link: error: undefined symbol: std::__1::__fs::filesystem::__status(std::__1::__fs::filesystem::path const&, std::__1::error_code*)
>>> referenced by ...

Incidentally, for a long time I didn't get this far, it turned out I needed to apply the -std=c++17 flag, until then I had this compile error rather than a link error;

src\zigtest.cpp:7:10: error: no member named 'filesystem' in namespace 'std'

Finally I'll note that (after some googling) I tried passing a -lstdc++fs flag, with no luck either. In that case I get;

error(link): DLL import library for -lstdc++fs not found
error: DllImportLibraryNotFound
Bill Forster
  • 6,137
  • 3
  • 27
  • 27

2 Answers2

3

A few comments: The fact that you are seeing std::__1::__fs::filesystem means that you're using libc++.

Trying to link to libstdc++fs which is a gcc thing, wouldn't work even if you had it on your system.

What version of libc++ are you using? The docs for libc++ are here:

They talk about using <filesystem> and what (if anything) you have to link to.

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
  • Thanks for your help. The Zig C++ build system is a little opaque (or maybe it is just my unfamiliarity) and it seems hard to find out what the underlying components and versions are. I am a little handicapped by being more familiar with MSVC than GCC or Clang too, sorry. Anyway, if I go Zig C++ --version I find it is Clang 13.0.1 (quite recent right?). I have no idea what version of libc++ I am using and I don't know how to find out. If I look at your llvm.org releases above, it seems I should either have to do nothing special (9,10) - or add -lc++fs (8).... (will add another comment) – Bill Forster Jan 09 '22 at 08:27
  • I had already tried -lc++fs as a result of previous desperate googling - with the same disappointing result as -lstdc++fs (i,e, error - DLL import library for -lc++fs not found) – Bill Forster Jan 09 '22 at 08:30
  • It's nice when I can refer to one of my own answers :-). https://stackoverflow.com/questions/38441490/how-to-check-if-libc-is-installed – Marshall Clow Jan 09 '22 at 22:18
  • Thanks I will check this when I get home tonight. I have a bad feeling about this one, my theory is that the Zig C++ compile chain is not setup for C++17, in the sense that this simple invocation of the standard library is going to defeat it. I have always been told that it is the Microsoft compiler that lags behind the standards committee, but on Visual Studio using has been easy for years. – Bill Forster Jan 09 '22 at 23:37
  • #define _LIBCPP_VERSION 13000. So https://releases.llvm.org/13.0.0/projects/libcxx/docs/UsingLibcxx.html which says nothing at all about . But https://releases.llvm.org/12.0.0/projects/libcxx/docs/UsingLibcxx.html says "Starting with LLVM 9.0, support for is provided in the main library and nothing special is required to use ." It looks like support for is now so bedded in they don't even mention it any more. Hmmmm, curiouser and curiouser. – Bill Forster Jan 10 '22 at 06:15
1

Should be solved in this PR. Jakub and Spex worked on it immediately after I linked them this question :^)

https://github.com/ziglang/zig/pull/10563

kristoff
  • 456
  • 2
  • 3
  • Thank you. Ironically, since what I was trying to do was build an executable for my friend who is on macOS (Intel) on my Windows box, I could have continued (I guess) without worrying about this (once I figured out how to retarget etc. - not obvious) I must admit I am so confused right now, for example the pull request talks about GNU and stuff not working on MSVC (itself a little puzzling MS tools have no problems with ). I assumed that Zig is using Clang through and through yet here are references to GCC (GNU?) and MSVC. I'll figure it out over time. – Bill Forster Jan 11 '22 at 02:12
  • I had already decided it might be easier to use an old Linux box, running Ubuntu 18.04, but I actually hit upon a much worse bug there that convinces me that Zig is not ready for prime-time for this kind of thing. The idea of a simple cross development C/C++ compiler is brilliant and the implementation is promising - but it's nowhere near there yet. Possibly no surprise to anyone but me. I spent a lot of time figuring out why my simple C++ program would crash on the Linux box, and eventually narrowed it down to an awe-inspiringly simple and reproduceable problem as follows (another comment) – Bill Forster Jan 11 '22 at 06:45
  • The very simple C++ program below just crashes (illegal operation) on the line m[5] = 6 when built with zig c++ on Ubuntu 18.04LTS (zig 0.9.0). Works okay if you move the map variable into main() as a stack variable. Also works as expected on WIndows. Please let me know if it would be helpful to report the problem elsewhere, thanks. `#include ` `#include ` `static std::map m;` `int main()` `{` `m[5] = 6;` `printf( "m[5] = %d\n", m[5] );` `return 0;` `}` – Bill Forster Jan 11 '22 at 06:55