C++ emscripten setup - Stack Overflow

admin2025-04-16  4

I'm setting up a C++ program using Visual Studio 2022 and Emscripten.

I read that I should check if emcc is setup from my command line:

c:\Users\emcc -v

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.2 (7591f1c5ea0adf6f4293cfba2995ee9700aa0d93)
clang version 21.0.0git (https:/github/llvm/llvm-project 9534d27e3321a3b9e6e79fe6328445575bf26b7b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\emsdk\upstream\bin

How do I use this in my program?

I tried 2 methods to install it.

The first one was installing the visual studio 2022 Emscripten Build Target, which I found at .emscripten-build-support. It seemed to install correctly, but when I created 2 new projects (one using the "Plain Emsripten Project" template, the other an empty C++ project), neither of them recognized stdio.h and emscripten.h. The template had:

#include <stdio.h>
#include <emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

It gives the error cannot open source file 'stdio.h' as well as 'emscripten.h'. It must have been a successful installation, seeing that it had this new template that didn't exist before, but it doesn't know where to find those .h files.

The second method I tried is at .html. Everything installed smoothly and I was able to find the .h files, but I get a ton of errors when I try the following code:

#include <C:\emsdk\upstream\lib\clang\21\include\llvm_libc_wrappers\stdio.h>
#include <C:\emsdk\upstream\emscripten\system\include\emscripten\emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

The errors:

cannot open source file emscripten/em_macros.h
cannot open source file stdbool.h
cannot open source file stdbool.h
identifier printf is undefined
cannot open source file stdlib.h
cannot open source file "stdio.h
#error directive: This file is for GPU offloading compilation only
The specified task executable location \upstream\emscripten\\emcc.bat is invalid.
#error directive: Including files directly from the emscripten source tree is not supported.  Please use the cache/sysroot/include directory.

I'm setting up a C++ program using Visual Studio 2022 and Emscripten.

I read that I should check if emcc is setup from my command line:

c:\Users\emcc -v

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.2 (7591f1c5ea0adf6f4293cfba2995ee9700aa0d93)
clang version 21.0.0git (https:/github.com/llvm/llvm-project 9534d27e3321a3b9e6e79fe6328445575bf26b7b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\emsdk\upstream\bin

How do I use this in my program?

I tried 2 methods to install it.

The first one was installing the visual studio 2022 Emscripten Build Target, which I found at https://marketplace.visualstudio.com/items?itemName=KamenokoSoft.emscripten-build-support. It seemed to install correctly, but when I created 2 new projects (one using the "Plain Emsripten Project" template, the other an empty C++ project), neither of them recognized stdio.h and emscripten.h. The template had:

#include <stdio.h>
#include <emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

It gives the error cannot open source file 'stdio.h' as well as 'emscripten.h'. It must have been a successful installation, seeing that it had this new template that didn't exist before, but it doesn't know where to find those .h files.

The second method I tried is at https://emscripten.org/docs/getting_started/downloads.html. Everything installed smoothly and I was able to find the .h files, but I get a ton of errors when I try the following code:

#include <C:\emsdk\upstream\lib\clang\21\include\llvm_libc_wrappers\stdio.h>
#include <C:\emsdk\upstream\emscripten\system\include\emscripten\emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

The errors:

cannot open source file emscripten/em_macros.h
cannot open source file stdbool.h
cannot open source file stdbool.h
identifier printf is undefined
cannot open source file stdlib.h
cannot open source file "stdio.h
#error directive: This file is for GPU offloading compilation only
The specified task executable location \upstream\emscripten\\emcc.bat is invalid.
#error directive: Including files directly from the emscripten source tree is not supported.  Please use the cache/sysroot/include directory.
Share edited Apr 8 at 22:48 philipxy 15.2k6 gold badges43 silver badges97 bronze badges asked Feb 2 at 4:17 Steven BuecheleSteven Buechele 271 silver badge3 bronze badges 4
  • marketplace.visualstudio.com/… gist.github.com/Roman-Port/8b03244e8c49abbf722b3196b4740c2c – Alan Birtles Commented Feb 2 at 6:03
  • 1 First you need a proper build system, e.g. CMake (I assume you're using VS projects right now). Then use emcmake (see Emscripten manual). Making VS compile with Empscripten probably isn't trivial, I'd compile from the terminal. – HolyBlackCat Commented Feb 2 at 6:10
  • 1 this worked for me, try setting the Include and Library for emscripten to the Project Properties of your VS Project: C/C++ -> General for Additional Include Directories put the path from your command line output, using C:\emsdk\upstream\emscripten\system\include for the include directory, and Linker -> General for Additional Library Directories put your path to the library C:\emsdk\upstream\emscripten\system\lib. Should be able to Build and Run an executable as usual. – chickity china chinese chicken Commented Feb 2 at 6:19
  • Please ask 1 specific researched non-duplicate question. Either ask re 1 bad query/function with obligatory minimal reproducible example, including why you think it should return something else or are unsure at the 1st subexpression where you don't get what you expect or are stuck, justified by reference to authoritative documentation, or ask about your overall goal giving working parts you can do with justification & a minimal reproducible example--then misunderstood code doesn't belong. But please ask about unexpected behaviour 1st because misconceptions get in the way of your goal. How to Ask Help center Basic questions are faqs. – philipxy Commented Apr 8 at 22:49
Add a comment  | 

1 Answer 1

Reset to default -1

If you're willing to use Conan and CMake (I checked that VS has an extension for Conan, but I don't know if/how it works), then you can use the emsdk conan recipe; if not, at least go for CMake, it'll simplify things a lot for you. You need to make it a build dependency of your project's package, use virtualenv generator, and its conanfile will set up the whole toolchain. Ideally, this build dependency should be in a conan profile which you use to build your package and all its dependencies.

Doing the same thing manually, i.e. without a package manager (conan) and build system (cmake), is a good way to learn how things work under the hood, but very impractical for any kind of more complex codebase. The goal of these tools is to make it so you don't have to set up a toolchain manually with system-specific caveats everytime.

Nonetheless, if you really want to use emscripten toolchain directly with your minimal project, you can look into the conan recipe's emsdk/all/conanfile.py, method package_info, to see which environment variables it sets. Some of them like CC, CXX are specific to CMake/build system, others like EMSCRIPTEN are for emscripten's CMake toolchain and yet others like EM_CACHE for emcc itself. You'll also need to manually replicate relevant parts of that CMake toolchain file.

As to your command line, you should probably separate compilation to object files and linking. emcc/em++ follow clang flags to compile, with small additions like setting cache directory. However, there's a ton of important custom flags for linking into final output - you need to choose what sort of output that'll be.

Do you want a UMD export? Do you want a ES6 module? Do you want to load it asynchronously? Do you want a standalone .wasm file instead of .js loader and .wasm code? Or do you want to compile to JS only without WASM? All of that is described in documentation. How will you bind your C/C++ code to JS? If you go for embind, there's a link flag -bind. There's a ton of options. This is something you'll need to decide based on how you want to use your project's output, even if you use CMake.

转载请注明原文地址:http://anycun.com/QandA/1744809331a87920.html