# Linking to compiled static library provided by dependency

**URL:** <https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597>\
**Category:** Help\
**Tags:** build-system\
**Created:** [October 30, 2024, 5:32am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597 "2024-10-30T05:32:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [October 30, 2024, 5:32am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/1 "2024-10-30T05:32:04Z")

</div>

I am trying to depend on npcap [https://npcap.com/](https://npcap.com/)

which provides an sdk with only headers in it (the user is expected to install npcap themselves) [Npcap: Windows Packet Capture Library & Driver](https://npcap.com/#download)

I want to create a zig package for it so I can depend on it like a regular zig dependency.

Can someone point me to an existing example library or documentation that does something similar?

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [October 30, 2024, 5:41am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/2 "2024-10-30T05:41:48Z")

</div>

Found this example, which is think is what I need:

> **[GitHub - allyourcodebase/zlib: zlib ported to the zig build system](https://github.com/allyourcodebase/zlib)**
>
> zlib ported to the zig build system

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [October 30, 2024, 5:58am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/3 "2024-10-30T05:58:39Z")

</div>

I’ve gotten this far:

```zig
const std = @import("std");

pub fn build(b: *std.Build) void {
    const upstream = b.dependency("npcap_sdk", .{});
    const lib = b.addStaticLibrary(.{
        .name = "npcap",
        .target = b.standardTargetOptions(.{}),
        .optimize = b.standardOptimizeOption(.{}),
    });
    lib.installHeadersDirectory(upstream.path("include"), "", .{});
    b.installArtifact(lib);
}

```

but I think I need to add the pre-compiled DLLs from the SDK somewhere?

```zig
jeff@jeff-debian:~/repos/npcap-zig$ zig build
install
└─ install npcap
   └─ zig build-lib npcap Debug native failure
error: the linker needs one or more objects to link
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install npcap transitive failure
   └─ zig build-lib npcap Debug native failure

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [October 30, 2024, 6:18am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/4 "2024-10-30T06:18:10Z")

</div>

well this builds but I think I should probably attempt a full build of npcap instead of relying on its published sdk?

```zig
const std = @import("std");

pub fn build(b: *std.Build) void {
    const upstream = b.dependency("npcap_sdk", .{});
    const lib = b.addStaticLibrary(.{
        .name = "npcap",
        .target = b.standardTargetOptions(.{}),
        .optimize = b.standardOptimizeOption(.{}),
    });
    lib.addObjectFile(upstream.path("Lib/wpcap.lib"));
    lib.addObjectFile(upstream.path("Lib/Packet.lib"));
    lib.installHeadersDirectory(upstream.path("Include"), "", .{});
    b.installArtifact(lib);
}

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [November 6, 2024, 6:14am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/5 "2024-11-06T06:14:25Z")

</div>

I gave up on compiling npcap from source so i will just use the pre-compiled SDK (has some .libs and some headers).

Is there a way to include this `.lib` into my compiled executable? Also, I don’t know how to do this.

build.zig.zon

```zig
.{
    .name = "gatorcat",
    .version = "0.0.0",
    .minimum_zig_version = "0.13.0",
    .dependencies = .{
        .flags = .{
            .url = "https://github.com/n0s4/flags/archive/refs/tags/v0.10.0.tar.gz",
            .hash = "12200d3243c5919d6ae3254f991bdf95956f349ccd6d3463f9beb5454d300a0f1e64",
        },
        .npcap_sdk = .{
            .url = "https://npcap.com/dist/npcap-sdk-1.13.zip",
            .hash = "12206001b370bb70f9dc47c846df6ad82725e4491650472dc7037c5e027792ba207e",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
        "LICENSE",
        "example",
    },
}

```

build zig

```zig
// windows npcap dependency
    const npcap_sdk = b.dependency("npcap_sdk", .{
        .target = target,
        .optimize = optimize,
    });
    const lib = b.addStaticLibrary(.{
        .name = "npcap",
        .target = target,
        .optimize = optimize,
    });
    lib.addObjectFile(upstream_sdk.path("Lib/wpcap.lib"));
    lib.addObjectFile(upstream_sdk.path("Lib/Packet.lib"));
    lib.installHeadersDirectory(upstream_sdk.path("Include"), "", .{});
    b.installArtifact(lib);

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [November 6, 2024, 6:30am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/6 "2024-11-06T06:30:32Z")

</div>

Ok i think I’m pretty close with this, zls is now giving me autocomplete

```zig
// CLI tool
    const cli_tool = b.addExecutable(.{
        .name = "gatorcat",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    cli_tool.root_module.addImport("gatorcat", gatorcat_module);

    // CLI tool dependencies
    const flags = b.dependency("flags", .{
        .target = target,
        .optimize = optimize,
    });
    cli_tool.root_module.addImport("flags", flags.module("flags"));
    b.installArtifact(cli_tool);

    const npcap_sdk = b.dependency("npcap_sdk", .{
        .target = target,
        .optimize = optimize,
    });
    cli_tool.addObjectFile(npcap_sdk.path("Lib/wpcap.lib"));
    cli_tool.addObjectFile(npcap_sdk.path("Lib/Packet.lib"));
    cli_tool.addIncludePath(npcap_sdk.path("Include"));

```

```zig
const npcap = @cImport({
    @cInclude("pcap/pcap.h");
});

pub const WindowsRawSocket = struct {

    pub fn init() WindowsRawSocket {
        npcap.
    }
};

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [November 6, 2024, 7:30am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/7 "2024-11-06T07:30:31Z")

</div>

got everything working, but forgot to link libc, and I will have to refactor this later to make it a conditional dependency to only occur on windows. Not sure how to do that, but that is a separate issue.

```zig
// CLI tool
  const cli_tool = b.addExecutable(.{
      .name = "gatorcat",
      .root_source_file = b.path("src/main.zig"),
      .target = target,
      .optimize = optimize,
  });
  cli_tool.root_module.addImport("gatorcat", gatorcat_module);

  // CLI tool dependencies
  const flags = b.dependency("flags", .{
      .target = target,
      .optimize = optimize,
  });
  cli_tool.root_module.addImport("flags", flags.module("flags"));
  b.installArtifact(cli_tool);
  cli_tool.addObjectFile(npcap_sdk.path("Lib/wpcap.lib"));
  cli_tool.addObjectFile(npcap_sdk.path("Lib/Packet.lib"));
  cli_tool.addIncludePath(npcap_sdk.path("Include"));
  cli_tool.linkLibC();

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [November 6, 2024, 8:02am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/8 "2024-11-06T08:02:09Z")

</div>

nevermind, its not actually linking yet

```zig
error: lld-link: undefined symbol: pcap_open
    note: referenced by /home/jeff/repos/gatorcat/src/nic.zig:388
    note: /home/jeff/repos/gatorcat/.zig-cache/o/0fb452cb5eb5abf34acdbe0b3ac8c9c5/gatorcat.exe.obj:(nic.WindowsRawSocket.init)

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [November 7, 2024, 5:52am UTC](https://ziggit.dev/t/linking-to-compiled-static-library-provided-by-dependency/6597/9 "2024-11-07T05:52:42Z")

</div>

I was close, my target was `x86_64` so I needed to link against the 64-bit versions of the DLLs.

here is the full solution, for a project that is depending on pre-compiled DLLs. Note that it should eventually contain logic to select the right DLL’s based on the target architecture.

build.zig.zon

```zig
.{
    .name = "gatorcat",
    .version = "0.0.0",
    .minimum_zig_version = "0.13.0",
    .dependencies = .{
        .flags = .{
            .url = "https://github.com/n0s4/flags/archive/refs/tags/v0.10.0.tar.gz",
            .hash = "12200d3243c5919d6ae3254f991bdf95956f349ccd6d3463f9beb5454d300a0f1e64",
        },
        .npcap_sdk = .{
            .url = "https://npcap.com/dist/npcap-sdk-1.13.zip",
            .hash = "12206001b370bb70f9dc47c846df6ad82725e4491650472dc7037c5e027792ba207e",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
        "LICENSE",
        "example",
    },
}

```

build.zig

```zig
// windows dependency
    const npcap_sdk = b.dependency("npcap_sdk", .{
        .target = target,
        .optimize = optimize,
    });
// CLI tool dependencies
    const flags = b.dependency("flags", .{
        .target = target,
        .optimize = optimize,
    });
    cli_tool.root_module.addImport("flags", flags.module("flags"));
    b.installArtifact(cli_tool);
    cli_tool.addObjectFile(npcap_sdk.path("Lib/x64/wpcap.lib"));
    cli_tool.addObjectFile(npcap_sdk.path("Lib/x64/Packet.lib"));
    cli_tool.addIncludePath(npcap_sdk.path("Include"));
    cli_tool.linkLibC();

```
