#How to use Cryptolens C library in Zig

1 messages · Page 1 of 1 (latest)

cosmic lance
#

I have the Cryptolens.lib file.

#

Okay i got this so far

#

lib.addLibraryPath(.{ .path = "include/cryptolens" });
lib.linkSystemLibrary("Cryptolens");

Building fine, but how can i access the library from my code?

#

do i need more than just the lib file

cosmic lance
#

well, i give up.

lament nova
#

If you want to include the headers vis @cImport then you need to tell the build system where the header files are via addIncludePath(.{ .path = "include/cryptolens" }).

cosmic lance
#

i did that

lament nova
#

Oh, well what is your full build.zig then? You probably don't want addLibraryPath to point at include/cryptolens unless you also put the library file there.

cosmic lance
#

that’s where I put the Cryptolens.lib. yes

#

its mixed in with the headers

cosmic lance
# cosmic lance Fail

Think it has something to do with this error. I don’t think it’s me doing something wrong rather than me simply not doing something required

#

can anyone reproduce in a sandbox project? its a relatively simple lib to include

lament nova
#

Where did the library file come from? Just restating the error, but it seems that a COFF file is expected and you have something else. I'm not a windows person though.

cosmic lance
#

Opened the solution in VS and built

#

It gave me the Cryptolens.lib file output.

#

The library is kind of strangely designed honestly

lament nova
#

I believe that zig by default uses x86_64-windows-gnu by default, maybe try the msvc target? E.g. use -Dtarget=x86_64-windows-msvc

cosmic lance
#

It still wont compile..

cosmic lance
#

Bunp please..

steep saffron
#

you can see what files you need to build it in the CMakeLists

cosmic lance
#

the origin of this issue is probably me not knowing anything about building C/C++ projects at all

#

CMake, gcc....

#

zig was really my intro to low level programming

#

I'll check that file

steep saffron
#

oh then this is not a trivial thing to learn

#

imo build systems are just difficult to learn. Only just starting to get a mental understand of how the zig one works

#

i could probably throw together a build.zig that builds this

#

but the problem is this ->
crypto ssl curl

#

target_link_libraries (cryptolens crypto ssl curl)

#

it links libcrypto, libssl and libcurl

cosmic lance
#

Cryptolens has a lot of client libraries for different languages, none for Zig yet (understandable why it wouldn't) but there's the C one so I figured, well I can just use that

#

all i did was copy the source files and headers files into the include subdirectory of my project, then added csource files so yeah makes sense why it wouldnt work out of the box

steep saffron
#

nah thats not how it works

#

it sometimes is but its not in this case

cosmic lance
#

I should've known to look at the CMakeLists file after realizing that the project structure looked kinda non-standard

steep saffron
#

well the readme also points out the build instructions

#

But you are going to need to get libcrpyto and libssl and libcurl somehow

cosmic lance
#

The Cryptolens.lib file can then be used in another project, together with the include files in the include/ directory.

#

I have the lib file built. does that include libcrypto,ssl,curl or no?

#

Sorry if that's a dumb question

steep saffron
#

oh you already have it built

cosmic lance
#

Yeah I tried both ways

#

I tried building it from source files using Zig build system, and also tried to use Cryptolens.lib file with only the headers

steep saffron
#

its not a dumb question i think it shouldn't need more stuff then

cosmic lance
#

I'll try to create a small repro of my problem

steep saffron
#

you should be able to link the lib file you build directly to your project

cosmic lance
#

i first used addLibraryPath to let it find Cryptolens.lib

#

lemme pull up the code

#

lib.addLibraryPath(.{ .path = "include/cryptolens" });

#

lib.linkSystemLibrary("Cryptolens");

#

Cryptolens.lib is in the folder

#

and it builds fine actually but i can't use it via cimprot

steep saffron
#

you need to add the include folders to your own exe

#

post the whole build.zig

#

not just this

#

put it on zigbin

cosmic lance
#

okay it's sort of a mess

steep saffron
#

doesnt matter

cosmic lance
#

i have stbimage in here and also imgui

#

1 sec

steep saffron
#

are you using the package manager to fetch the cryptolens include directory or did you just copy paste the files into your project

cosmic lance
#

copy..

steep saffron
#

okay then it should be fairly straightforward

#

whats the dir structure like.
Persumably:
/src/
/include/cryoptolens/crytolens.lib?

cosmic lance
#

now that i look at the build.zig it's not as messy as i thought, one sec i'm about to send zigbin paste and a screenshot of dir structure

cosmic lance
#

and this won't even build, ive got error: lld-link: x64\Release\error.obj: is not a native COFF file. Recompile without /GL?

#

I built the project using VS2022 first, release x64 configuration to get the .lib file

gusty oracle
#

I can try building it using zig build system later

steep saffron
#

if you want to cimport it then you need to use lib.addIncludePath(.{ .path = "include/cryptolens" });

cosmic lance
#

yep i had that line before

cosmic lance
steep saffron
#

Also:

.obj files produced by /GL and precompiled header files shouldn't be used to build a .lib file unless the .lib file is linked on the same machine that produced the /GL .obj file. Information from the .obj file's precompiled header file is needed at link time.
So persumably you should disable this on the .slon

cosmic lance
#

not even using it yet

#

unless the .lib file is linked on the same machine that produced the /GL .obj file
Hm i see but i am on the same machine..

#

but yeah i should get rid of that i suppose

#

would make it not work for others

steep saffron
#

not exactly

#

it wont work for you either

#

it will only work for exactly the visual studio that built it

cosmic lance
#

ohhh

#

this a PCH issue?

#

should i disable pch

steep saffron
#

The format of files produced with /GL in the current version often isn't readable by later versions of Visual Studio and the MSVC toolset. Unless you're willing to ship copies of the .lib file for all versions of Visual Studio you expect your users to use, now and in the future, don't ship a .lib file made up of .obj files produced by /GL . For more information, see Restrictions on binary compatibility.

cosmic lance
#

Okay so this is why it wouldn't work with Zig build system even on my same machine

steep saffron
#

no not precompiled headers

cosmic lance
#

get rid of /GL compile flag

steep saffron
#

yea

cosmic lance
#

i mean im kinda dumb that's what the error said to do

#

didn't know what it meant

#

thank you for that

steep saffron
#

well try it, once it builds you might run into different issues

steep saffron
# cosmic lance would make it not work for others

note though the main issue with building the lib in Visual studio and then putting the .lib file into your folder is that it complicates other people using your library a lot. Unless they go and fetch themselves their own statically linked cryptolens.lib file.

cosmic lance
#

now it builds

cosmic lance
cosmic lance
steep saffron
#

yes

#

then you should be able to CImport clnclude the headers

cosmic lance
#

wow i didn't know it was that simple

#

thanks a ton

#

😄

steep saffron
#

cheers

cosmic lance
#

@steep saffron hello, sorry for pinging you but of course I got more linking issues I think with windows http library

#

Am i just missing a lib.linkSystemLibrary(...) here

#

only appears after trying to compile with my source code calling a function that needs to make an http request

steep saffron
#

winhttpconnect is in windows.h

#

oh wait it not

cosmic lance
#

winhttp.h?

#

unrelated but i didn't think the Cryptolens.lib file would contain debug info on release build

#

it leaks folder path from my PC

#

can i disable that when I build Cryptolens.lib?

#

strip debug info

#

or would that be a redundant step if im stripping debug info from my Zig project build

steep saffron
#

idk but the problem here is that

#

your cryptolens thingy requires this library called winhttp.dll

cosmic lance
#

__declspec(dllimport)

#

cause it's static library not dynamic right

#

i don't know how to include winhttp.dll

#

Oh wait

#

I think I might know

steep saffron
#

heres a random shot in the dark, but try this:
copy paste it from system32 and put it into the same folder as your binary

cosmic lance
#

So when I was messing around trying to get Cryptolens.lib to work, I changed the build configuration to build as a dll Cryptolens.dll but it wouldn't work (also gave me winhttp linking errors) until I added this line #pragma comment(lib, "winhttp.lib")

#

Is this helpful?

#

and fwiw i'm building my zig project as dynamic library aswel

steep saffron
#

by it i mean winhttp.dll

cosmic lance
steep saffron
#

C:\Windows\System32\winhttp.dll and put it in the bin directory

cosmic lance
#

sorry if idk what i'm saying

steep saffron
#

no its a dll

#

but it might need to be together with the bin

cosmic lance
#

yeah i know but I'm wondering why System32 is not a default include path

#

okay i'll try it

cosmic lance
steep saffron
#

cause is the message you put a runtime error?

cosmic lance
#

This isn't a runtime error

#

Should have been more specific

#

My fault

#

lib.linkSystemLibrary("winhttp");

#

this helped

#

now there's only 2

#

__stdio_common_vsprintf

#

need to find the library this is from

steep saffron
#

ya know what this nonsense shouldnt be happening

cosmic lance
#
error: lld-link: undefined symbol: __declspec(dllimport) __stdio_common_vsprintf
    note: referenced by C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdio.h:1776
    note:               Cryptolens.lib(cJSON.obj):($LN8)
error: lld-link: undefined symbol: __declspec(dllimport) __stdio_common_vsscanf
    note: referenced by C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdio.h:2251
    note:               Cryptolens.lib(cJSON.obj):($LN6)
steep saffron
#

the Cryptolens should be statically linked

#

is it not?

cosmic lance
#

it is

#

include/cryptolens folder has the headers as well as Cryptolens.lib

#

and in build.zig:

#
lib.addLibraryPath(.{ .path = "include/cryptolens" });
    lib.addIncludePath(.{ .path = "include/cryptolens" });
#
lib.linkSystemLibrary("Cryptolens");```
#
const cryptolens = @cImport({
    @cInclude("cryptolens.h");
});
//...
steep saffron
#

the problem is more in the lines of the visual studio project settings

#

this cryptolens thing doesnt have all the symbols it needs in it

cosmic lance
#

tbf the only thing i did was click Yes when I opened the project for the first time in VS to upgrading it

#

from an older version

#

since the lib is like 4 yrrs old

#

i don't know if that would have changed smtn

steep saffron
#

no but like you might need to change something

cosmic lance
#

if not, i don't know which settings would be relevant

#

what do you think i should checK?

steep saffron
#

idk maybe hop in a call and let me see the settings

cosmic lance
#

i could

#

1 sec

#

can you see

steep saffron
#

is my audio broken?

cosmic lance
#

oh im defeaned

#

sorry

#

es

#

yes

#

like a different version of VS to compile with?

#

no hang on

#

the file's the same size now 430kb as before

#
error: lld-link: undefined symbol: __declspec(dllimport) __stdio_common_vsprintf
    note: referenced by C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdio.h:1776
    note:               Cryptolens.lib(cJSON.obj):($LN8)
error: lld-link: undefined symbol: __declspec(dllimport) __stdio_common_vsscanf
    note: referenced by C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdio.h:2251
    note:               Cryptolens.lib(cJSON.obj):($LN6)
#
lib.linkLibC();
    lib.linkLibCpp();
cosmic lance
#

1 sec

cosmic lance
#
comptime {
    @export(..., .{ .name = "__stdio_common_vsprintf", .linkage = .Strong });
    @export(..., .{ .name = "__stdio_common_vsscanf", .linkage = .Strong });
}
steep saffron
#
extern "c" sprintf([*c]u8, [*c]u8, ...);
cosmic lance
#

extern "c" fn sprintf([*c]u8, [*c]u8, ...) void;

steep saffron
#

extern "c" fn sprintf([*c]u8, [*c]u8, ...) c_int;

cosmic lance
#
extern "c" fn sprintf([*c]u8, [*c]u8, ...) callconv(.C) c_int;

comptime {
    @export(sprintf, .{ .name = "__stdio_common_vsprintf", .linkage = .Strong });
}
steep saffron
#

extern "c" fn sscanf([*c] const u8, [*c] const u8, ...) callconv(.C) c_int;

#

extern "c" fn sprintf([*c]u8, [*c]const u8, ...) callconv(.C) c_int;

cosmic lance
#

hmm thinkgun

steep saffron
#

Apparently _NO_CRT_STDIO_INLINE is a preprocessor definition you need included in your visual studio project

#

becuase those two functions are marked inline and hence generate no code

cosmic lance
#

in project settings right?

#

or is this in source code

steep saffron
#

either i think

#

pretty sure it would work better though if you define it in the project settings

#

like this picture i stole off the internet