【问题标题】:How to build nodejs as a shared library from source code如何从源代码构建 nodejs 作为共享库
【发布时间】:2013-04-05 08:48:23
【问题描述】:

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

./configure
sudo make

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

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

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

【问题讨论】:

  • 我得到这个错误:配置:错误:没有这样的选项:--enable-shared
  • 好的。那么你在裸./configure 期间看到任何警告吗?
  • 不,他们没有任何警告。

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


【解决方案1】:

节点主线已添加对构建为共享库的支持。请参阅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】:

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

    希望这会有所帮助。

    【讨论】:

    • 4 年后,它仍然可以正常工作,非常棒,非常感谢
    【解决方案3】:

    这就是我在 Windows 中的做法。除了构建过程,一切都应该是一样的。

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

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

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

    executable 更改为shared_library

    在 windows 或其他平台上运行 vcbuild.bat 关注instructions

    【讨论】:

    • 在windows下检查一下告诉你,其实我在linux下试过这个解决方案,没用。
    • 我在 Linux (Debian) 上试过这个。但是我构建了static_library 而不是shared_library。要编译,您必须在项目中将包含路径设置为 path/to/nodejs/deps/uv/include、path/to/nodejs/deps/v8/include、path/to/nodejs/src。您还必须设置 nodejs 使用的库:libnode.a、libv8_base.a、libuv.a、libhttp_parser.a、libchrome_zlib.a、libopenssl.a libcares.a、libv8_snapshot.a。
    • 在 node-v0.10.32 上测试
    【解决方案4】:

    更新: https://gist.github.com/aklen/849f3460b7a028c9aed8a84e1d4cecb7

    窗口

    .\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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2020-10-13
      • 2018-08-22
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多