【问题标题】:Error compiling C++/CLI in a Node.JS application在 Node.JS 应用程序中编译 C++/CLI 时出错
【发布时间】:2020-08-19 10:51:57
【问题描述】:

我需要在 Node.JS 应用程序中编译托管 C++ 模块。我在网上读到我需要使用 /clr 标志启用 CLI,并设置 msvs-settings 和 msbuild_settings 属性,但它仍然不适合我。我收到此错误消息:

cl : 命令行错误 D8016: '/clr' 和 '/EHs' 命令行选项不兼容 [C:\MyProject\native-modules\encryption-utilities\addon\ build\encrypt.vcxproj]

我从这个页面得到了信息: https://github.com/nodejs/node-gyp/issues/568

这是我的 binding.gyp。

谢谢。

{
    "targets": [
        {
            "target_name": "encrypt",
            'conditions': [
                ['OS=="mac"', {
                    "xcode_settings": {
                        "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
                        "GCC_ENABLE_CPP_RTTI": "YES",
                        "OTHER_CFLAGS": ["-mmacosx-version-min=10.7", "-std=c++11", "-stdlib=libc++"],
                        "OTHER_CPLUSPLUSFLAGS": ["-mmacosx-version-min=10.7", "-std=c++11", "-stdlib=libc++"]
                    },
                    'defines': [
                        'LINUX_DEFINE',
                        '_MAC',
                        '_ULONGLONG',
                    ],
                    "cflags": ["-Wall", "-std=c++11", '-fexceptions', '-Wno-writable-strings'],
                    "cflags!": ['-fno-exceptions', '-Wwritable-strings'],
                    "cflags_cc!": ['-fno-exceptions', '-Wwritable-strings'],
                    "cflags_cc": ['-fexceptions', '-Wno-writable-strings'],
                    "include_dirs": [
                        "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include"
                    ]
                }],
                ['OS=="win"', {
                    'defines+': [
                        'WIN_DEFINE',
                        'INCLUDE_SIGNATURE_ENCRYPTION',
                        'WIN64',
                        'WIN32',
                        '_WIN32',
                        "_WIN32_WINNT=0x0601",
                        "_WINSOCK_DEPRECATED_NO_WARNINGS",
                        "_HAS_EXCEPTIONS=2",
                        "_SCL_SECURE_NO_WARNINGS",
                        "UNICODE",
                        "_UNICODE",
                        "_NO_ASYNCRTIMP",
                        "BOOST_ERROR_CODE_HEADER_ONLY"
                    ],
                    'msbuild_toolset': 'v141',

                    {
                      'cflags_cc': [
                        '/clr'
                      ],
                    }],

                    'msvs_settings': {
                      'VCCLCompilerTool': {
                        'RuntimeLibrary':'MultiThreadedDLL'
                      }
                    },
                    'msbuild_settings': {
                      'ClCompile': {
                        'CompileAsManaged':'true' # can be safe, pure
                      }
                    },


                    "include_dirs": [
                        "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared",
                    ],
                    # "run_as": {
                    #     "action": ['C:/Program Files/nodejs/node.exe', '$(ProjectDir)/../../out/encrypt.js', ],
                    # },
                    "configurations": {
                        "Debug": {
                            'defines': [
                                'DEBUG',
                                '_DEBUG'
                            ],
                            'library_dirs': [
                            ],
                        },
                        "Release": {
                            'defines': [
                                'NDEBUG',
                            ],
                            'library_dirs': [
                            ],
                        }
                    },

                    "libraries": [
                        "crypt32.lib",
                        "winhttp.lib",
                        "httpapi.lib",
                        "bcrypt.lib",
                        "ws2_32.lib",
                        "iphlpapi.lib"
                    ]
                }]
            ],
            "sources": [
                "hello.cc",
                "aes.cpp",
                "base64.cpp",
                "crypt.cpp",
                "StringUtil.cpp"
            ],
            "cflags": ["-Wall", "-std=c++11"],
            "cflags!": ['-fno-exceptions'],
            "cflags_cc!": ['-fno-exceptions'],
            "include_dirs": [
                "<!(node -e \"require('nan')\")",
                "addon",
                "addon/include",
                "../addon/include",
                "C:/Projects/C++/vcpkg/installed/x64-windows/include",
                "include",
                ".."
            ]
        },
        {
            "target_name": "copy_binary",
            "type": "none",
            "dependencies": ["encrypt"],
            'conditions': [
                [
                    'OS=="mac"', {
                        "copies": [
                            {
                                "destination": "<(module_root_dir)/mac/",
                                "files": ["<(module_root_dir)/build/Release/encrypt.node"]
                            }
                        ],
                    }
                ],
                [
                  'OS=="win"', {
                    'msbuild_toolset': 'v141',
                    {
                  'cflags_cc': [
                    '/clr'
                  ],
                }],



                'msvs_settings': {
                  'VCCLCompilerTool': {
                    'RuntimeLibrary':'MultiThreadedDLL'
                  }
                },
                'msbuild_settings': {
                  'ClCompile': {
                    'CompileAsManaged':'true' # can be safe, pure
                  }
                },



                        "configurations": {
                            "Debug": {
                                "copies": [
                                    {
                                        "destination": "<(module_root_dir)/win32/",
                                        "files": ["<(module_root_dir)/build/Debug/encrypt.node", "<(module_root_dir)/build/Debug/encrypt.pdb"]
                                    }
                                ]
                            },
                            "Release": {
                                "copies": [
                                    {
                                        "destination": "<(module_root_dir)/win32/",
                                        "files": ["<(module_root_dir)/build/Release/encrypt.node", "<(module_root_dir)/build/Release/encrypt.pdb"]
                                    }
                                ]
                            },
                        }
                    }
                ],
            ]
        }
    ]
}

【问题讨论】:

    标签: node.js c++-cli clr node-gyp node.js-addon


    【解决方案1】:

    我对 node.js 或 .gyp 文件了解不多,但我知道您遇到的错误。 D8016 表示“/clr 隐含 /EHa,您不能使用 /clr 指定任何其他 /EH 编译器选项”。当您导入旧项目文件或将/clr 添加到现有项目时,可能会出现此问题。

    修复它的官方方法是在 Visual Studio 中打开有问题的 .vcxproj 项目文件,导航到项目 Properties / Config Properties / C/C++ / Code Generation 并更改 Enable C++ 异常从“是的外部 C 函数 (/EHs)”到“是的 SEH 异常 (/EHa)”。

    要在 VS 之外执行此操作,请在任何文本(或 xml)编辑器中打开 .vcxproj 文件并删除以下行:

    <ExceptionHandling>SyncCThrow</ExceptionHandling>
    

    或将它们替换为:

    <ExceptionHandling>Async</ExceptionHandling>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-27
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      相关资源
      最近更新 更多