Skip to content

better error message when the user needs to link against libc #1698

Closed

Description

@steveno

The following code

const std = @import("std");                                                                                                                                                                   
const os = std.os;                                                                                                                                                                            
const io = std.io;                                                                                                                                                                            
//const mem = std.mem;                                                                                                                                                                        
//const Allocator = mem.Allocator;                                                                                                                                                            
                                                                                                                                                                                              
pub fn main() !void {                                                                                                                                                                         
//    var direct_allocator = std.heap.DirectAllocator.init();                                                                                                                                 
//    const allocator = &direct_allocator.allocator;                                                                                                                                          
    const allocator = std.heap.c_allocator;                                                                                                                                                   
                                                                                                                                                                                              
    const args = try os.argsAlloc(allocator);                                                                                                                                                 
    if (args.len <= 1) {                                                                                                                                                                      
        os.exit(1);                                                                                                                                                                           
    }                                                                                                                                                                                         
} 

produces this error

lld: error: duplicate symbol: _start
>>> defined at /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o:(_start)
>>> defined at bootstrap.zig:21 (/home/steveno/local/lib/zig/std/special/bootstrap.zig:21)
>>>            zig-cache/main.o:(.text+0x21660)

after issuing

zig build-exe main.zig 

The commented lines in the sample code will produce a working build if you comment current definition of allocator.

Activity

  1. added this to the 0.4.0 milestone on Nov 4, 2018
  2. changed the title [-]lld error duplicates _start when building[/-] [+]better error message when the user needs to link against libc[/+] on Nov 4, 2018
  3. andrewrk commented on Nov 4, 2018

    @andrewrk
    Member

    You need to add --library c to the build line.

  4. steveno commented on Nov 4, 2018

    @steveno
    Author

    How would you add that line to a build.zig file, so that I can just do zig build?

  5. steveno commented on Nov 4, 2018

    @steveno
    Author

    Never mind, I figured it out:

    const Builder = @import("std").build.Builder;                                   
                                                                                    
    pub fn build(b: *Builder) void {                                                
        const mode = b.standardReleaseOptions();                                    
        const exe = b.addExecutable("project", "src/main.zig");                        
        exe.setBuildMode(mode);                                                     
        exe.linkSystemLibrary("c");                                                 
                                                                                    
        b.default_step.dependOn(&exe.step);                                         
        b.installArtifact(exe);                                                     
    }  
    
  6. emekoi commented on Nov 17, 2018

    @emekoi
    Contributor

    zig also gives a confusing error message when you have a c-style main function and forget to link libc:

    ~/zig/build/zig/std/special/bootstrap.zig:87:33: error: 'main' is private
        switch (@typeId(@typeOf(root.main).ReturnType)) {
                                    ^
    ~/zig/example/hello_world/hello_libc.zig:10:1: note: declared here
    export fn main(argc: c_int, argv: **u8) c_int {
    ^
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions