c++node.jsboostnode-gypn-api

NodeJS Addon build error C3861: '_alloca': identifier not found


When trying to build a simple node addon with boost,the compiler fails with this error

absolute\path\to\boost_1_76_0\boost_1_76_0\boost\asio\detail\impl\socket_ops.ipp(2481,34): error C3861: '_alloca': identifier not found [absolute\path\to\project\src\nativeSimpl
e\build\nativesimple-native.vcxproj]
absolute\path\to\boost_1_76_0\boost_1_76_0\boost\asio\detail\impl\socket_ops.ipp(2692,32): error C3861: '_alloca': identifier not found [absolute\path\to\project\src\nativeSimpl 
e\build\nativesimple-native.vcxproj]

I have setup the project using the Yeoman Generator: https://www.npmjs.com/package/generator-napi-module#installation. The build works for the generated code. I replaced the generated code with my function.

Source Code:

nativesimple.cc

#include <iostream>
#include <napi.h>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using namespace Napi;
using boost::asio::ip::udp;

Napi::String Connect(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    boost::asio::io_context io_context;

    udp::resolver resolver(io_context);
    udp::endpoint sender_endpoint =
        *resolver.resolve(udp::v4(), "127.0.0.1", "20777").begin();

    udp::socket socket(io_context);
    socket.open(udp::v4());
    socket.bind(sender_endpoint);

    std::array<float, 66> recv_buf;
    size_t len = socket.receive_from(
    boost::asio::buffer(recv_buf), sender_endpoint);

    std::cout << std::to_string(recv_buf[29]).c_str() << std::to_string(recv_buf[31]).c_str() << std::endl;

    return Napi::String::New(env, "DONE");
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "udpConnect"),
              Napi::Function::New(env, Connect));
  return exports;
}



NODE_API_MODULE(addon, Init)

binding.gyp

{
  'targets': [
    {
      'target_name': 'nativesimple-native',
      'sources': [ 'src/nativesimple.cc' ],
      'include_dirs': [
        "<!@(node -p \"require('node-addon-api').include\")",
        "absolute\\path\\to\\boost_1_76_0\\boost_1_76_0"
      ],
      'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
      'cflags!': [ '-fno-exceptions' ],
      'cflags_cc!': [ '-fno-exceptions' ],
      'xcode_settings': {
        'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
        'CLANG_CXX_LIBRARY': 'libc++',
        'MACOSX_DEPLOYMENT_TARGET': '10.7'
      },
      'msvs_settings': {
        'VCCLCompilerTool': { 'ExceptionHandling': 1 },
      }
    }
  ]
}

Build environment:

I have tried to build the same code on Visual Studio 2019 as a c++ project and it builds successfully. How should I go resolve this issue? I assume it should be a configuration issue with node-gyp.


Solution

  • boost\asio\detail\impl\socket_ops.ipp doesn't include <malloc.h> which is the required header for the _alloca function.

    You can work around this by including <malloc.h> in your code before including the boost headers.

    You should raise a bug with boost asio with a full example of how to reproduce the issue to get this fixed properly.