Emscripten: unable to work with embind

Created on 29 Jan 2014  路  13Comments  路  Source: emscripten-core/emscripten

Hi,

can any one please help me to get cpp functions to get used at javascript.

Facing below warnings at compilation,

warning: unresolved symbol: _embind_register_class_function
warning: unresolved symbol: _embind_register_class_constructor
warning: unresolved symbol: _embind_register_class
warning: Casting a function pointer type to a potentially incompatible one (use -s VERBOSE=1 to see more)

Got the below error when tried to use it with nodejs
node bindtest.js
missing function: _embind_register_class
-1
-1

CPP Code,

include

include "emscripten/bind.h"

Student::Student() {
}
void Student::setName(std::string name_) {
name = name_;
}
const std::string& Student::getName() {
return name;
}

EMSCRIPTEN_BINDINGS(Student_example) {
class_("Student").constructor()
.function("setName", &Student::setName)
.function("getName", &Student::getName);

}

Thanks,
-Vikas.

Most helpful comment

I think the problem is that in the very first example, you are specifying --bind in the wrong stage: it should not be added to the cmdline when you compile .cpp to .bc, but it should be on the line when compiling .bc to .js (the rule for dsTestApp.js in your makefile). Then, when you tried the lerp example, the spurious warnings threw you off and you assumed it would be failing as well.(?)

When I compile your student.h and student.cpp example on the cmdline with em++ --bind -o student.js student.cpp and run it in node, I don't get any errors. Try adjusting your makefile so that the --bind directive is specified when you produce the final .js file.

All 13 comments

Please use 4 backticks to properly escape the code sample.

How are you compiling it?

Thank you for your time, please find the code as below with back ticks and followed by the core parts of my make file displaying compilation source

student.h

#include <string>

#ifndef __STUDENT__H
#define __STUDENT__H

  class Student {
    std::string name;
  public:
    Student();
    void setName(std::string name);
    const std::string& getName();
  };

#endif

student.cpp

#include <student.h>
#include "emscripten/bind.h"

using namespace emscripten;


  Student::Student() {
  }

  void Student::setName(std::string name) {
    this->name = name;
  }

  const std::string& Student::getName() {
    return this->name;
  }

  EMSCRIPTEN_BINDINGS(Student_example) {
    class_<Student>("Student").constructor()
      .function("setName", &Student::setName)
      .function("getName", &Student::getName);

   }


Make file: that is being used for compilation.

SRC=$(PRJ_DIR)/src
INC_DIR=$(PRJ_DIR)/include
EMSCRIPT_INC_DIR=$(PRJ_DIR)/../system/include/
OBJ_DIR=$(PRJ_DIR)/obj


CC=emcc
CFLAGS = -O2 -I $(INC_DIR) -I $(EMSCRIPT_INC_DIR)
LD_FLAGS =

_OBJ = main.bc linkedList.bc student.bc

$(OBJ_DIR)/%.bc:    $(SRC)/%.cpp
    $(CC) -c --bind -o $@ $< $(CFLAGS)

OBJ=$(patsubst %, $(OBJ_DIR)/%, $(_OBJ))

dsTestApp.js:   $(OBJ)
        $(CC) -O2 -o $@ $^

.PHONY: clean

clean:
    rm -f $(OBJ_DIR)/*.bc
    rm -f dsTestApp.js

I'm still waiting on this thread, can anyone please have a look at it. I will really appreciate to work with this app, and your help will make cross the initial barriers.

hopefully @chadaustin can help

I successfully compiled student.cpp with: em++ -Wall --bind -o student.js student.cpp

It seems to work?

> chad@chad-ubuntu:~/projects/embind-test$ nodejs
> var student = require('./student.js');
undefined
> student.Student
[Function: Student]
> var a = new student.Student();
undefined
> a.getName();
''
> a.setName("hi");
undefined
> a.getName();
'hi'

Thanks for your time, but unfortunately i'm not able to run it.

After your post I got the latest emscripten 1.8.2, but still faced the same issue, got the simple code from https://github.com/kripken/emscripten/wiki/embind example, but still facing same warnings.

I'm working on Mac OS 10.9.1, got XCode properly installed. please let me know how i can trouble shoot this issue, it will be a great help.

Vikas-Mac:mytest vikas$ cat test.cpp
#include <emscripten/bind.h>

using namespace emscripten;

float lerp(float a, float b, float t) {
  return (1 - t) * a + t * b;
}

EMSCRIPTEN_BINDINGS(my_module) {
  function("lerp", &lerp);
}
Vikas-Mac:mytest vikas$ em++ -Wall --bind -o test.js test.cpp
warning: unresolved symbol: _embind_register_function
warning: unresolved symbol: _embind_register_void
warning: unresolved symbol: _embind_register_bool
warning: unresolved symbol: _embind_register_integer
warning: unresolved symbol: _embind_register_float
warning: unresolved symbol: _embind_register_std_string
warning: unresolved symbol: _embind_register_std_wstring
warning: unresolved symbol: _embind_register_emval
warning: unresolved symbol: _embind_register_memory_view
Vikas-Mac:mytest vikas$

Hey @juj, is there something about Emscripten SDK on Mac that would break this?

Not that I know of. The unresolved symbols are present due to --bind using --pre-js instead of --js-library, but afaik that shouldn't cause problems. @pvssvikas, can you try installing the emscripten-master and/or emscripten-incoming tool in the SDK, and activate that to check if the issue is the same there?

I think the problem is that in the very first example, you are specifying --bind in the wrong stage: it should not be added to the cmdline when you compile .cpp to .bc, but it should be on the line when compiling .bc to .js (the rule for dsTestApp.js in your makefile). Then, when you tried the lerp example, the spurious warnings threw you off and you assumed it would be failing as well.(?)

When I compile your student.h and student.cpp example on the cmdline with em++ --bind -o student.js student.cpp and run it in node, I don't get any errors. Try adjusting your makefile so that the --bind directive is specified when you produce the final .js file.

Thanks a lot for your time @kripken, @chadaustin , And special thanks to you @juj for your imagination, support of you all helped me.

@juj, yes you were right i assumed it as not working by seeing the warnings which i was facing from the start. you were even right with my makefile, as i got it working after i added --bind at linkage step, But it is also needed at compilation without which i faced compilation errors.

So I assume --bind is required for both compilation as well as linking phases. I'll continue my exploration more, so you mean I can safely ignore those warnings?

Oh yeah, now that you mention it, I remember that --bind at compile time enables the --std=c++11 option, which is required for embind. Also, a real (but functionally harmless) bug here is that those warnings should naturally not be emitted, and it would be best to rework embind to function as a --js-library, so that emscripten sees the functions in the .js file and can track their inclusion.

Hi,

I know that this is an old thread, but I have tried the student example above.
And like @chadaustin it works nicely on the command line.

But if I put everything in a js file (example.js)
var student = require('./student.js'); var a = new student.Student(); a.setName("hi"); a.getName();

And run it with

node example.js

I get
````
/mnt/d/SVN/Xenon/Learn-WebAssembly/chapter-09-node/student/student.js:120
throw ex;
^

TypeError: student.Student is not a constructor
at Object. (/mnt/d/SVN/Xenon/Learn-WebAssembly/chapter-09-node/student/example.js:2:9)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
````

Is that expected?

Did you mean Module.Student instead of student.Student? And did you allow for the module to initialize before calling the constructor?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jcfr picture jcfr  路  4Comments

kripken picture kripken  路  4Comments

HolgerStrauss picture HolgerStrauss  路  4Comments

phraemer picture phraemer  路  3Comments

nerddan picture nerddan  路  4Comments