本文介绍了在 node-sqlite3 中使用 Gyp 中的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Gyp 的新手.我不想编译我的依赖项,而是想使用共享库,特别是我机器上已经存在的 libsqlite3.so.目前主要的 binding.gyp 看起来像

{目标":[{'target_name': 'node_sqlite3',来源":['src/database.cc','src/node_sqlite3.cc','src/statement.cc'],依赖关系":['deps/sqlite3/binding.gyp:sqlite3']}]}

如何更改它以便使用共享的 sqlite3 库?deps 文件夹中的 binding.gyp 有一个如下所示的部分.我认为我不需要 gyp 来为我编译任何 sqlite3,因此将类型切换为 shared_library 可能不是正确的答案.

'目标':[{'target_name': 'sqlite3','type': 'static_library','include_dirs': [ '.'],'direct_dependent_settings':{'include_dirs': [ '.'],'定义':['SQLITE_THREADSAFE=1','SQLITE_ENABLE_FTS3','SQLITE_ENABLE_RTREE'],},'定义':['_REENTRANT=1','SQLITE_THREADSAFE=1','SQLITE_ENABLE_FTS3','SQLITE_ENABLE_RTREE'],'来源': [ './sqlite3.c', ],},{'target_name': '壳','type': '可执行文件','依赖':['sqlite3'],'来源':['./shell.c']}]}

更新.我能够通过将 binding.gyp 更改为 this 来编译内容

{目标":[{'target_name': 'node_sqlite3',来源":['src/database.cc','src/node_sqlite3.cc','src/statement.cc'],'ldflags': ['-lsqlite3']}]}

但是,当我使用该模块运行程序时,我得到

节点:符号查找错误:/usr/local/lib/node_modules/sqlite3/build/Release/node_sqlite3.node:未定义符号:sqlite3_open_v2

好像共享库没有加载或不可访问.我想我很接近了.libsqlite3 安装到/usr/local/lib

/usr/local/lib$ lslibsqlite3.a libsqlite3.so libsqlite3.so.0.8.6 node_modules python2.7libsqlite3.la libsqlite3.so.0 节点 pkgconfig

Update2. 情节变厚了.我在 node-sqlite3 创建的可执行文件上尝试了 ldd

 linux-vdso.so.1 =>(0x00007ffffd7168000)libstdc++.so.6 =>/usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc9451df000)libpthread.so.0 =>/lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc944fc2000)libc.so.6 =>/lib/x86_64-linux-gnu/libc.so.6 (0x00007fc944c04000)libm.so.6 =>/lib/x86_64-linux-gnu/libm.so.6 (0x00007fc94490a000)/lib64/ld-linux-x86-64.so.2 (0x00007fc945704000)libgcc_s.so.1 =>/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc9446f4000)

显然缺少 libsqlite3.所以也许我的 ldflags 声明并没有真正按计划工作.

解决方案

答案在这里.

{目标":[{'target_name': 'node_sqlite3',来源":['src/database.cc','src/node_sqlite3.cc','src/statement.cc'],链接设置":{图书馆":['-lsqlite3']}}]}

使用 ldd 时:

~/node-sqlite3/build/Release$ ldd node_sqlite3.nodelinux-vdso.so.1 => (0x00007fffe9548000)> libsqlite3.so.0 =>/usr/local/lib/libsqlite3.so.0 (0x00007f6649504000)libstdc++.so.6 =>/usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f66491ff000)libpthread.so.0 =>/lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f6648fe1000)libc.so.6 =>/lib/x86_64-linux-gnu/libc.so.6 (0x00007f6648c24000)libdl.so.2 =>/lib/x86_64-linux-gnu/libdl.so.2 (0x00007f6648a20000)libm.so.6 =>/lib/x86_64-linux-gnu/libm.so.6 (0x00007f6648725000)/lib64/ld-linux-x86-64.so.2 (0x00007f66499cd000)libgcc_s.so.1 =>/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f664850f000)

I'm new to Gyp. Instead of compiling my dependency, I would like to use a shared library, in particular, the libsqlite3.so which is already on my machine. The main binding.gyp currently looks like

{
  'targets': [
    {
      'target_name': 'node_sqlite3',
      'sources': [
        'src/database.cc',
        'src/node_sqlite3.cc',
        'src/statement.cc'
      ],
      'dependencies': [
        'deps/sqlite3/binding.gyp:sqlite3'
      ]
    }
  ]
}

How do I change this so that a shared sqlite3 library is used? The binding.gyp in the deps folder has a section that looks like below. I don't think I need gyp to do any compilation of sqlite3 for me, so switching type to shared_library is probably not the right answer.

'targets': [
    {
      'target_name': 'sqlite3',
      'type': 'static_library',
      'include_dirs': [ '.' ],
      'direct_dependent_settings': {
        'include_dirs': [ '.' ],
        'defines': [
          'SQLITE_THREADSAFE=1',
          'SQLITE_ENABLE_FTS3',
          'SQLITE_ENABLE_RTREE'
        ],
      },
      'defines': [
        '_REENTRANT=1',
        'SQLITE_THREADSAFE=1',
        'SQLITE_ENABLE_FTS3',
        'SQLITE_ENABLE_RTREE'
      ],
      'sources': [ './sqlite3.c', ],
    },

    {
      'target_name': 'shell',
      'type': 'executable',
      'dependencies': [ 'sqlite3' ],
      'sources': [ './shell.c' ]
    }
  ]
}

Update. I was able to get things to compile by changing by binding.gyp to this

{
  'targets': [
    {
      'target_name': 'node_sqlite3',
      'sources': [
        'src/database.cc',
        'src/node_sqlite3.cc',
        'src/statement.cc'
      ],
      'ldflags': [
        '-lsqlite3'
      ]
    }
  ]
}

However, when I go to run a program using the module, I get

as if the shared library is not loading or is not accessible. I think I'm close. libsqlite3 was installed to /usr/local/lib

Update2. The plot thickens. I tried ldd on the executable created by node-sqlite3

Clearly missing libsqlite3. So perhaps my ldflags statement did not really work as planned.

解决方案

Here's the answer.

{
  'targets': [
    {
      'target_name': 'node_sqlite3',
      'sources': [
        'src/database.cc',
        'src/node_sqlite3.cc',
        'src/statement.cc'
      ],
      'link_settings': {
          'libraries': [
              '-lsqlite3'
          ]
      }
    }
  ]
}

Upon use of ldd:

这篇关于在 node-sqlite3 中使用 Gyp 中的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 03:46
查看更多