Skip to content

Instantly share code, notes, and snippets.

@kassane
Last active October 6, 2023 19:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kassane/7e9a2da137e13eb6e1dbab726693bdb7 to your computer and use it in GitHub Desktop.
Save kassane/7e9a2da137e13eb6e1dbab726693bdb7 to your computer and use it in GitHub Desktop.
Zig/clang++ (C++20) modules
// main.cpp

import moduleTest;

#include <cstdio>

auto main() -> int
{
  printf("retInt is: %d\n", retInt());

  return 0;
}
// moduleTest.cpp

module;

export module moduleTest;

export auto retInt() -> int { return 42; }
$> zig c++ -std=c++20 -fmodules -fbuiltin-module-map -fmodules-ts -Xclang -emit-module-interface -c moduleTest.cpp -o moduleTest.pcm
LLD Link... ld.lld: error: /home/kassane/.cache/zig/o/ec639bf9d5e7c589c4e222dd65d28017/moduleTest.o:995: unclosed quote

Issue: llvm/llvm-project#61198

Hack fix (by: @vesim987)

$> cp $(zig c++ -std=c++20 -fmodules -fbuiltin-module-map -fmodules-ts -Xclang -emit-module-interface -c moduleTest.cpp -o moduleTest.pcm 2>&1 | grep "ld.lld: error" | sed 's/.*error: \(.*\):[0-9]\+.*/\1/g') moduleTest.pcm

Next steps

$> zig c++ -std=c++20 -fmodules -fbuiltin-module-map -c moduleTest.cpp
$> zig c++ -std=c++20 -fmodules -fbuiltin-module-map -fprebuilt-module-path=. -c main.cpp -o main.o
$> zig c++ -std=c++20 -fmodules -fbuiltin-module-map main.o moduleTest.o -o main

Output

$> ./main
retInt is: 42
@kassane
Copy link
Author

kassane commented Feb 19, 2023

zig c++ default link to LLVM libc++ (builtin).

How to use another C++ ABI?

Example: https://en.cppreference.com/w/cpp/algorithm/ranges/sort

#include <algorithm>
#include <array>
#include <functional>
#include <iomanip>
#include <iostream>
 
void print(auto comment, auto const& seq, char term = ' ') {
    for (std::cout << comment << '\n'; auto const& elem : seq)
        std::cout << elem << term;
    std::cout << '\n';
}
 
struct Particle {
    std::string name; double mass; // MeV
    template<class Os> friend
    Os& operator<< (Os& os, Particle const& p) {
        return os << std::left << std::setw(8) << p.name << " : " << p.mass << ' ';
    }
};
 
int main()
{
    std::array s {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
 
    namespace ranges = std::ranges;
 
    ranges::sort(s);
    print("Sort using the default operator<", s);
 
    ranges::sort(s, ranges::greater());
    print("Sort using a standard library compare function object", s);
 
    struct {
        bool operator()(int a, int b) const { return a < b; }
    } customLess;
    ranges::sort(s.begin(), s.end(), customLess);
    print("Sort using a custom function object", s);
 
    ranges::sort(s, [](int a, int b) { return a > b; });
    print("Sort using a lambda expression", s);
 
    Particle particles[] {
        {"Electron", 0.511}, {"Muon", 105.66}, {"Tau", 1776.86},
        {"Positron", 0.511}, {"Proton", 938.27}, {"Neutron", 939.57},
    };
    ranges::sort(particles, {}, &Particle::name);
    print("\nSort by name using a projection", particles, '\n');
    ranges::sort(particles, {}, &Particle::mass);
    print("Sort by mass using a projection", particles, '\n');
}

Linking to libc++ (LLVM 15) ABI

$>  zig c++ -Oz -std=c++20 ranges.cpp -lc++
ranges.cpp:27:13: error: no type named 'sort' in namespace 'std::ranges'
    ranges::sort(s);
    ~~~~~~~~^
ranges.cpp:30:13: error: no member named 'sort' in namespace 'std::ranges'
    ranges::sort(s, ranges::greater());
    ~~~~~~~~^
ranges.cpp:36:13: error: no member named 'sort' in namespace 'std::ranges'
    ranges::sort(s.begin(), s.end(), customLess);
    ~~~~~~~~^
ranges.cpp:39:13: error: no member named 'sort' in namespace 'std::ranges'
    ranges::sort(s, [](int a, int b) { return a > b; });
    ~~~~~~~~^
ranges.cpp:46:13: error: no member named 'sort' in namespace 'std::ranges'
    ranges::sort(particles, {}, &Particle::name);
    ~~~~~~~~^
ranges.cpp:48:13: error: no member named 'sort' in namespace 'std::ranges'
    ranges::sort(particles, {}, &Particle::mass);
    ~~~~~~~~^
6 errors generated.

Linking to libstdc++ (GNU) ABI

$> zig c++ -Oz -std=c++20 ranges.cpp /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 -I/usr/lib/gcc/x86_64-linux-gnu/11/include -I/usr/include/c++/11 -I/usr/include/x86_64-linux-gnu/c++/11 -nostdinc++ -nostdlib++ -o range_cpp
$> ./range_cpp
Sort using the default operator<
0 1 2 3 4 5 6 7 8 9
Sort using a standard library compare function object
9 8 7 6 5 4 3 2 1 0
Sort using a custom function object
0 1 2 3 4 5 6 7 8 9
Sort using a lambda expression
9 8 7 6 5 4 3 2 1 0

Sort by name using a projection
Electron : 0.511
Muon     : 105.66
Neutron  : 939.57
Positron : 0.511
Proton   : 938.27
Tau      : 1776.86

Sort by mass using a projection
Electron : 0.511
Positron : 0.511
Muon     : 105.66
Proton   : 938.27
Neutron  : 939.57
Tau      : 1776.86

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment