Search code examples
c++webassemblyemscripten

Emscripten C++ to WASM with Classes - "error: undefined symbol"


I have a really simple C++ project (removed as much code til the problem is still there.) Running em++ is always leading to an error, error: undefined symbol

main.cpp

#include "edgeguard.hpp"

int main()
{
    EdgeGuardConfig core = EdgeGuardConfig::BuildEdgeGuard();
}

edgeguard.cpp

#include "edge-guard.hpp"

EdgeGuardConfig EdgeGuardConfig::BuildEdgeGuard()
{
}

edgeguard.hpp

class EdgeGuardConfig
{
public:
    static EdgeGuardConfig BuildEdgeGuard();
};

The command I'm using is em++ ./main.cpp --std=c++17 -o ~/edgeguard-wasm/edgeguard-ft90x.html

The results are always the same:

error: undefined symbol: _ZN15EdgeGuardConfig14BuildEdgeGuardEv (referenced by top-level compiled C/C++ code)
warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
warning: __ZN15EdgeGuardConfig14BuildEdgeGuardEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
em++: error: '/home/nessdan/code/emsdk/node/14.18.2_64bit/bin/node /home/nessdan/code/emsdk/upstream/emscripten/src/compiler.js /tmp/tmpqwnlcp5h.json' failed (returned 1)

Some other things I've tried:

  1. Using EMSCRIPTEN_KEEPALIVE
  2. Wrapping things in extern "C"
  3. Running with -s ERROR_ON_UNDEFINED_SYMBOLS=0 (command finishes, but HTML output logs the error in my console when I open it.)
  4. Running with -s LINKABLE=1 -s EXPORT_ALL=1 - nothing changes, console output is the same.

I'm using the latest version of emscripten (3.1.6) and installed it fresh today (also installed and ran from my Windows computer with the exact same results.)


Solution

  • Thanks to Marc Glisse's comment, I realize now that I needed to not just pass main.cpp but also all related .cpp files (I thought it would pick up that information from the includes 🤦‍♂️)

    It is now compiling 😌