#Build and link a C++ library

1 messages · Page 1 of 1 (latest)

hexed hollow
#

Hey guys, I'm compiling a C++ static library using zig:

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

    const mayaDevUtils = b.addStaticLibrary(.{
        .name = "mayaDevUtils",
        .target = target,
        .optimize = mode});
    mayaDevUtils.linkLibCpp();
    mayaDevUtils.root_module.addCSourceFile(.{
        .file = b.path("MayaDevUtilities/MayaUtilities.cpp"),
    });

    b.installArtifact(mayaDevUtils);
}

This seems to work but I need to link it to some pre-compiled C++ libraries and add their include directories.
Is there any documentation for this?

silent granite
#

Look into linkSystemLibrary and addLibraryPath

ocean marlin
#

also Compile.addIncludePath

silent granite
#

Missed the part about include directories

ocean marlin
#

in general the build system changes a lot, personally I find it most useful to look at recent build.zigs on github (you can search with language:zig as a filter). the stdlib documentation is also very grokkable imo once you understand the general premise of build steps and how to link them together

hexed hollow
#

Have in mind that it is not a system library. It is an sdk.

ocean marlin
#

an sdk consists of system libraries, no?

hexed hollow
#

Let me try.

#

What I mean is that they are not libraries installed in the system or found in PATH.

ocean marlin
#

'system library' refers to dynamic/static libraries found using your system's path and linkage etc

#

you can use addLibraryPath to allow zig to find the directory they exist, and then linkSystemLibrary to link the library. this mirrors behavior in build systems in other systems languages. with projects built using zig, you can use linkLibrary and addImport which are a little less unwieldy

hexed hollow
#

If I do:

    mayaDevUtils.linkSystemLibrary("/path/to/libs/folder");

I get:

error: error: cannot use absolute path as a system library: /path/to/libs/folder
ocean marlin
#
mayaDevUtils.addLibraryPath("/path/to/libs/folder");
mayaDevUtils.linkSystemLibrary("maya"); // assuming it's `libmaya.so` or something like that
hexed hollow
#

I get error:

error: expected type 'Build.LazyPath', found '*const [20:0]u8'
    mayaDevUtils.addLibraryPath("/path/to/libs/folder");

Should it be:

    mayaDevUtils.addLibraryPath(.{ .src_path = "/path/to/libs/folder" });

?

#

Ok same error with both.

ocean marlin
#

whoops. you can use addLibraryPath(.{ .cwd_relative = "/absolute/path/to/folder" })

hexed hollow
#

It seems to work. Now I have to find a way to add the .h include dirs distributed with the library. And a way to link all the libraries from the lib folder (one by one?).

hexed hollow
#

It works HAHAHAHAHA

#

Is there a way to generate project files to load in an IDE or cmake is still the only way to do that?