C++代码使用普通的V8引擎编译良好,无法使用Node提供的V8进行构建
C++ code compiles fine with normal V8 engine, fails to build with V8 provided by Node
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: %sn", *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/node.h>
和<node/v8.h>
作为includes,而不是普通的<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
有人知道这里出了什么问题吗?
使用node-gyp
构建解决了问题。
脸。棕榈
Makefile change:
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)
相关文章:
- node-gyp 在 macOS 上未正确链接库
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 在 G++ v8.2.0 中使用 std::experimental::可选
- node-gyp 的先有鸡还是先有蛋的问题:指向依赖项中的头文件
- 如何为 v8::P ersistent<v8::Function> 创建到 c++ 函数的包装器
- 在 ubuntu 上安装 node js pulsar 客户端
- 如何使用 Node-addon-API 实现 node-nan 回调
- 如何使用 v8 本机插件将 C++ 数组交付到 Node.js
- Mac 上的 node-gyp 构建错误"calling a protected constructor of class 'v8::HandleScope'"
- 使用 Node 原生 C++ 插件中的 protobuf 并返回 V8
- C++代码使用普通的V8引擎编译良好,无法使用Node提供的V8进行构建
- 在Node插件中将c++对象封装为v8对象
- 为Node.js插件在void *中检索和存储V8对象
- Node c++插件/ v8 /自定义依赖
- *v8::String::Utf8Value(args[0]->ToString()) 不返回 node 的字符串.js addon 参数
- 在Node.js和v8中调用一个使用包装对象作为参数的函数
- 为什么V8在Node.JS中比我的本地c++插件快
- 使用NAN将数组从node.js传递到c++v8
- node.js/[我自己的库/插件]如何在v8上运行?