Hello! This guide will show you how to set up CMake projects in Visual Studio 2019 to use Emscripten. This guide is mostly for me to set this up again, but it'll hopefully be useful for someone else.
This guide assumes you've already installed and set up Emscripten but just want to use it with Visual Studio 2019.
After you've created your CMake project, right click CMakeLists.txt
in Visual Studio and click "CMake Settings For...". You'll get a new editor tab.
In this tab, locate the "CMake toolchain file" and set the path to the following, replacing EM_PATH with the path to the SDK:
EM_PATH\upstream\emscripten\cmake\Modules\Platform\Emscripten.cmake
Next, under "CMake variables and cache", change the following values once again replacing EM_PATH:
CMAKE_C_COMPILER
->EM_PATH\upstream\emscripten\emcc.bat
CMAKE_CXX_COMPILER
->EM_PATH\upstream\emscripten\em++.bat
You should then be able to ctrl
+s
save and "delete and regenerate" CMake cache. It should now build!
Here's the CMakeSettings.json
file. You should be able to create this file and paste the following in, replacing C:\Users\Roman\Documents\emsdk-main\
with the path to your Emscripten SDK.
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"buildCommandArgs": "-v",
"ctestCommandArgs": "",
"cmakeToolchain": "C:\\Users\\Roman\\Documents\\emsdk-main\\upstream\\emscripten\\cmake\\Modules\\Platform\\Emscripten.cmake",
"variables": [
{
"name": "CMAKE_C_COMPILER",
"value": "C:\\Users\\Roman\\Documents\\emsdk-main\\upstream\\emscripten\\emcc.bat",
"type": "FILEPATH"
},
{
"name": "CMAKE_CXX_COMPILER",
"value": "C:\\Users\\Roman\\Documents\\emsdk-main\\upstream\\emscripten\\em++.bat",
"type": "FILEPATH"
}
]
}
]
}```