0

生成文件:

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)

插件.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

插件.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

当使用没有定义的普通 V8 引擎编译上述代码时BUILDING_NODE_EXTENSION,输​​出是我们所期望的:

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

当使用定义的进行编译时BUILDING_NODE_EXTENSION,使用 Node 的<node/node.h><node/v8.h>for 包含而不是正常的<v8.h>,我得到这个:

 ❱ 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

有人知道这里出了什么问题吗?

4

1 回答 1

0

建设node-gyp解决了这个问题。

脸。棕榈。

生成文件更改:

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 回答