-->

Using shared library in Gyp in node-sqlite3

2019-03-20 17:51发布

问题:

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

node: symbol lookup error: /usr/local/lib/node_modules/sqlite3/build/Release/node_sqlite3.node: undefined symbol: sqlite3_open_v2

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

/usr/local/lib$ ls
libsqlite3.a   libsqlite3.so    libsqlite3.so.0.8.6  node_modules  python2.7
libsqlite3.la  libsqlite3.so.0  node                 pkgconfig

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

    linux-vdso.so.1 =>  (0x00007fffd7168000)
    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)

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

回答1:

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/build/Release$ ldd node_sqlite3.node linux-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)