#How to use Cryptolens C library in Zig
1 messages · Page 1 of 1 (latest)
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
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" }).
i did that
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.
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
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.
I followed the instructions on the readme
Opened the solution in VS and built
It gave me the Cryptolens.lib file output.
The library is kind of strangely designed honestly
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
It still wont compile..
Bunp please..
you can see what files you need to build it in the CMakeLists
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
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
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
I should've known to look at the CMakeLists file after realizing that the project structure looked kinda non-standard
well the readme also points out the build instructions
But you are going to need to get libcrpyto and libssl and libcurl somehow
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
oh you already have it built
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
its not a dumb question i think it shouldn't need more stuff then
I'll try to create a small repro of my problem
you should be able to link the lib file you build directly to your project
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
you need to add the include folders to your own exe
post the whole build.zig
not just this
put it on zigbin
okay it's sort of a mess
doesnt matter
are you using the package manager to fetch the cryptolens include directory or did you just copy paste the files into your project
copy..
okay then it should be fairly straightforward
whats the dir structure like.
Persumably:
/src/
/include/cryoptolens/crytolens.lib?
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
the only 2 relevant lines to cryptolens are these though
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
I can try building it using zig build system later
if you want to cimport it then you need to use lib.addIncludePath(.{ .path = "include/cryptolens" });
yep i had that line before
i removed it for the sake of simplifying things because i'm still getting the err just trying too link the librayr
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
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
not exactly
it wont work for you either
it will only work for exactly the visual studio that built it
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.
Okay so this is why it wouldn't work with Zig build system even on my same machine
no not precompiled headers
get rid of /GL compile flag
yea
i mean im kinda dumb that's what the error said to do
didn't know what it meant
thank you for that
well try it, once it builds you might run into different issues
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.
now it builds
ah i see thank you i did not know
now i assume i can just do this right
cheers
@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
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
idk but the problem here is that
your cryptolens thingy requires this library called winhttp.dll
__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
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
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
by it i mean winhttp.dll
is this gonna statically put the used winhttp funcs into the binary and increase file size?
C:\Windows\System32\winhttp.dll and put it in the bin directory
sorry if idk what i'm saying
yeah i know but I'm wondering why System32 is not a default include path
okay i'll try it
Nah the problem is that i can't even build the binary
cause is the message you put a runtime error?
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
ya know what this nonsense shouldnt be happening
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)
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");
});
//...
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
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
no but like you might need to change something
if not, i don't know which settings would be relevant
what do you think i should checK?
idk maybe hop in a call and let me see the settings
is my audio broken?
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();
1 sec
comptime {
@export(..., .{ .name = "__stdio_common_vsprintf", .linkage = .Strong });
@export(..., .{ .name = "__stdio_common_vsscanf", .linkage = .Strong });
}
extern "c" sprintf([*c]u8, [*c]u8, ...);
extern "c" fn sprintf([*c]u8, [*c]u8, ...) void;
extern "c" fn sprintf([*c]u8, [*c]u8, ...) c_int;
extern "c" fn sprintf([*c]u8, [*c]u8, ...) callconv(.C) c_int;
comptime {
@export(sprintf, .{ .name = "__stdio_common_vsprintf", .linkage = .Strong });
}
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;
hmm 
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
how to include that?
in project settings right?
or is this in source code