从C++调用 C 库时获取"Symbol Lookup Error"(节点.js插件)
Getting "Symbol Lookup Error" when calling C library from C++ (Node.js Addon)
我正在开发一个Node.js插件,该插件需要包装C++中C库中的对象,以便可以从客户端JavaScript(用CoffeeScript编写)访问它们。
C++模块进行编译,但当我尝试通过Node.js JavaScript运行它时,无法使用调试时遇到问题的symbol lookup error
调用C库。
错误如下:node: symbol lookup error: /var/lib/cloud9/ledscape-wrapper/wrapper/build/Release/wrapper.node: undefined symbol: ledscape_init
wrapper.node
是已编译的包,ledscape_init
是我试图调用的库中的函数。
我已经尝试跟踪代码,并在多个文件中找到相关的片段。我放弃了我认为无关的台词。
# "AllFade.coffee"
@ledscape = require "./ledscape.js"
@frames[1] = @ledscape.LedscapeInit()
# "Ledscape.coffee"
wrapper = require "./build/Release/wrapper"
module.exports = wrapper
包装纸.cc
extern "C" {
#include <ledscape.h>
}
#include <node.h>
#include <v8.h>
#include <node_object_wrap.h>
#include "LedscapeWrapper.h"
Handle<Value> LedscapeInit(const Arguments& args) {
HandleScope scope;
return scope.Close(LedscapeWrapper::NewInstance(args));
}
void InitAll(Handle<Object> exports, Handle<Object> module) {
LedscapeWrapper::Init(module);
NODE_SET_METHOD(exports, "LedscapeInit", LedscapeInit);
}
NODE_MODULE(wrapper, InitAll)
LedscapeWrapper.h
extern "C" {
#include <ledscape.h>
}
#include <node.h>
#include <node_object_wrap.h>
using namespace v8;
class LedscapeWrapper : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> exports);
static Handle<Value> NewInstance(const Arguments& args);
inline ledscape_t* value() const { return value_; }
private:
explicit LedscapeWrapper(ledscape_t* value = ledscape_init(1));
~LedscapeWrapper();
static Handle<Value> New(const Arguments& args);
static v8::Persistent<v8::Function> constructor;
ledscape_t* value_;
};
LedscapeWrappe.cpp
extern "C" {
#include <ledscape.h>
}
#include <node.h>
#include "LedscapeWrapper.h"
using namespace v8;
void LedscapeWrapper::Init(Handle<Object> exports) {
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("LedscapeWrapper"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
constructor = Persistent<Function>::New(tpl->GetFunction());
exports->Set(String::NewSymbol("LedscapeWrapper"), constructor);
}
Handle<Value> LedscapeWrapper::New(const Arguments& args) {
HandleScope scope;
if(args.IsConstructCall()) {
ledscape_t* ledscape = ledscape_init(args[0]->NumberValue());
LedscapeWrapper* obj = new LedscapeWrapper(ledscape);
obj->Wrap(args.This());
return args.This();
}
else {
const int argc = 1;
Local<Value> argv[argc] = { args[0] };
return scope.Close(constructor->NewInstance(argc, argv));
}
}
Handle<Value> LedscapeWrapper::NewInstance(const Arguments& args) {
HandleScope scope;
const int argc = 1;
Handle<Value> argv[argc] = { args[0] };
Local<Object> instance = constructor->NewInstance(argc, argv);
return scope.Close(instance);
}
装订石膏
{
"targets": [{
"target_name": "wrapper",
"sources": ["wrapper.cc","LedscapeWrapper.cpp","LedscapeFrameWrapper.cpp"],
'include_dirs': ['/opt/ledscape/'],
'link_settings': { 'library_dirs': ['/opt/ledscape'] },
}],
}
我认为问题出在LedscapeWrapper.cpp中对ledscape_init()
的一个调用中,它找不到库(ledscape.h),但我主要不是C/C++开发人员。
我试图查看来自GNU或Node的nm
工具,但它拒绝检查.node
文件,而且我在网上找不到任何使用指针。
发生此问题的原因是,实际程序找不到动态库(.so文件)我建议创建一个动态库并将其添加到当前查找路径,以便在linux 中使用/usr/lib
对我有用的是用封装包含的C头文件
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
如此排列(来自https://github.com/bartobri/base64-simple):
// Copyright (c) 2017 Brian Barto
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the MIT License. See LICENSE for more details.
#ifndef BASE64SIMPLE_H
#define BASE64SIMPLE_H 1
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdlib.h>
/*
* Function Prototypes
*/
char *base64simple_encode(unsigned char *, size_t);
unsigned char *base64simple_decode(char *, size_t, size_t *);
#ifdef __cplusplus
}
#endif
#endif
更多信息:https://stackoverflow.com/a/949889
- 反向给定链表中的K节点
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Boost Graph Library,修复节点大小
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 为什么我的删除节点函数实际上没有删除节点?
- C++错误:"error: int aaa::bbb is protected within this context"
- 我们可以删除链表中静态内存中的节点吗
- 如何在pugixml中获取节点的内部XML
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 从C++调用 C 库时获取"Symbol Lookup Error"(节点.js插件)