I have been trying to use GitHub - open62541/open62541: Open source implementation of OPC UA (OPC Unified Architecture) aka IEC 62541 licensed under Mozilla Public License v2.0 · GitHub using translate-C. So far, I can build an example with debug build. However, I never successfully build it with any release mode, it just never finished, stuck in linking the c files.
Here is zig build that I am using
const std = @import("std");
const Translator = @import("translate_c").Translator;
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const translate_c = b.dependency("translate_c", .{});
const open62541 = buildLibOpen62541(b, target, optimize);
const trans_open62541: Translator = .init(translate_c, .{
.c_source_file = b.addWriteFiles().add("c.h",
\\#include <open62541.h>
),
.target = target,
.optimize = optimize,
});
trans_open62541.linkLibrary(open62541);
const build_zig_zon = b.createModule(.{
.root_source_file = b.path("build.zig.zon"),
.target = target,
.optimize = optimize,
});
const mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "build.zig.zon", .module = build_zig_zon },
.{ .name = "open62541", .module = trans_open62541.mod },
},
});
const exe = b.addExecutable(.{ .name = "test", .root_module = mod });
b.installArtifact(exe);
}
fn buildLibOpen62541(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) *std.Build.Step.Compile {
const mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
mod.addCSourceFiles(.{
.root = b.path("vendor"),
.files = &.{"open62541.c"},
});
// mod.addCMacro("UA_LOGLEVEL", "500");
const lib = b.addLibrary(.{
.name = "open62541",
.root_module = mod,
});
// Install the headers, so that linking this library makes those headers available.
lib.installHeader(b.path("vendor/open62541.h"), "open62541.h");
return lib;
}
Both the amalgamated .h and .c files can be found on the release of the repo that I post here.
Here is the simple example file that I successfully build with debug mode
const std = @import("std");
const open62541 = @import("open62541");
pub fn main(init: std.process.Init) !void {
_ = init;
const server = open62541.UA_Server_new();
defer _ = open62541.UA_Server_delete(server);
const code = open62541.UA_Server_runUntilInterrupt(server);
if (open62541.UA_StatusCode_isGood(code) == false) {
std.log.err("{s}", .{open62541.UA_StatusCode_name(code)});
}
}