I am working on converting my C++ project to use the zig build system as a part of this effort I am converting the yaml-cpp build system to zig as well.
I have run into an issue where if I build yaml-cpp source files directly as a part of my executable I have no issue running my code as expected. However, when I build it as a dynamic library and try to link to it I have a malloc error. I also have no issue if I change the linkage type for the library to static instead of dynamic.
main(88451,0x20c492140) malloc: *** error for object 0x102e384b8: pointer being freed was not allocated
main(88451,0x20c492140) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort ./zig-out/bin/main
This is the version of my build script which adds everything to the executable directly
const std = @import("std");
const zcc = @import("compile_commands");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{ .name = "main", .root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}) });
exe.addIncludePath(b.path("include"));
exe.addIncludePath(b.path("include/inputs"));
exe.addIncludePath(b.path("yaml-cpp-0.8.0/include"));
exe.addCSourceFiles(.{
.files = &.{
"main.C",
"src/InputErrorHelper.C",
"src/InputParameters.C",
"src/TypeNameHelper.C",
"src/Parameter.C",
"yaml-cpp-0.8.0/src/binary.cpp",
"yaml-cpp-0.8.0/src/convert.cpp",
"yaml-cpp-0.8.0/src/depthguard.cpp",
"yaml-cpp-0.8.0/src/directives.cpp",
"yaml-cpp-0.8.0/src/emit.cpp",
"yaml-cpp-0.8.0/src/emitfromevents.cpp",
"yaml-cpp-0.8.0/src/emitter.cpp",
"yaml-cpp-0.8.0/src/emitterstate.cpp",
"yaml-cpp-0.8.0/src/emitterutils.cpp",
"yaml-cpp-0.8.0/src/exceptions.cpp",
"yaml-cpp-0.8.0/src/exp.cpp",
"yaml-cpp-0.8.0/src/memory.cpp",
"yaml-cpp-0.8.0/src/node_data.cpp",
"yaml-cpp-0.8.0/src/node.cpp",
"yaml-cpp-0.8.0/src/nodebuilder.cpp",
"yaml-cpp-0.8.0/src/nodeevents.cpp",
"yaml-cpp-0.8.0/src/null.cpp",
"yaml-cpp-0.8.0/src/ostream_wrapper.cpp",
"yaml-cpp-0.8.0/src/parse.cpp",
"yaml-cpp-0.8.0/src/parser.cpp",
"yaml-cpp-0.8.0/src/regex_yaml.cpp",
"yaml-cpp-0.8.0/src/scanner.cpp",
"yaml-cpp-0.8.0/src/scanscalar.cpp",
"yaml-cpp-0.8.0/src/scantag.cpp",
"yaml-cpp-0.8.0/src/scantoken.cpp",
"yaml-cpp-0.8.0/src/simplekey.cpp",
"yaml-cpp-0.8.0/src/singledocparser.cpp",
"yaml-cpp-0.8.0/src/stream.cpp",
"yaml-cpp-0.8.0/src/tag.cpp",
},
.flags = &.{
"-std=c++17",
"-g",
},
});
exe.linkLibCpp();
b.installArtifact(exe);
}
and this is the version with the dynamic library
const std = @import("std");
const zcc = @import("compile_commands");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const yaml_cpp = b.addLibrary(.{ .name = "yaml-cpp", .linkage = .dynamic, .version = .{ .major = 0, .minor = 8, .patch = 0 }, .root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}) });
yaml_cpp.addIncludePath(b.path("yaml-cpp-0.8.0/include"));
yaml_cpp.linkLibCpp();
yaml_cpp.addCSourceFiles(.{
.files = &.{
"yaml-cpp-0.8.0/src/binary.cpp",
"yaml-cpp-0.8.0/src/convert.cpp",
"yaml-cpp-0.8.0/src/depthguard.cpp",
"yaml-cpp-0.8.0/src/directives.cpp",
"yaml-cpp-0.8.0/src/emit.cpp",
"yaml-cpp-0.8.0/src/emitfromevents.cpp",
"yaml-cpp-0.8.0/src/emitter.cpp",
"yaml-cpp-0.8.0/src/emitterstate.cpp",
"yaml-cpp-0.8.0/src/emitterutils.cpp",
"yaml-cpp-0.8.0/src/exceptions.cpp",
"yaml-cpp-0.8.0/src/exp.cpp",
"yaml-cpp-0.8.0/src/memory.cpp",
"yaml-cpp-0.8.0/src/node_data.cpp",
"yaml-cpp-0.8.0/src/node.cpp",
"yaml-cpp-0.8.0/src/nodebuilder.cpp",
"yaml-cpp-0.8.0/src/nodeevents.cpp",
"yaml-cpp-0.8.0/src/null.cpp",
"yaml-cpp-0.8.0/src/ostream_wrapper.cpp",
"yaml-cpp-0.8.0/src/parse.cpp",
"yaml-cpp-0.8.0/src/parser.cpp",
"yaml-cpp-0.8.0/src/regex_yaml.cpp",
"yaml-cpp-0.8.0/src/scanner.cpp",
"yaml-cpp-0.8.0/src/scanscalar.cpp",
"yaml-cpp-0.8.0/src/scantag.cpp",
"yaml-cpp-0.8.0/src/scantoken.cpp",
"yaml-cpp-0.8.0/src/simplekey.cpp",
"yaml-cpp-0.8.0/src/singledocparser.cpp",
"yaml-cpp-0.8.0/src/stream.cpp",
"yaml-cpp-0.8.0/src/tag.cpp",
},
.flags = &.{
"-std=c++17",
"-g",
},
});
const exe = b.addExecutable(.{ .name = "main", .root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}) });
exe.addIncludePath(b.path("include"));
exe.addIncludePath(b.path("include/inputs"));
exe.addIncludePath(b.path("yaml-cpp-0.8.0/include"));
exe.addCSourceFiles(.{
.files = &.{
"main.C",
"src/InputErrorHelper.C",
"src/InputParameters.C",
"src/TypeNameHelper.C",
"src/Parameter.C",
},
.flags = &.{
"-std=c++17",
"-g",
},
});
exe.linkLibrary(yaml_cpp);
exe.linkLibCpp();
b.installArtifact(exe);
b.installArtifact(yaml_cpp);
}
All the source code for this is available here https://github.com/gsgall/prism/tree/rewrite/inputs
I am using zig version 0.16.0-dev.368+2a97e0af6 on an apple silicon laptop.
I have been stuck on this for quite a while so if anyone has some insight as to what the issue might be I would really appreciate it!