节点C ++模块共享库

时间:2019-06-29 05:03:07

标签: node.js shared-libraries node-gyp node-addon-api

我正试图创建一个Node C ++模块,以与Steam api接口。库文件是./steam/lib/linux64/libsteam_api.so,头文件在./steam中。

我创建了一个小的常规C ++文件进行测试,该文件成功使用了通过#include "steam_api.h"导入的Steam api。我已经像这样编译并导入了共享库:g++ -L./steam/lib/linux64 -Wl,-rpath=./steam/lib/linux64 -Isteam -lsteam_api main.cpp

binding.gyp:

{
 "targets": [ {
  "target_name": "steam",
  "sources": [ "steam.cpp" ],
  "include_dirs": [
   "steam",
   "<!@(node -p \"require('node-addon-api').include\")"
  ],
  "cflags!": [ "-fno-exceptions" ],
  "cflags_cc!": [ "-fno-exceptions" ],
  "libraries": [ "./steam/lib/linux64/libsteam_api.so" ]
 } ]
}

当我尝试使用node-gyp编译Node模块时,得到g ++:错误:./steam/lib/linux64/libsteam_api.so:没有这样的文件或目录

如何正确导入共享库?

2 个答案:

答案 0 :(得分:0)

看来,node-gyp在运行时正在更改当前目录,这会使您的相对路径无效。可以改用绝对路径,也可以做一些实验以找到新的当前目录,然后使用相对于该目录的路径。

答案 1 :(得分:0)

在查看了一些示例和大量的反复试验后,我能够更正binding.gpy:

{
 "targets": [ {
  "target_name": "steam",
  "sources": [ "steam.cpp" ],
  "include_dirs": [
   "steam",
   "<!@(node -p \"require('node-addon-api').include\")"
  ],
  "cflags!": [ "-fno-exceptions" ],
  "cflags_cc!": [ "-fno-exceptions" ],
  "libraries": [
   "-lsteam_api",
   "-L../steam/lib/linux64",
   "-Wl,-rpath=./steam/lib/linux64"
  ]
 } ]
}

库部分需要包含与g ++调用方式类似的参数,不同之处在于“ -L”不同于“ -Wl,-rpath =“,并且g ++输入需要启动一个文件夹级别以解决某些未知问题原因。