From 5f2ba99f2d3f604f12d1471abd92f9fe632b67e9 Mon Sep 17 00:00:00 2001 From: Nexoniarz Date: Sun, 11 Oct 2026 02:20:42 +0200 Subject: [PATCH] -I : look for imported C headers there too (an SDK's headers) Co-Authored-By: Claude Opus 5.5 --- docs/en/cheatsheet.md | 2 +- src/main.cpp | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/en/cheatsheet.md b/docs/en/cheatsheet.md index 42dfe2a..ec86711 100644 --- a/docs/en/cheatsheet.md +++ b/docs/en/cheatsheet.md @@ -12,7 +12,7 @@ Everything in Finch on one page. For explanations, see the [guides](../../README |---|---| | `finch run file.fch [args…]` | compile and run (args go to the program) | | `finch build file.fch [-o name]` | make a program (`name.exe` on Windows) | -| `finch build file.fch --shared` | make a library (`libfile.so`, `file.dll`) that a C program loads; its `export fn`s are its C functions (numbers, bool, char, pointers) | +| `finch build file.fch --shared [-I dir]` | make a library (`libfile.so`, `file.dll`) that a C program loads; its `export fn`s are its C functions (numbers, bool, char, pointers) | | `finch ir file.fch` | print the LLVM IR | | `finch lsp` | the language server (editors use it) | | `finch version` | version | diff --git a/src/main.cpp b/src/main.cpp index fe8f2f0..70c088e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,7 @@ static void usage() { " finch lsp the language server, for editors (VS Code, Kate, ...)\n" "\n" " -l link a C library, same as link \"lib\" in the file\n" + " -I also look for imported C headers in dir\n" " --shared (build) a library with no main: lib.so / .dll, its export fn's callable from C\n" " --target build for another system: windows, linux, arm64 (Linux), macos,\n" " or an LLVM triple\n" @@ -281,7 +282,7 @@ int main(int argc, char **argv) { std::string cmd = argv[1], file = argv[2]; if (cmd != "run" && cmd != "build" && cmd != "ir") usage(); std::string out = stem(file); - std::vector libs, programArgs; + std::vector libs, programArgs, includeDirs; std::string targetName; bool optimizeCode = true, debug = false, outGiven = false; for (int i = 3; i < argc; i++) { @@ -292,6 +293,8 @@ int main(int argc, char **argv) { else if (a == "-O0") optimizeCode = false; else if (a == "-g") debug = true; else if (a == "--shared" && cmd == "build") g_target.shared = true; + else if (a == "-I" && i + 1 < argc) includeDirs.push_back(argv[++i]); + else if (a.rfind("-I", 0) == 0 && a.size() > 2) includeDirs.push_back(a.substr(2)); else if (a == "-l" && i + 1 < argc) libs.push_back(argv[++i]); else if (a.rfind("-l", 0) == 0 && a.size() > 2) libs.push_back(a.substr(2)); else if (cmd == "run") programArgs.push_back(a); @@ -316,6 +319,7 @@ int main(int argc, char **argv) { } if (std::find(dirs.begin(), dirs.end(), dirOf(p.path)) == dirs.end()) dirs.push_back(dirOf(p.path)); } + dirs.insert(dirs.end(), includeDirs.begin(), includeDirs.end()); // -I: after the files' own folders CImports c = importHeaders(allImports, dirs); LLVMContext ctx; TargetMachine *tm = targetMachine();