2013-06-30 42 views
0

生成文件提供:C++代碼編譯細跟正常V8發動機,構建失敗,V8由節點

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 

在定義了BUILDING_NODE_EXTENSION而沒有使用正常的V8引擎編譯上面的代碼時,輸​​出結果就是我們所期望的:

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

當定義BUILDING_NODE_EXTENSION編譯,使用節點的<node/node.h><node/v8.h>爲包括而非正常<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 

任何人有任何想法是怎麼回事錯在這裏?

回答

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)