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

时间:2013-04-12 17:58:03

标签: c++ node.js build shared-libraries

我需要在我的c ++项目中包含 node.h ,我尝试使用以下命令从源代码构建节点:

./configure
sudo make

我有一个节点可执行文件和一些目标文件和.a文件,我需要构建为.so文件,以便在我的c ++代码中使用它。

我尝试构建libnode,但是我遇到了cmakelists错误,这不是官方的nodejs项目。

如果有人知道如何从源代码构建nodejs,因为.so文件会很棒,similar question in a google group但答案是行不通的。

4 个答案:

答案 0 :(得分:3)

这就是我在Windows中的表现。除了构建过程,一切都应该是相同的。

Nodejs使用node-gyp进行构建。您可以阅读this进行构建和安装。或者只是git克隆存储库。

在node-vX.XX.XX中打开node.gyp并找到

'targets': [
    {
      'target_name': 'node',
      'type': 'executable',

executable更改为shared_library

在Windows或其他平台上运行vcbuild.bat,然后按instructions

答案 1 :(得分:3)

在节点主线中添加了对构建为共享库的支持。请参阅PR 6994,特别是comment

我跑了

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$ 

答案 2 :(得分:2)

我认为在静态库中构建更容易,因为共享需要添加' -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

希望这有帮助。

答案 3 :(得分:0)

更新: 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