I’m working on a project that is made up of two different compilation units, a static library and an executable — both written in C. Rather than linking against the static library, however, the executable uses #embed to statically store the library code.
The documentation for the Zig Build System has a great example of doing something very similar from Zig code, using the @embedFile builtin and an anonymous import, but #embed isn’t import-aware, and the library’s filename isn’t well-known.
I’ve got a few thoughts on how to accomplish my goals, but I’m not sure how exactly to enact them.
I could communicate the path to the compiled library via a C define/macro.
To do this, the executable target would need configured after the library build finished, which I can’t find a way to do.
I could install or copy the library to a location specified in the build script, and communicate that to the compiled library via a C define/macro.
I can’t seem to find a way to do that install/copy in a way that doesn’t also change the executable’s install dir.
I could leverage the anonymous import approach, if Zig’s C compiler exposed that path to me as a define/macro.
I could rewrite the executable in Zig, if the C translation layer didn’t choke on its dependencies’ headers.
I’m hopeful that someone here can either give me other ideas to try, or fill in some gaps in my understanding.
I am a bit confused about what you are trying to do, @embedFile is usually used to embed static data into the executable, I wouldn’t assume that that data is marked as executable, my assumption would be that it isn’t and that it is mapped as read-only, but not executable. (I haven’t verified that but it would seem logical to me from how I have it seen used in practice, always with data never with code. (so far))
Usually C code is used with Zig by either statically or dynamically linking it, where static is probably the more popular default.
I have trouble understanding what the point of that would be, if the code is available to be embedded, wouldn’t it also be possible to just statically link it?
It seems unnecessarily complicated to embed it instead.
Not saying you shouldn’t do it, I just wonder what would be the benefit of that approach?
If you create a build step that creates the library and you get a lazy path to the result, you should be able to use that as the .root_source_file of the anonymous import and the build system would automatically ensure that it is build first.
I have trouble understanding your project structure, I think it would be helpful if you created a small example project that illustrates what you are doing, for example by creating a example repository.
At the moment I don’t understand how you compile your C code, do you use the Zig build system?
I am a bit confused about what you are trying to do, @embedFile is usually used to embed static data into the executable, I wouldn’t assume that that data is marked as executable, my assumption would be that it isn’t and that it is mapped as read-only, but not executable.
That’s correct; I’m trying to bundle this library into my executable as data.
I have trouble understanding what the point of that would be, if the code is available to be embedded, wouldn’t it also be possible to just statically link it? It seems unnecessarily complicated to embed it instead.
The executable in my project doesn’t run any of the library code itself. The compiled library is just binary data that the executable writes to the filesystem later.
If you create a build step that creates the library and you get a lazy path to the result, you should be able to use that as the .root_source_file of the anonymous import and the build system would automatically ensure that it is build first.
The issue isn’t dependency ordering; that was fairly straightforward. It’s more that AFAICT the only mechanism I have to communicate the library Compile’s output filename to the executable Compile seems to be C defines/macros, and I haven’t worked out how to configure that define/macro with data from a LazyPath.
I have trouble understanding your project structure, I think it would be helpful if you created a small example project that illustrates what you are doing, for example by creating a example repository.
The core is pretty simple:
// src/library.c
#include <stdio.h>
void foo() {
puts("Hi there!");
}
// src/executable.c
char library[] = {
#embed LIBRARY
};
int main(int argc, char *argv[]) {
// Do things with the `library` bytes.
}
At the moment I don’t understand how you compile your C code, do you use the Zig build system?
I’ve been trying to move to the Zig build system; previously, I’d been scripting compilation by hand, which looked something like this: