You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何指定Emscripten编译时查找源文件的路径?

Fixing "File Not Found" Errors with Emscripten Include Paths

Hey there! As someone who’s hopped between JS and Emscripten projects, I totally get how disorienting compile-time path issues can be when you’re used to module resolvers in JavaScript. Let’s walk through exactly how to tell Emscripten where to find your header files.

The Core Issue

When you use #include "Core/main.h" and that file references Core/app.h, the compiler needs to know where the root directory containing the Core folder lives. By default, Emscripten (which uses Clang under the hood) only searches the current working directory and system include paths—so if your Core folder isn’t in one of those spots, you’ll hit that frustrating "file not found" error.

Solutions to Specify Include Paths

1. Use the -I Compile Flag (Most Common Fix)

The -I (uppercase "i") flag tells the compiler to add a directory to its list of places to hunt for header files.

Let’s say your project structure looks like this:

my-project/
├── Core/
│   ├── main.h
│   └── app.h
└── src/
    └── my-program.c  // This includes "Core/main.h"

If you’re running emcc from the my-project directory, your compile command should include -I./ to add the project root as a search path:

emcc src/my-program.c -I./ -o output.js

If you’re working from the src subdirectory instead, use the relative path to the project root:

emcc my-program.c -I../ -o output.js

2. Adjust Your #include Syntax (Optional)

Since main.h and app.h are in the same Core directory, you can simplify the include in main.h to:

#include "app.h"

When you use double quotes ("") instead of angle brackets (<>), the compiler first searches the directory where the current header file is located before checking system paths. This can cut down on long include paths, but it’s still smart to specify the root search path with -I for consistency across your project.

3. For CMake Projects

If you’re using CMake to manage your build (common for larger Emscripten projects), add this to your CMakeLists.txt to include your project root or Core directory:

# Add the project root as an include directory
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# OR, specifically add the Core directory
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Core)

Then build with Emscripten’s CMake wrapper:

emcmake cmake ..
make

4. Verify Paths with Verbose Mode

If you’re still stuck, run emcc with the -v flag to see exactly which directories the compiler is searching. This can help you spot if your -I path isn’t pointing where you think it is:

emcc -v src/my-program.c -I./ -o output.js

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:37:59