Search code examples
c++c++17zig

Is it possible to use the standard C++17 <filesystem> facilities with Zig in C++ compiler mode?


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

Solution

  • 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