【问题标题】:Symbols are not loading on dynamic linking of wfdb library using node-gyp on macOS High Sierra在 macOS High Sierra 上使用 node-gyp 动态链接 wfdb 库时未加载符号
【发布时间】:2019-01-25 04:48:50
【问题描述】:

我正在尝试创建一个依赖于 WFDB 库 (https://www.physionet.org/physiotools/wfdb.shtml) 的动态库。 我的 C++ 代码如下所示:

#include <stdio.h>
#include <iostream>
#include <vector>
#include <vector>
extern "C" {
    #include <wfdb/wfdb.h>
}

#include "./sample_wfdb.h"

int add(int a, int b){
    return a + b;
}

int read(){
    int i, nsig;
    WFDB_Siginfo* siarray;
    WFDB_Sample* v;
    nsig = isigopen("/data/100s", NULL, 0);
    if (nsig < 1)
        exit(1);
    siarray = (WFDB_Siginfo*)malloc(nsig * sizeof(WFDB_Siginfo));
    nsig = isigopen("data/100s", siarray, nsig);
    for (i = 0; i < nsig; i++)

        v = (WFDB_Sample*)malloc(nsig * sizeof(WFDB_Sample));

    if (v == NULL) {
        fprintf(stderr,"%s: insufficient memory\n");
        exit(2);
    }
    std::vector<int> signal1, signal2;
    for (int i = 0; i < siarray[0].nsamp; i++) {
        if (getvec(v) < 0)
            break;
        for (int j = 0; j < nsig; j++) {
            if (j == 0) {
                signal1.push_back(v[j]);
            }
            if (j == 1) {
                signal2.push_back(v[j]);
            }
        }
    }
    int sig_size = signal1.size();
    std::cout << sig_size << std::endl;
    return sig_size;
}

这个头文件是这样的

#ifdef __linux__
  extern "C" int read();
  extern "C" int add(int x, int y);
#elif _WIN32
  extern "C" __declspec(dllexport) extern "C" int read();
  extern "C" __declspec(dllexport) int add(int x, int y);
#elif __APPLE__
  extern "C" int read();
  extern "C" int add(int x, int y);
#endif

绑定。吉普是

{
  "targets": [
    {
      "target_name": "wfdb-test",
      "type": "shared_library",
      "libraries": [ "/usr/local/lib/libwfdb.10.6.0.dylib"] ,
      "sources": [ "sample_wfdb.cpp -lwfdb" ],
      "cflags!": [ "-fno-exceptions" ],
      "cflags": [ "-std=c++11" ],
      "cflags_cc!": [ "-fno-exceptions" ]
    }
  ]
}

在使用 node-gyp rebuild 运行项目时。我得到以下输出。

gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.11.3 | darwin | x64
gyp info spawn /usr/local/bin/python2
gyp info spawn args [ '/usr/local/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',gyp info spawn args   '/Users/abhinashkumarjha/Desktop/cpp-codes/build/config.gypi',
gyp info spawn args   '-I',gyp info spawn args   '/usr/local/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/abhinashkumarjha/.node-gyp/8.11.3/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/abhinashkumarjha/.node-gyp/8.11.3',
gyp info spawn args   '-Dnode_gyp_dir=/usr/local/lib/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/abhinashkumarjha/.node-gyp/8.11.3/<(target_arch)/nod
e.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/abhinashkumarjha/Desktop/cpp-codes',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  SOLINK(target) Release/wfdb-test.dylib
gyp info ok

并在./build/Release/wfdb-test.dylib下生成动态库

在生成的 dylib 文件中查找符号时:

nm -a build/Release/wfdb-test.dylib

我明白了

U dyld_stub_binder

这是默认的 cpp 符号(我希望这里有更多符号。)。谁能帮我解决我哪里出错了。

开发环境详情如下:

os: mac OS High Sierra ( v10.13.6 )
node: v8.11.3
node-gyp: v3.8.0

【问题讨论】:

    标签: c++ node.js dynamic-linking node-gyp missing-symbols


    【解决方案1】:

    如果这看起来很愚蠢,请原谅我,但是 main() 函数在哪里?代码还有其他入口点吗?

    Is it possible to write a program without using main() function?

    此外,还有 cflags!和 cflags_cc!设置为-fno-exceptions,编译是否有可能一直进行而没有抛出正确的异常?

    https://blog.mozilla.org/nnethercote/2011/01/18/the-dangers-of-fno-exceptions/

    【讨论】:

    • 嗨,我正在尝试构建一个共享库。 main 不需要作为整个程序的入口点。请通过sysleaf.com/nodejs-ffi,因为它可以更简单地演示我想要实现的目标。
    【解决方案2】:

    如果你通过 Cmake 创建项目,你应该检查你的 CMakeLists.txt。某处应该是这一行:

    target_link_libraries(your_project_name wfdb)
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 2019-01-20
      • 2018-12-05
      相关资源
      最近更新 更多