C++ Classes on a Node.js AddOn

487 views
Skip to first unread message

Benjamin Farrell

unread,
Mar 15, 2013, 10:27:22 AM3/15/13
to nod...@googlegroups.com
Hey everyone,
This group's help got me up and running with my first Node.js AddOn a couple months back, I'm pretty excited that all this stuff is working.  But now that I have something stable, I realize that my C++ code is a little disorganized and I should really start breaking out what I can into separate files or classes as I continue.

I'm having a pretty big problem doing this, though.  I thought the problem was solely C++ oriented, but the more I look around and ask people, it sounds like I'm doing the right thing, and it might be specific to my project or just Node compilation in general.

So the problem is this - I broke out functionality into another class.  I did it the proper way, and put the method signatures into a .h file and the actual methods into a .cpp file.

I then, of course, include the .h file in my main project.   It all compiles fine, but then when running the addon, I get:

/usr/local/bin/node: symbol lookup error: ../node_modules/nuimotion/src/build/Release/nuimotion.node: undefined symbol: _ZN11EnumMapping15mapEventToLabelEi

Where EnumMapping is my class and mapEventToLabel is a method within.

Also weird is that I can write gibberish into my cpp file that obviously would never compile, but it does.  So obviously it seems that my cpp file isn't being referenced, only the h file is being compiled in.

So, this is where I'm at.  I feels like any other weird C++ problem, but everyone I talk to tell me that all they ever have to do is include the .h file and it works, but they aren't working on Node addons - and that's why I'm writing here...in case there's something Node/V8 specific.

Perhaps I'm missing a fundamental piece of information being a beginner c++ person.  I see people using externs, templates, etc, and admittedly I don't really understand those concepts yet.  I'm just using a plain .h file like so:

class EnumMapping
{
public:
Local<String> mapEventToLabel(int event);
Local<String> mapGeneralEventToLabel(int event);
Local<String> mapGestureToLabel(int event);
Local<String> mapErrorToLabel(int event);
}

And then my .cpp file takes the structure like so:

#include "../enums/EnumMapping.h"

Local<String> EnumMapping::mapEventToLabel(int event) {
   ...function body
}

...other methods...

Again, sorry if this isn't a Node specific problem, but I hope you can help!

thanks!

Ben Noordhuis

unread,
Mar 15, 2013, 10:53:47 AM3/15/13
to nod...@googlegroups.com
It sounds like you forgot to add the .cc file to the 'sources' section
in your bindings.gyp.

Benjamin Farrell

unread,
Mar 15, 2013, 2:23:45 PM3/15/13
to nod...@googlegroups.com
Really?  I much appreciate the answer, and I can do that, but how are large projects done?  I would have thought that the include directive would be enough.  Let's say I had 50 classes in my project, would I put them all in the gyp file?  Or perhaps the strategy is that you need to compile them into a library?

I had considered exactly what you said, but I just figured that you would only include the main entry point for your addon in your gyp file, and the others would be compiled in by virtue of the include.

Anyway, I'll take it!  It's a strategy, so many thanks - I'm just curious how this scales to larger projects.

Benjamin Farrell

unread,
Mar 15, 2013, 2:26:50 PM3/15/13
to nod...@googlegroups.com
Oh never mind.....I'm looking and I see you can use wildcards and possibly regex in the source directive.

Sorry for the noob questions - I just thought I had a handle on this and made some bad assumptions.
Thanks so much!

Ryan Dahl

unread,
Mar 15, 2013, 2:31:54 PM3/15/13
to nodejs
There are wildcards but there's nothing wrong with listing out all the
source files. Chrome has many thousands of source files and pretty
much all of them are listed explicitly in gyp files. Explicit is
excellent.

Benjamin Farrell

unread,
Mar 15, 2013, 3:12:18 PM3/15/13
to nod...@googlegroups.com, r...@tinyclouds.org
Awesome, thanks Ryan!  I def don't mind listing explicitly, but I wouldn't have thought that was the "right" way.
Reply all
Reply to author
Forward
0 new messages