title: Emscripten EMSCRIPTEN_KEEPALIVE / EXPORTED_FUNCTIONS from hpp/cpp files other then main
body: <p>I have a c++ webassembly module with several function that is called wasmExec.wasm. So far i have been exporting methods from c++ to JavaScript that are located in the cpp file where the main() method is located using the EMSCRIPTEN_KEEPALIVE keyword. That is working just fine. I do see those methods when i do </p>
  
  <pre><code>wasm-nm wasmExec.wasm 
  </code></pre>
  
  <p>and i can call them from JavaScript and get the expected result. In order achieve clean methods name i have been disabling C++ name mangling.</p>
  
  <p>Now i have tried to use EMSCRIPTEN_KEEPALIVE with <strong><em>static</em></strong> methods that are not located in the main.cpp file instead in a separate namespace and separate cpp and header files. In those classes i am also using EMSCRIPTEN_KEEPALIVE but unfortunately the produced wasm module is eliminating my method and thus i can not see them when executing <code>wasm-nm wasmExec.wasm</code> althout they are marked with EMSCRIPTEN_KEEPALIVE in the header as well as in the cpp file. i.e. here is an example</p>
  
  <pre><code>#ifndef API_REGISTRATION_REGISTRATIONMANAGER_H_
  #define API_REGISTRATION_REGISTRATIONMANAGER_H_
  
  
  #include &lt;string&gt;
  
  #include &lt;emscripten/emscripten.h&gt;
  
  using namespace std;
  
  namespace api::registration {
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  
  class RegistrationManager {
  public:
      explicit RegistrationManager();
      virtual ~RegistrationManager();
  
      static char* EMSCRIPTEN_KEEPALIVE registerClient(
              char* userNameInputPtr, int userNameLength
  
      );
  
  };
  
  
  #ifdef __cplusplus
  }
  #endif
  
  
  } 
  
  #endif /* API_REGISTRATION_REGISTRATIONMANAGER_H_ */
  </code></pre>
  
  <p>I am using cmake to compile the project. In the target properties i have explicitly defined EXPORTED_FUNCTIONS to be equal to </p>
  
  <pre><code>set_target_properties(wasmExec PROPERTIES LINK_FLAGS "-std=c++17 -s WASM=1  -s TOTAL_MEMORY=512MB  -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=1  -s VERBOSE=1  --pre-js /Projects/src/wasm/preModule.js -s DEMANGLE_SUPPORT=1 -s DISABLE_EXCEPTION_CATCHING=0 -s ERROR_ON_UNDEFINED_SYMBOLS=0  -s EXPORTED_FUNCTIONS='['registerClient']'    -s EXTRA_EXPORTED_RUNTIME_METHODS='['cwrap', 'getValue', 'setValue', 'registerClient' ]' " )
  </code></pre>
  
  <p>What could be the reason to not be able to register this the method "registerClient"    within the webassembly module. I have even tired to use the option <code>-s EXPORT_ALL=1</code> but that did not exported all the know methods as i was expcting it to do</p>
  
  <p>I am compiling everything statically here i.e. below is the example of the registration_api  cmake sub-folder</p>
  
  <pre><code>MESSAGE( STATUS "ADDING API_REGISTRATION_DIR")
  
  set( API_REGISTRATION_SRC
  
      RegistrationManager.h RegistrationManager.cpp
  
  
      )
  set(LIB_TYPE STATIC)
  
  add_library(api_registration ${LIB_TYPE} ${API_REGISTRATION_SRC} )
  
  
  target_include_directories(api_registration PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/")
  </code></pre>
  
  <p>the compilation process is successful with emscripten, nevertheless i do get this warrning exactly about the method i am trying to export:</p>
  
  <pre><code>em++: warning: undefined exported function: "registerClient" [-Wundefined]
  </code></pre>
  
  <p>if i put that method in the main.cpp it is automatically exported without any problem. </p>
  
  <p>My question is what could be the reason why this export is not working when the method is located in external cp/hpp file?</p>
  
  <p>Addition 15 May 2020: I was able to register the method only if i remove the class definition, change the namesapce to the one that main have. Then it seems that the underscore in the EXPORTED_FUNCTIONS is important i.e. -s EXPORTED_FUNCTIONS='['_registerClient']' . That worked for me. I was under the impression that if i use static methods that should work with classes as well, but it seems that not to be the case. </p>
  
  <pre><code>#ifndef API_REGISTRATION_REGISTRATIONMANAGER_H_
  #define API_REGISTRATION_REGISTRATIONMANAGER_H_
  
  #include &lt;string&gt;
  
  #include &lt;emscripten/emscripten.h&gt;
  
  using namespace std;
  
  namespace wasm {
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  char* EMSCRIPTEN_KEEPALIVE registerClient(
              char* userNameInputPtr, int userNameLength
      );
  
  #ifdef __cplusplus
  }
  #endif
  
  } 
  
  #endif /* API_REGISTRATION_REGISTRATIONMANAGER_H_ */
  </code></pre>
  
  <p>thus my question has changed to: Can emscripten register a static method of a class with EMSCRIPTEN_KEEPALIVE or not?</p>