如何从源代码构建nodejs作为共享库
How to build nodejs as a shared library from source code
我需要在我的c++项目中包含node.h,我试图使用
从源代码构建节点:./configure
sudo make
我得到了一个节点可执行文件和一些对象文件和。a文件,我需要构建为。so文件在我的c++代码中使用它。
我试图构建libnode,但我得到了cmakelist错误,这不是官方的nodejs项目。
如果有人知道如何从源代码构建nodejs作为。so文件将是伟大的,一个类似的问题在谷歌组,但答案是不工作。
支持作为共享库构建已经添加到节点主线中。请参阅PR 6994,特别是这条评论。
I just run
git clone https://github.com/nodejs/node.git
cd node
git checkout v6.9.4
./configure --shared
make -j4
生产:
ubuntu@server:~/node$ find . -name libnode.so* -exec ls -la {} ;
-rwxrwxr-x 2 ubuntu ubuntu 31576776 Jan 6 18:57 ./out/Release/lib.target/libnode.so.48
-rw-rw-r-- 1 ubuntu ubuntu 387 Jan 6 18:57 ./out/Release/.deps/home/ubuntu/node/out/Release/lib.target/libnode.so.48.d
-rw-rw-r-- 1 ubuntu ubuntu 4202 Jan 6 18:57 ./out/Release/.deps/home/ubuntu/node/out/Release/obj.target/libnode.so.48.d
-rwxrwxr-x 2 ubuntu ubuntu 31576776 Jan 6 18:57 ./out/Release/obj.target/libnode.so.48
ubuntu@server:~/node$
我认为在静态库中构建共享库更容易,需要添加'-fpic'。
对于我的项目(在Linux下),我使用这个脚本来构建一个静态node.js库:
#!/bin/sh
# This script is LGPL feel free to use it!
if test ! "$#" = "1"; then
echo "Run with the archive in parameter:"
echo "t${0} ./node-v0.XX.XX.tar.gz"
echo "nIt will build a ./libnode_static.a in current dir"
return
fi
HERE=$PWD
#Extract Tarball
tar xf $1 | exit 1
DIRNAME=`echo $1 | sed s/.tar.gz//g`
cd $DIRNAME
#Patch node.gyp to build in static
sed -i "s/'type': 'executable',/'type': 'static_library',/g" ./node.gyp
#Patch node_main.cc to rename the main in node_main
sed -i "s/int main(/int node_main(/g" ./src/node_main.cc
#Build Node.js
./configure
make -j8
#Move to build directory
cd ./out/Release
#Extract .a
#Cleanup if previous build
rm -fr *.tmpd
echo "== Extracting *.a =="
#Make sure we create a directory
#for each.a as some .o might
#have the same name
for a in `ls *.a`
do
echo "t${a}..."
mkdir "$a.tmpd"
cd "$a.tmpd"
ar x ../$a
cd ..
done
#Repack in a single .a
find . -iname "*.o" | xargs ar rcs libnode_static.a
#Cleanup
rm -fr *.tmpd
echo "== DONE =="
#Move in start directory
mv ./libnode_static.a ${HERE}/
cd ${HERE}
#Sanity CHECK
echo "== Performing Sanity Check =="
TMP_FILE=`mktemp /tmp/XXXXXX.cxx`
TMP_EXE=`mktemp /tmp/XXXXXX`
cat << . > ${TMP_FILE}
int node_main( int argc, char **argv);
int main(int argc, char ** argv )
{
node_main( argc, argv );
return 0;
}
.
#Try compiling
g++ ${TMP_FILE} -o ${TMP_EXE} -lnode_static -ldl -pthread -L.
#Try running
RET=`${TMP_EXE} -e "console.log('okfromnode')"`
if test "x${RET}" = "xokfromnode"; then
echo "== Sanity check OK =="
else
echo "== Sanity check FAILED =="
exit 1
fi
rm ${TMP_FILE} ${TMP_EXE}
echo "== Node.js is now built statically in ./libnode_static.a =="
exit 0
按如下命令运行:
sh script.sh node-v0.10.XX.tar.gz
如果一切顺利,您应该得到一个libnode_static。a在当前目录。
与如下代码一起使用:
int node_main( int argc, char **argv);
int main(int argc, char ** argv )
{
/* Here we spawn a node.js instance */
return node_main( argc, argv );
}
然后像这样编译:
g++ ./test.cxx -o ./my_node -lnode_static -ldl -pthread -L.
你有嵌入节点:
./my_node -e "console.log('Hello World')"
#Outputs
Hello World
我就是这样做的。除了构建过程之外,其他一切都应该是相同的。
Nodejs使用node- yp进行构建。您可以阅读本文以进行构建和安装。或者直接克隆仓库。
打开节点。在节点- vx . xx。查找
'targets': [
{
'target_name': 'node',
'type': 'executable',
将executable
改为shared_library
在windows或其他平台上运行vcbuild.bat。
更新:https://gist.github.com/aklen/849f3460b7a028c9aed8a84e1d4cecb7
Windows.vcbuild release vs2017 dll x64
.vcbuild release vs2017 dll x86
.vcbuild debug vs2017 dll x64
.vcbuild debug vs2017 dll x86
Linux/MacOS
./configure --shared --release
make -j4
./configure --shared --debug
make -j4
其他构建选项
https://github.com/nodejs/node/blob/master/BUILDING.md相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 在cuda线程之间共享大量常量数据
- 如何从具有移动语义的类对象中生成共享指针
- 在c代码之间共享数据的最佳方式
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 将静态库链接到不带-fPIC的共享库中
- 为什么std::互斥需要很长的、非常不规则的时间来共享
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 无法在Ubuntu上将共享库与Eclipse链接
- 从python调用openMP共享库时,未定义opnMP函数
- 在为LINUX创建共享库时,如何避免STL的私有/弱副本
- 为什么我的共享库中存在展开符号
- 使用共享指针的函数调用,其对象应为 const
- 具有两个独占锁组的共享锁
- 共享队列的线程安全
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- nodejs插件错误:无法加载共享库
- nodejs C++共享对象
- 如何从源代码构建nodejs作为共享库