afer adding lib as a module, this works I can build and run the exe but when I try to manually run the app1 as in cd /app1/ and run zig run main.zig I get this error:
main.zig:3:24: error: no module named 'lib' available within module main
zig run file.zig doesn’t use the modules / build steps described in build.zig, I see it more as a convenience tool to run simple little zig programs, it is possible to use zig run ... with modules, but you would need to explicitly specify them as commandline arguments (I think at that point it is easier to use a run step specified in the build.zig to run your executable).
If you have an example.zig and lib.zig in the same directory you could use zig run with a module, like this:
zig run --dep lib -Mroot=example.zig -Mlib=lib.zig
However instead I would encourage you to define a run step in your build.zig so that you can instead use zig build run, to see how you can do this, create an empty directory and execute zig init within it, then look at the resulting build.zig and how it creates a run step for the executable.
Further if you then use zig build run --verbose you will be able to see how the --dep and -M flags are used as arguments to zig build-exe to build your application (without you having to manually specify them on the commandline).
Looking at the verbose output can help with understanding how these flags work, but I think normally you wouldn’t need to use these flags directly yourself, writing your build.zig and using that definitely is the easier option.
I don’t know what you intend to do with these two lines, from what I see you only have a source module with source code called “lib”, you haven’t created any library-artifact, for that you would have to create a static library like what is shown when you use zig init, it is also unclear how libRoot is defined.
Try to fix your build.zig adding a run-step to it, executing it with zig build run, if that doesn’t work, you should post the complete build.zig.
libroot is just the root.zig that is generated when running zig init. Here is the build func. I just want to be able to run the code from src instead of having to run the exe or build it and run it. Its faster that way.
zig run also builds and runs an executable, it just builds that executable in the background without using the build.zig, there is no way to run a zig program without building it in some way.
I am also unsure how it would be faster to use zig run instead of zig build run those should be pretty similar in terms of speed.
Do you mean you find it more comfortable to run zig run main.zig from within the src directory in a terminal, instead of invoking zig build run within that terminal?
I don’t really understand what you want to do, or why you find zig build run less desirable.
Yeah I just prefer running it from src since im already working in that dir but if there is no real way to run it from src then I can just run the build exe. Also can you explain what is the difference between static lib and dynamic lib? tHANKS FOR THE HELP. But the weird thing is i can run it from src on my other app
so my folder structure is like this
-root
-main.zig
- const Logger = @import("../lib/logger/logger.zig"); <<<<<< THIS WORKS RUNNING FROM SRC WHY IS THAT?
use logger...
-build.zig
-build.zon
-app1
-src
-main.zig
-build.zig
-build.zon
-app2 (same as app1)
-lib
-main.zig (just a file to export all the sub folders)
pub const Logger = @import("./logger/logger.zig").Logger;
pub const Utils = @import("./utils//utils.zig");
pub const Http = @import("./http/http.zig").Http;
pub const Types = @import("./types/types.zig");
pub const Context = @import("./context/context.zig");
-utils/
-utils.zig
-logger/
-logger.zig
-http/
-http.zig
Just as another method when you are in src you can also execute zig build run it should travel up the directory hirarchy find the first build.zig and run the run step defined by that build.zig, so you don’t really have to change to the parent directory first.
There is a way to use zig run ... as my first comment shows, it just isn’t easier than using a build.zig with a run step defined (in my opinion).
You need to separate static/dynamic libraries conceptually from “libraries” of zig source code modules.
The former are more about creating artifacts that can be used as static/dynamic libraries (similar to how you could use c to create static or dynamic libraries), these are useful to compile code in separate compilation units which are then later linked statically or dynamically. Unless you really know that you need this, you likely don’t need it at all, if you just want to write pure Zig programs.
The latter is about providing modules of Zig source code, that ends up being compiled directly into the executable as a single compilation unit assembled from all modules.
Your other app doesn’t use imported modules, instead it only has the application root as the only module and then uses relative (file-based) imports to directly make the lib source code part of its own source code.
When you use an import ending in .zig it is a relative import, which means that the imported file is part of the current module.
When you use something like @import("lib") it is a import of a different module, this requires that somewhere a commandline-flag or a build.zig defines the lib module, you can’t use a module that you haven’t defined.
That is why zig run only works with programs that use other modules when you specify (complicated) flags.