How to build nodejs as a shared library from source code

谁说我不能喝 提交于 2021-02-07 04:13:29

问题


I need to include node.h in my c++ project, I tried to build node from source code using:

./configure
sudo make

I got a node executable and some object files and .a files, I need to build as .so file to use it in my c++ code.

I tried to build libnode, but I got cmakelists error and this is not official nodejs project.

if anybody know how to build nodejs from source code as .so file will be great, a similar question in a google group but the answer is not working.


回答1:


I think it is easier to build in a static library as shared requires the addition of '-fpic'.

For my projects (under Linux) I use this script to built a static node.js library:

#!/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

Run it as follows :

sh script.sh  node-v0.10.XX.tar.gz

If everything goes well you should get a libnode_static.a in current directory.

Use it with a code like this:

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 );
}

And compile like this:

g++ ./test.cxx -o ./my_node -lnode_static -ldl -pthread -L. 

And you have embedded node :

./my_node  -e "console.log('Hello World')"
#Outputs
Hello World

Hope this helps.




回答2:


Support for building as a shared library has been added in to node mainline. Please see PR 6994 and specifically this comment.

I just ran

git clone https://github.com/nodejs/node.git
cd node
git checkout v6.9.4
./configure --shared
make -j4

which produced:

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$ 



回答3:


This is how i did it in windows. Except for the build procedures, everything should be same.

Nodejs uses node-gyp for building. You can read this for building and installation. Or just git clone the repository.

Open node.gyp in the node-vX.XX.XX and find

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

change the executable to shared_library.

Run vcbuild.bat in windows or for other platforms follow instructions.




回答4:


Update: 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

Other build options

https://github.com/nodejs/node/blob/master/BUILDING.md



来源:https://stackoverflow.com/questions/15977901/how-to-build-nodejs-as-a-shared-library-from-source-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!