PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] How to Embind multiple cpp files in the Emscripten compiler?

 June 26, 2022     compiler-errors, embind, emscripten, javascript     No comments   

Issue

I have two cpp files called in the Main.cpp file. This code is to be called from the ams.js file. I'm using the Embind Compiler to call WASM from JS.

Here is my sample code:

class.h:

class CLASS{
public:
int VARIABLE;
void FUNCTION();
};

class.cpp:

#include "CLASS.h"
void CLASS::FUNCTION()
{
VARIABLE = 5;

std::cout << "out : "+VARIABLE << std::endl;
}

Main.cpp:

#include <emscripten/bind.h>
#include "CLASS.h"
using namespace emscripten;
class MyClass
{
public:
MyClass(int x)
: x(x)

{}
int getCharCount(std::string strKey)
{
CLASS a;
a.FUNCTION();

return 0;

}

private:
int x;

 };

EMSCRIPTEN_BINDINGS(my_class_example) {

 class_<MyClass>("MyClass")
 .constructor<int>()
 .function("getCharCount", &MyClass::getCharCount);
    
 }

For compiling:

emcc --bind Main.cpp -o main.js

Calling the function in Render.js:

  var instance = new Module.MyClass();
  if (instance){
  var mainee  =  instance.getCharCount("hi")
  console.log("Somrthing is There");
  }else{
  console.log("Somrthing Wrong");
  }
  instance.delete();

Output error:

main3.js:2780 Uncaught BindingError: Tried to invoke ctor of MyClass with invalid number of parameters (0) - expected (1) parameters instead!

How can I solve this issue?


Solution

Use separate compilation.

emcc --bind -c class.cpp
emcc --bind -c main.cpp
emcc --bind class.o main.o -o main.js

But BindingError is caused by new Module.MyClass();, try new Module.MyClass(123);.



Answered By - zakki
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing