Problem with addSourceFiles in build system

Hi all -

I’m having problems with the addSourceFiles statement in the build system.
I’ve done web searches and tried to find some info in the docs, but I find the documentation to be difficult to follow.
Anyway - here’s my build file -


//  build.zig  
//  The build file for Rome.  
//  This code is released to the public domain.  
//  "Share and enjoy....."   :)  


const std = @import("std");

pub fn build(b: *std.Build) void {
    
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{ 
          .name = "rome_compiler" , 
          .root_source_file = b.path("src/main.zig") ,  
          .target = target,  
          .optimize = optimize,     
    });  
    
    b.installArtifact(exe);


    // Add source files
      exe.addSourceFiles(.{        
        .files = &.{
            "src/lexer/lexer.zig",
            "src/parser/parser.zig",   
            "src/ast/ast.zig",   
            "src/semantic_analyzer/semantic_analyzer.zig",   
            "src/ir/ir.zig",   
            "src/codegen/codegen.zig",   
      }, 
     });  


//    exe.addSourceFile("src/lexer/lexer.zig");
//    exe.addSourceFile("src/parser/parser.zig");
//    exe.addSourceFile("src/ast/ast.zig");
//    exe.addSourceFile("src/semantic_analyzer/semantic_analyzer.zig");
//    exe.addSourceFile("src/ir/ir.zig");
//    exe.addSourceFile("src/codegen/code_generator.zig");

    exe.install();
    
}

When I run the build, I get this -

andy@obsidian:~/rome$ zig build
/home/andy/rome/build.zig:28:10: error: no field or member function named 'addSourceFiles' in 'Build.Step.Compile'
      exe.addSourceFiles(.{        
      ~~~^~~~~~~~~~~~~~~
/home/andy/zig/lib/std/Build/Step/Compile.zig:1:1: note: struct declared here
const builtin = @import("builtin");
^~~~~
referenced by:
    runBuild__anon_8818: /home/andy/zig/lib/std/Build.zig:2116:27
    main: /home/andy/zig/lib/compiler/build_runner.zig:301:29
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
andy@obsidian:~/rome$ 

When I saw that, I looked at the docs but they’re difficult to take in.
I contrast them to the Python docs which are really well laid-out and easy to find any aspect of the language in. ( No offense meant there - just an example of a language which does that well. )

So, I’m hoping that someone can point out the correct syntax for addSourceFiles.
Many thanks in advance -

  • mooseman

There is no need to list the zig files. Zig compiler can find them from the root_source_file (src/main.zig) by examining your @import statements.

1 Like

You don’t need to use addSourceFile() directly, Zig uses lazy evaluation, if you have main.zig and inside you have an @import("foo.zig") then foo.zig will be automatically detected as a dependency of main.zig and will be lazily evaluated. This is different if you were to include files from C/C++/Asm/Obj, but that’s beyond the point.

Hi dimdin and pierrelgol - thanks for that!

I removed the lines that try to bring in the files there and that worked!
Many thanks!
In spite of what it may look like, I’m getting better with Zig!
I can now see the common patterns in the build.zig file and I’m gradually becoming more confident with the language itself (which I love!).

Bye for now - thanks again -

  • mooseman
2 Likes