如何从源代码构建nodejs作为共享库

How to build nodejs as a shared library from source code

本文关键字:共享 nodejs 构建 源代码      更新时间:2023-10-16

我需要在我的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