尝试使用 boost 构建简单的节点插件时,编译器失败并出现此错误
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]
我已经使用 Yeoman Generator 设置了项目:https ://www.npmjs.com/package/generator-napi-module#installation 。该构建适用于生成的代码。我用我的函数替换了生成的代码。
源代码:
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 },
}
}
]
}
搭建环境:
- 操作系统:Windows 10
- 视觉工作室:2019
我尝试在 Visual Studio 2019 上构建与 c++ 项目相同的代码,并且构建成功。我应该如何去解决这个问题?我认为这应该是node-gyp
.