|
|
|
|
|
by HenriTEL
451 days ago
|
|
All the magic seems to happen in const lib = b.addLibrary(.{
.linkage = .static,
.name = "cgo_static_linking",
.root_module = lib_mod,
});
and/*
#cgo LDFLAGS: -L./zig-out/lib -lcgo_static_linking -static
#include "zig_lib.h"
*/ Now I can only guess why it works. A bit of an explanation wouldn't hurt.
Good post btw |
|
The #cgo line tells the Go compiler to add custom compilation flags: -l flag tells the compiler that you wish to link to a library called "cgo_static_linking" in to the binary. However, the cgo compiler can't find it since it is not in the default search paths for libraries. The -L option extends the library search path to "./zig-out/lib/", allowing "cgo_static_linking" to be found.
The "#include" line inserts the header file contents as-is into the source file (main.go), bringing the definition of the function available to the compiler. The compiler is then able to compile the Go source, and later link it with the other build artifacts to produce the final executable.
The key here is the "C ABI" which acts as common ground for both languages. The rest is just scaffolding to let the compilers find and connect appropriate files together.