Skip to content

Instantly share code, notes, and snippets.

@Roman-Port
Created September 5, 2021 20:45
Show Gist options
  • Save Roman-Port/8b03244e8c49abbf722b3196b4740c2c to your computer and use it in GitHub Desktop.
Save Roman-Port/8b03244e8c49abbf722b3196b4740c2c to your computer and use it in GitHub Desktop.
Using Emscripten with Visual Studio 2019

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.

Applying Settings

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!

Impatient?

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"
        }
      ]
    }
  ]
}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment