#Failing to build pg.zig due to 'openssl/ssl.h' or '/opt/openssl/lib' not found

1 messages · Page 1 of 1 (latest)

chilly granite
#

Here is the project files:

// main.zig
const std = @import("std");
const pg = @import("pg");

pub fn main() !void {
    var pool = try pg.Pool.init(std.heap.c_allocator, .{ .size = 5, .connect = .{
        .port = 5432,
        .host = "...",
    }, .auth = .{
        .username = "postgres",
        .database = "...",
        .password = "...",
        .timeout = 10_000,
    } });
    defer pool.deinit();

    var result = try pool.query("SELECT tag from params_table LIMIT 100", .{});
    defer result.deinit();

    while (try result.next()) |row| {
        const tag_name = row.get([]u8, 0);
        std.debug.print("tag: {s}\n", .{tag_name});
    }
}
// build.zig
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "test_pg",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "pg", .module = b.dependency("pg", .{}).module("pg") },
            },
        }),
    });

    b.installArtifact(exe);

    const run_step = b.step("run", "Run the app");

    const run_cmd = b.addRunArtifact(exe);
    run_step.dependOn(&run_cmd.step);
    run_cmd.step.dependOn(b.getInstallStep());
}

The example runs on native build but with an error during the build (how is that possible btw?):

$ zig build run
run
└─ run exe test_pg
   └─ install
      └─ install test_pg
         └─ compile exe test_pg Debug native failure
error: warning: unable to open library directory '/opt/openssl/lib': FileNotFound

tag: ... (correct output)

When I cross-compile for arm-linux-gnueabihf, I get (see next message):

#
install
└─ install webc-zig
   └─ compile exe webc-zig ReleaseSmall arm-linux-gnueabihf.2.31 2 errors
/home/timfayz/.cache/zig/p/pg-0.0.0-Wp_7gfUEBgDCwOFBDNEOe_YeB4WpffYPgk3reelcCtbH/src/lib.zig:4:21: error: C import failed
pub const openssl = @cImport({
                    ^~~~~~~~
referenced by:
    SSLCtx: /home/timfayz/.cache/zig/p/pg-0.0.0-Wp_7gfUEBgDCwOFBDNEOe_YeB4WpffYPgk3reelcCtbH/src/lib.zig:22:37
    SSLCtx: /home/timfayz/.cache/zig/p/pg-0.0.0-Wp_7gfUEBgDCwOFBDNEOe_YeB4WpffYPgk3reelcCtbH/src/conn.zig:9:19
    15 reference(s) hidden; use '-freference-trace=17' to see all references
.zig-cache/o/bbb642955c71afa010ec993af423a33f/cimport.h:1:10: error: 'openssl/ssl.h' file not found
#include <openssl/ssl.h>
         ^
error: warning: unable to open library directory '/opt/openssl/lib': FileNotFound

error: the following command failed with 2 compilation errors:
candid vector
#

It depends on system openssl, you need to either provide rootfs that has arm install of openssl or modify pg.zig so it can build openssl from a source