0

Makefile:

CC:=g++
OUTFILE?=addon
SOURCE?=src
CFLAGS+=-Iinclude -lv8 -m64

.PHONY: all clean

all:
    $(CC) $(CFLAGS) -o $(OUTFILE) `ls $(SOURCE)/*.cc`

clean:
    rm -f $(OUTFILE)

addon.h:

#ifndef __addon_h
#define __addon_h


#define BUILDING_NODE_EXTENSION
#define ADDON_VERSION "0.0.1"


#ifdef BUILDING_NODE_EXTENSION
  #include <node/node.h>
  #include <node/v8.h>
#else
  #include <v8.h>
#endif


using namespace v8;

void init(Handle<Object> exports);
Handle<Object> setupExports(Handle<Object> exports);

#endif

addon.cc:

#include <cstdlib>
#include <cstdio>
#include <addon.h>

using namespace v8;

#ifndef BUILDING_NODE_EXTENSION
  int main(int argc, char **argv) {
    Isolate *isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);
    Handle<Context> context = Context::New(isolate);
    Context::Scope context_scope(context);
    Handle<Object> exports = Object::New();
    init(exports);

    printf("Version: %s\n", *String::Utf8Value(exports->Get(String::New("version"))));
    return 0;
  }
#else
  int main() {
    printf("This is a module compiled for Node.\nPlease use require in Node to use this file.\n");
    return 0;
  }
#endif

void init(Handle<Object> exports) {
  setupExports(exports);
}

Handle<Object> setupExports(Handle<Object> exports) {
  // Set version number.
  exports->Set(String::New("version"), String::New(ADDON_VERSION));

  return exports;
}



#ifdef BUILDING_NODE_EXTENSION
  NODE_MODULE(addon, init)
#endif

When compiling the above code with the normal V8 engine without BUILDING_NODE_EXTENSION defined, the output is what we expect:

 ❱ make && ./addon
g++ -Iinclude -lv8 -m64 -o addon `ls src/*.cc`
Version: 0.0.1

When compiling with BUILDING_NODE_EXTENSION defined, using Node's <node/node.h> and <node/v8.h> for includes instead of the normal <v8.h>, I get this:

 ❱ make && ./addon
g++ -Iinclude -lv8 -m64 -o addon `ls src/*.cc`
Undefined symbols for architecture x86_64:
  "v8::String::New(char const*, int)", referenced from:
      setupExports(v8::Handle<v8::Object>)       in ccRntYkS.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [all] Error 1

Anyone have any idea what is going wrong here?

4

1 に答える 1

0

でビルドするとnode-gyp問題が解決しました。

顔。やし。

Makefile の変更:

CC:=g++
OUTFILE?=addon
SOURCE?=src
CFLAGS+=-Iinclude -lv8 -m64

.PHONY: all clean

all:
    if [ -d build ]; then \
        node-gyp build; \
    else \
        $(CC) $(CFLAGS) -o $(OUTFILE) `ls $(SOURCE)/*.cc`; \
    fi;

clean:
    rm -f $(OUTFILE)
于 2013-07-01T02:46:33.323 に答える