Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
steveno opened this Issue Nov 4, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@steveno
Copy link

steveno commented Nov 4, 2018

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.

@andrewrk andrewrk added this to the 0.4.0 milestone Nov 4, 2018

@andrewrk andrewrk changed the title lld error duplicates _start when building better error message when the user needs to link against libc Nov 4, 2018

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Nov 4, 2018

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

@steveno

This comment has been minimized.

Copy link
Author

steveno commented Nov 4, 2018

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

@steveno

This comment has been minimized.

Copy link
Author

steveno commented Nov 4, 2018

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);                                                     
}  
@emekoi

This comment has been minimized.

Copy link
Contributor

emekoi commented Nov 17, 2018

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
You can’t perform that action at this time.