Commit 152db27146

Andrew Kelley <andrew@ziglang.org>
2019-02-26 02:09:18
better error message when forgetting to link against libc
closes #1698
1 parent 3ca861c
Changed files (3)
src/all_types.hpp
@@ -1808,6 +1808,7 @@ struct CodeGen {
     bool enable_cache;
     bool enable_time_report;
     bool system_linker_hack;
+    bool reported_bad_link_libc_error;
 
     //////////////////////////// Participates in Input Parameter Cache Hash
     /////// Note: there is a separate cache hash for builtin.zig, when adding fields,
src/ir.cpp
@@ -15520,6 +15520,14 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
 }
 
 static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name, AstNode *source_node) {
+    if (buf_eql_str(lib_name, "c") && ira->codegen->libc_link_lib == nullptr &&
+        !ira->codegen->reported_bad_link_libc_error)
+    {
+        ir_add_error_node(ira, source_node,
+            buf_sprintf("dependency on library c must be explicitly specified in the build command"));
+        ira->codegen->reported_bad_link_libc_error = true;
+    }
+
     LinkLib *link_lib = add_link_lib(ira->codegen, lib_name);
     for (size_t i = 0; i < link_lib->symbols.length; i += 1) {
         Buf *existing_symbol_name = link_lib->symbols.at(i);
test/compile_errors.zig
@@ -1,6 +1,16 @@
 const tests = @import("tests.zig");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.addTest(
+        "implicit dependency on libc",
+        \\extern "c" fn exit(u8) void;
+        \\export fn entry() void {
+        \\    exit(0);
+        \\}
+    ,
+        ".tmp_source.zig:3:5: error: dependency on library c must be explicitly specified in the build command",
+    );
+
     cases.addTest(
         "libc headers note",
         \\const c = @cImport(@cInclude("stdio.h"));