diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e1720e650a..bc98e12607 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -282,7 +282,7 @@ pub fn (mut g Gen) init() { g.cheaders.writeln(g.pref.custom_prelude) } else if !g.pref.no_preludes { g.cheaders.writeln('// Generated by the V compiler') - g.cheaders.writeln('#include ') // int64_t etc + g.cheaders.writeln(get_guarded_include_text('', 'Please install build-essentials')) // int64_t etc g.cheaders.writeln(c_builtin_types) if g.pref.is_bare { g.cheaders.writeln(bare_c_headers) @@ -979,19 +979,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { } else { missing_message += ' Please install the corresponding development headers.' } - mut guarded_include := ' - |#if defined(__has_include) - | - |#if __has_include($node.main) - |#include $node.main - |#else - |#error VERROR_MESSAGE $missing_message - |#endif - | - |#else - |#include $node.main - |#endif - '.strip_margin() + mut guarded_include := get_guarded_include_text(node.main, missing_message) if node.main == '' { // fails with musl-gcc and msvc; but an unguarded include works: guarded_include = '#include $node.main' @@ -5533,3 +5521,20 @@ fn (mut g Gen) panic_debug_info(pos token.Position) (int, string, string, string pamod := g.fn_decl.modname() return paline, pafile, pamod, pafn } + +pub fn get_guarded_include_text(iname string, imessage string) string { + res := ' + |#if defined(__has_include) + | + |#if __has_include($iname) + |#include $iname + |#else + |#error VERROR_MESSAGE $imessage + |#endif + | + |#else + |#include $iname + |#endif + '.strip_margin() + return res +}