better error message when forgetting to link against libc

closes #1698
master
Andrew Kelley 2019-02-25 20:09:18 -05:00
parent 3ca861c7dd
commit 152db27146
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
3 changed files with 19 additions and 0 deletions

View File

@ -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,

View File

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

View File

@ -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"));