From 152db27146e8b219af118abae8d235831aad2153 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 25 Feb 2019 20:09:18 -0500 Subject: [PATCH] better error message when forgetting to link against libc closes #1698 --- src/all_types.hpp | 1 + src/ir.cpp | 8 ++++++++ test/compile_errors.zig | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/all_types.hpp b/src/all_types.hpp index 44c1cc0956..fcd4f35b3a 100644 --- a/src/all_types.hpp +++ b/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, diff --git a/src/ir.cpp b/src/ir.cpp index d6e40384f8..73ad54a32d 100644 --- a/src/ir.cpp +++ b/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); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 37b39706b5..d11c077164 100644 --- a/test/compile_errors.zig +++ b/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"));