#Linking libc with zig test

1 messages · Page 1 of 1 (latest)

frank glacier
#

I remember being able to do this with -lc
But this option no longer exist.

potent garden
#

the option still exits, both in 0.11 and the latest master build

#
$> cat ctest.zig
const c = @cImport(@cInclude("stdio.h"));

test "needs C" {
    _ = c.puts("Hello from libc");
}
$> zig test ctest.zig -lc
Test [1/1] test.needs C... Hello from libc
All 1 tests passed.
frank glacier
potent garden
#

can you send the error message and the command your running?

frank glacier
# potent garden can you send the error message and the command your running?
pub extern "c" fn dlopen(path: [*:0]const u8, mode: c_int) ?*anyopaque;
           ^~~
referenced by:
    loadPosixModule: src/platform/linux/xorg/module.zig:9:16
    loadXExtensions: src/platform/linux/xorg/global.zig:108:39
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/imad/Programs/zig/lib/std/mem.zig:948:21: error: invalid type given to std.mem.len: *[:0]u8
            else => @compileError("invalid type given to std.mem.len: " ++ @typeName(@TypeOf(value))),
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/imad/Programs/zig/lib/std/c.zig:389:12: error: dependency on libc must be explicitly specified in the build command
pub extern "c" fn dlclose(handle: *anyopaque) c_int;
           ^~~
done.
#

the command i ran: zig test src/platform/linux/xorg/monitor_impl.zig -lX11 -lc

potent garden
#

whats your zig version? you can check by running zig version @frank glacier

frank glacier
potent garden
frank glacier
# potent garden can you send your code? btw it looks like you've got another unrelated error the...

Still haven’t pushed the changes but this is the current code ```

const std = @import("std");
const libc = std.c;

extern "c" fn dlerror() ?[*:0]const u8;

pub inline fn loadPosixModule(module_path: [:0]const u8) ?*anyopaque {
const RTLD_LAZY = @as(c_int, 0x00001);
const RTLD_LOCAL = @as(c_int, 0);
return libc.dlopen(module_path.ptr, RTLD_LAZY | RTLD_LOCAL);
}

pub inline fn moduleError() [*:0]const u8 {
return dlerror().?;
}

pub inline fn freePosixModule(module_handle: *anyopaque) void {
_ = libc.dlclose(module_handle);
}

pub inline fn getModuleSymbol(module_handle: *anyopaque, symbol_name: [:0]const u8) ?*anyopaque {
return libc.dlsym(module_handle, symbol_name.ptr);
}

// pub fn getProcessHandle() ?*anyopaque {
// }

test "Loading and freeing win32 libraries" {
const testing = std.testing;
const module = loadPosixModule("libXrandr.so.2");
try testing.expect(@intFromPtr(module) != 0);
const symbol = getModuleSymbol(module.?, "XRRGetScreenResourcesCurrent");
try testing.expect(@intFromPtr(symbol) != 0);
freePosixModule(module.?);
}

potent garden
#

could you add the two missing ` at the start?

potent garden
#

one more then its perfect pregfrog

frank glacier
potent garden
#

np, but the error is weird

#

with -lc it should work