首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C++ (Node.js Addon)调用C库时获取“符号查找错误”

问从C++ (Node.js Addon)调用C库时获取“符号查找错误”
EN

Stack Overflow用户
提问于 2014-10-20 23:19:48
回答 1查看 3K关注 0票数 5

我正在开发一个Node.js插件,它需要在C++中包装来自C库的对象,这样就可以从客户端JavaScript (用CoffeeScript编写)访问这些对象。

C++模块会编译,但是当我试图通过Node.js JavaScript运行它时,C库无法用一个symbol lookup error调用,因为我在调试时遇到了问题。

错误如下:

node: symbol lookup error: /var/lib/cloud9/ledscape-wrapper/wrapper/build/Release/wrapper.node: undefined symbol: ledscape_init

wrapper.node是编译过的包,ledscape_init是我试图调用的库中的函数。

我试图跟踪代码,并在多个文件中找到相关的片段。我已经放弃了我认为是无关的台词。

代码语言:javascript
复制
# "AllFade.coffee"
@ledscape = require "./ledscape.js"
@frames[1] = @ledscape.LedscapeInit()
# "Ledscape.coffee"
wrapper = require "./build/Release/wrapper"
module.exports = wrapper

wrapper.cc

代码语言:javascript
复制
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

代码语言:javascript
复制
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_;
};

LedscapeWrapper.cpp

代码语言:javascript
复制
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);
}

binding.gyp

代码语言:javascript
复制
{
  "targets": [{
    "target_name": "wrapper",
    "sources": ["wrapper.cc","LedscapeWrapper.cpp","LedscapeFrameWrapper.cpp"],
    'include_dirs': ['/opt/ledscape/'],
    'link_settings': {  'library_dirs': ['/opt/ledscape']  },
  }],
}

我认为问题在于LedscapeWrapper.cpp中对LedscapeWrapper.cpp的调用之一,它无法找到库(ledscape.h),但我并不主要是一个C/C++开发人员。

我试图查看来自GNU或Node的nm工具,但是它拒绝检查.node文件,也没有在网上找到任何使用指针。

EN

回答 1

Stack Overflow用户

发布于 2022-01-27 20:05:18

发生此问题的原因是,实际程序无法找到动态库(.so文件),我建议的是创建一个动态库并将其添加到当前的查找路径,以便在linux中/usr/lib

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26476513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档