#Zig Use Single .h File

1 messages · Page 1 of 1 (latest)

mint pivot
#

I have been attempting to get a version of ENet working so I can use it in some Zig projects. (https://github.com/zpl-c/enet for reference). I did attempt to use the main version (https://github.com/lsalzman/enet) but I ended up with similar problems.

In after I build and compile it I end up with the error of raw_enet.zig:1:1: error: expected type expression, found \'invalid token\' ■

This is, unideal. I have tried using both zig translate-c and just writing my own pub extern fns in it's own file. Both give apparently the same error. I have even tried making a tiny '.c' file that does nothing but have an include statement of the header.

What I would like is to be able to use ENet. (Also the only one that I have found that has been wrapped for zig was last updated ~3 years ago.). I would also like to understand what I am doing wrong. The move I dive into the build system the more it feels like a black box. Thank you for your time and your help.

GitHub

⚡️ ENet reliable UDP networking library . Contribute to zpl-c/enet development by creating an account on GitHub.

GitHub

ENet reliable UDP networking library . Contribute to lsalzman/enet development by creating an account on GitHub.

#

My Build file :


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

    const c_enet = b.addStaticLibrary(.{
        .target = target,
        .optimize = optimize,
        .link_libc = true,
        .name = "c_enet",
    });


    c_enet.linkLibC();
    c_enet.addIncludePath(b.path("enet/include"));
    c_enet.addCSourceFiles(
        .{.files = &[_][]const u8 {
            //"c_enet.c",
            "enet/include/enet.h",
        }},
    );
    b.installArtifact(c_enet);

    const raw_enet = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("raw_enet.zig"),
        .link_libc = true,
    });
    raw_enet.linkLibrary(c_enet);



    const exe_module = b.createModule(.{
        .root_source_file = b.path("enet.zig"),
        .target = target,
        .optimize = optimize,
    });

    const exe = b.addExecutable(.{
        .name = "enet",
        .root_module = exe_module,
    });


    //exe.linkLibrary(c_enet);
    exe.root_module.addImport("raw_enet", raw_enet);
    b.installArtifact(exe);

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

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app.");
    run_step.dependOn(&run_cmd.step);
}```
#

raw_enet.zig :

pub extern fn enet_deinitialize() void;```

enet.zig : 
```const std = @import("std");
const raw_enet = @import("raw_enet");

const print = std.debug.print;

pub fn main() !void {
    const res = raw_enet.enet_initialize();
    print("{}\n", .{res});
}```

c_enet.c : 
`#include "enet.h"`
hollow saffron
#

if it's getting an invalid token at line 1 character 1 in the zig file, I wonder if this is an issue with the raw_enet.zig file having some sort of byte order mark

#

and not really an issue with the API or library at all

mint pivot
#

I will copy and paste the text of raw_enet.zig into a new file and see if that does anything, but IDK

hollow saffron
#

you said it's AFTER you compile. that seems like an odd error to see while running your program, I'd think

#

I'm a little too use to 0.15.0 changes to how build.zig works, so it's gonna take me a second to figure out the correct approach with zig 0.14.1

#

I wonder if addImport is the right method tho. checking.

mint pivot
#

This is the build summary :

#
run transitive failure
└─ run enet transitive failure
   ├─ zig build-exe enet Debug native 1 errors
   └─ install transitive failure
      └─ install enet transitive failure
         └─ zig build-exe enet Debug native (+3 more reused dependencies)```
hollow saffron
#

addImport is correct

#

yeah I'd check the file in a hexeditor, and make sure the first character isn't a BOM

#

but I already have one installed, so that's easier for me

#

if you want to drop raw_enet.zig here I can check it

mint pivot
#

Alr

#

I am also downloading the latest dev; if that would be easier to debug.

#

Also, thank you for your help. I appreciate it

hollow saffron
#

yeah it's got a BOM

mint pivot
#

Okay, can I ask what that is?

hollow saffron
#

it's extra bytes at the beginning of the file that tell programs what order to read the bytes when using multibyte character sets

#

kinda some old junk, now that most things use utf-8

#

you may want to use a different code editor

#

here it is without the BOM

mint pivot
#

Wait-wait wait. My problem is that I am using VS-Code?

#

LOL

hollow saffron
#

vs code shouldn't cause problems. I don't know why it's doing that

#

I was suspecting like notepad or wordpad or something

#

let me know if that file gets you past the issue

mint pivot
#

It does; I now am getting : error: lld-link: undefined symbol: enet_initialize

hollow saffron
#

linker error

#

you didn't actually link the library, you just effectlively imported a .h file

#

addStaticLibrary just creates a new, empty library. the fact that you named it c_enet doesn't make it import that library

#

a couple options:

  • get a path to a precompiled .lib for the library, and link that to your exe in your build.zig
  • add all the .c source files to that lib, in addition to that .h file, so that zig compiles the C code and puts it in that new empty static library you created in your build.zig
mint pivot
#

It is just a header file

hollow saffron
#

is it? I know nothing about it 😄

#

maybe you need to define #define ENET_IMPLEMENTATION or something like that

#

if it's a single-header lib

#

the references I find to an enet library all look like they have .c files as well

#

oh, a different one seems to be single-header

mint pivot
#

I am using a modern iteration. Yeah that one

#

I did try using the other one but I was running into something weird.

hollow saffron
#

at the top of the .h file, it says you need to #define ENET_IMPLEMENTATION

#

nice. predicted it hehe

#

so, there's a couple ways to do that:

mint pivot
#

Okay

#

I made a tiny c-file

hollow saffron
#
  • create your own .c file and compile THAT in your lib, instead of the .h file. it will have JUST #define ENET_IMPLEMENTATION and #include enet.h
  • call the appropriate method to add the #define for your c source files. looking up that call now...
mint pivot
#

that just has

#include "enet.h"```
hollow saffron
#

yeah

mint pivot
#

Oh; good I did it right

#

that's new

hollow saffron
#

there's an alternative option I can look up if your prefer, but sometimes I just do the .c file

mint pivot
#

unfortunately I am getting this error : error: lld-link: undefined symbol: __declspec(dllimport) WSAStartup note: referenced by C:\Users\default.LAPTOP-4MN3KEP0\Desktop\code\Zig\Testing\Bind_ZPL_Enet_take2\enet\include\enet.h:5881 note: c_enet.lib(c_enet.obj):(enet_initialize) error: lld-link: undefined symbol: __declspec(dllimport) WSACleanup note: referenced by ...\Bind_ZPL_Enet_take2\enet\include\enet.h:5886 note: c_enet.lib(c_enet.obj):(enet_initialize) note: referenced by ...\Bind_ZPL_Enet_take2\enet\include\enet.h:5896 note: c_enet.lib(c_enet.obj):(enet_deinitialize) error: lld-link: undefined symbol: __declspec(dllimport) timeBeginPeriod note: referenced by ...\Bind_ZPL_Enet_take2\enet\include\enet.h:5890 note: c_enet.lib(c_enet.obj):(enet_initialize) error: lld-link: undefined symbol: __declspec(dllimport) htonl note: referenced by ...\Bind_ZPL_Enet_take2\enet\include\enet.h:1557 note: c_enet.lib(c_enet.obj):(enet_crc32) note: referenced by ...\Bind_ZPL_Enet_take2\enet\include\enet.h:5020 note: c_enet.lib(c_enet.obj):(enet_host_bandwidth_throttle) note: referenced by ...\Bind_ZPL_Enet_take2\enet\include\enet.h:5023 note: c_enet.lib(c_enet.obj):(enet_host_bandwidth_throttle) note: referenced 27 more times

#

So maybe that would help

hollow saffron
#

those look like windows APIs I think

#

I don't remember which of the main 4 DLLs has which windows APIs in it

#

but sounds like you'll need to link that

#

WSAStartup is winsock, not sure which DLL that's in

#

timeBeginPeriod is in Winmm.lib

#

and that's how I figure out which DLL each windows API is in

#

so yeah, sounds like you need to link those. I haven't done that yet, so not sure how to do that. probably isn't difficult tho

mint pivot
#

Okay I will look into that. Thank you

#

I think I will call it quits for the time being and I will take a look at this later. I appreciate all the help

hollow saffron
#

I guess it's as simple as exe.linkSystemLibrary("ws2_32") etc.
https://ziggit.dev/t/msvc-linking-woes/9475/3

mint pivot
#

Okay I think it is working now

#

To any unfortunate soul who has the same problem I had to link c_enet.linkSystemLibrary("ws2_32"); c_enet.linkSystemLibrary("winmm");

mint pivot
mint pivot
hollow saffron
#

imhex is free, it's not bad

mint pivot
hollow saffron
#

select the first two characters and do file -> remove. I think you might be able to use backspace too, not sure

#

if it doesn't say FFEF or whatever, it's not a BOM

mint pivot
#

Again, I am endebted

mint pivot
#

Nevermi- y'know I think I'll try to figure this one out on my own. It seems to be inconsistent.

hollow saffron
#

maybe it's coming from your .h file

mint pivot
#

Any idea how I might fix that?

#

The error is now : "raw_enet.zig:1:2: error: expected ',' after field
p"

#

but you have already helped me so much. If you want to do something else, I get it

hollow saffron
#

if it's coming from the .h file, try opening the .h file in the hex editor

#

I'm playing metal gear solid rn heh

mint pivot
#

The 1st 3 Bytes are 2F 2A 2A

mint pivot
hollow saffron
#

it shows on the right what those correspond to

#

/** sounds like it starts with a comment, and not a BOM

#

so that's not it

#

dunno why it's inserting a BOM

#

I wonder if you have some weird plugin or vscode setting that is saving a BOM automatically somehow

#

dunno what that would be tho

mint pivot
#

That worked

mint pivot
# hollow saffron it shows on the right what those correspond to

Hey, I just wanted to say that I found that ZPL wasn't the version that I was looking for ( I am trying to follow along with a tutorial and I ran into some issues ) so I switched back to the base version and, by looking back at the help you have given here I managed to get that working (I had another BOM from the translate Zig-file), so, in short, I just wanted to say thank you again for your help. I really appreciate it.