C import failed for WolfSSL

After finding out that std TLS only supports up to v1.1 I decided to use WolfSSL instead, but ran into another issue:

const std = @import("std");

const c = @cImport({
    @cInclude("wolfssl/options.h");
    @cInclude("wolfssl/ssl.h");
});

pub fn main() !void {
    const version = c.wolfSSL_lib_version();

    std.debug.print("WolfSSL version: {s}\n", .{version});
}

Added to build.zig:

exe.root_module.linkSystemLibrary("c", .{});
exe.root_module.linkSystemLibrary("wolfssl", .{});
❯ zig version
0.14.0-dev.1939+816dfca0b
❯ zig build run -freference-trace
run
└─ run zig_wolfssl
   └─ zig build-exe zig_wolfssl Debug native 2 errors
/home/robert/Projects/toy/zig_wolfssl/.zig-cache/o/e3bfa70253faa95d7f8c8d9a9a35487b/cimport.zig:6043:24: error
: use of undeclared identifier 'struct_XSTAT'
pub const XSTAT_TYPE = struct_XSTAT;
                       ^~~~~~~~~~~~
src/main.zig:3:11: error: C import failed: AnalysisFail
const c = @cImport({
          ^~~~~~~~
referenced by:
    main: src/main.zig:9:21
    main: /home/robert/Projects/zigs/master/lib/std/start.zig:621:37
    comptime: /home/robert/Projects/zigs/master/lib/std/start.zig:59:30
    start: /home/robert/Projects/zigs/master/lib/std/std.zig:96:27
    comptime: /home/robert/Projects/zigs/master/lib/std/std.zig:155:9
error: the following command failed with 2 compilation errors:
/home/robert/Projects/zigs/master/zig build-exe -freference-trace=256 -lwolfssl -ODebug -Mroot=/home/robert/Projects/toy/zig_wolfssl/src/main.zig -lc --cache-dir /home/robert/Projects/toy/zig_wolfssl/.zig-cache --global-cache-dir /home/robert/.cache/zig --name zig_wolfssl --zig-lib-dir /home/robert/Projects/zigs/master/lib/ --listen=-
Build Summary: 2/7 steps succeeded; 1 failed
run transitive failure
└─ run zig_wolfssl transitive failure
   ├─ zig build-exe zig_wolfssl Debug native 2 errors
   └─ install transitive failure
      └─ install zig_wolfssl transitive failure
         └─ zig build-exe zig_wolfssl Debug native (reused)
error: the following build command failed with exit code 1:
/home/robert/Projects/toy/zig_wolfssl/.zig-cache/o/32098adf2fbbdf4f9abe2155063ae0a0/build /home/robert/Projects/zigs/master/zig /home/robert/Projects/zigs/master/lib /home/robert/Projects/toy/zig_wolfssl /home/robert/Projects/toy/zig_wolfssl/.zig-cache /home/robert/.cache/zig --seed 0x90121f30 -Z9440478479d7c46b run -freference-trace

As a hack I found that changing .zig-cache/o/e3bfa70253faa95d7f8c8d9a9a35487b/cimport.zig from

pub const XSTAT_TYPE = struct_XSTAT;

To

pub const XSTAT_TYPE = c_int; // Or any other type...

makes this program work, but I doubt that this is the right approach.
I had no trouble using curl from Zig.
C interop is an area of Zig I’m very unfamiliar with. Am I holding it wrong?

from How do I manage the build configuration of wolfSSL? – wolfSSL
try

const c = @cImport({
    @cInclude("wolfssl/options.h");
    @cInclude("wolfssl/wolfcrypt/settings.h");
    @cInclude("wolfssl/ssl.h");
});

I have tried including most (if not all) wolfssl header files, it didn’t change anything.

It is a 0.13.0 bug (does not correctly expand XSTAT in XSTAT_TYPE define).
It is fixed in master.

        #define XSTAT       stat
    #ifndef XSTAT_TYPE
        #define XSTAT_TYPE struct XSTAT
    #endif

translates to:

pub const XSTAT = stat;
pub const XSTAT_TYPE = struct_XSTAT;

Try this for workaround:

const c = @cImport({
    @cDefine("XSTAT_TYPE", "struct stat");
    @cInclude("wolfssl/ssl.h");
});
2 Likes

I am using master.
However, adding @cDefine("XSTAT_TYPE", "struct stat"); does solve the issue.
Ευχαριστώ!

1 Like

Added issue #21746 in zig github.

2 Likes