I'm trying to set up a simple c++ program using Visual Studio 2022. I want it to use emscripten.
After taking a long time trying to follow instructions on how to install emscripten, I'm not completely sure that it's correct. I read that I should check if emcc is setup from my command line, which I show here
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
Now that this is taken care of, I have no idea how to use this in my program. Instructions aren't clear at all anywhere I looked, and I can use some advice from programmers that know all this.
Thanks ahead for your comments!
--------- UPDATE ---------
Sorry about fetting to show the instructions I read. It slipped my mind when I was writing this message.
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 (1 using the "Plain Emsripten Project" template, the other an empty c++ project), neither of them recognized "stdio.h" and "emscripten.h" The mentioned template had the the following
#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, and I didn't want to spend a lot of time trying to find them.
The second method I tried is at the website .html. For this, 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.
The 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 trying to set up a simple c++ program using Visual Studio 2022. I want it to use emscripten.
After taking a long time trying to follow instructions on how to install emscripten, I'm not completely sure that it's correct. I read that I should check if emcc is setup from my command line, which I show here
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
Now that this is taken care of, I have no idea how to use this in my program. Instructions aren't clear at all anywhere I looked, and I can use some advice from programmers that know all this.
Thanks ahead for your comments!
--------- UPDATE ---------
Sorry about fetting to show the instructions I read. It slipped my mind when I was writing this message.
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/items?itemName=KamenokoSoft.emscripten-build-support. It seemed to install correctly, but when I created 2 new projects (1 using the "Plain Emsripten Project" template, the other an empty c++ project), neither of them recognized "stdio.h" and "emscripten.h" The mentioned template had the the following
#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, and I didn't want to spend a lot of time trying to find them.
The second method I tried is at the website https://emscripten./docs/getting_started/downloads.html. For this, 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.
The 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
Improve this question
edited Feb 2 at 15:58
Steven Buechele
asked Feb 2 at 4:17
Steven BuecheleSteven Buechele
271 silver badge3 bronze badges
3
|
1 Answer
Reset to default -1If 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.
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:10C/C++ -> General
forAdditional Include Directories
put the path from your command line output, usingC:\emsdk\upstream\emscripten\system\include
for the include directory, andLinker -> General
forAdditional Library Directories
put your path to the libraryC:\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