带有 Swig 和 go-integration 包装器错误的 Cmake

Cmake with Swig and go-integration wrapper errors

我目前正在尝试将 C++ 库与 SWIG 集成,SWIG 是各种语言的包装器生成器 (UseSWIG)。我想集成到 golang 并这样做,我正在使用 cmake 轻松构建它。

我的 cmake 在构建时正在运行,它生成包装文件和 go 文件以包含我的 go 模块。

但是在 MSVC 中,我从生成的文件中得到了一些错误。我还尝试直接调用我的 go 模块(通过 go 运行 ...)但是它缺少库,这就是为什么我首先选择使用 Cmake 构建它(link ext 库等...) 例如 linking <fmt/format.h> 由 vcpkg 在 cmake 中获取。

CMakeLists:

...
...
# SWIG integration
find_package(SWIG 4.0.2)
if(SWIG_FOUND)
    message("Swig integration")
    include (UseSWIG)
    set(IPATH "go-integration/basic_host")
    set (SWIG_FILE "basic_host.i")
    set_source_files_properties(${IPATH}/${SWIG_FILE} PROPERTIES CPLUSPLUS ON)
    
    #library for Go
    find_program(GO_EXECUTABLE go)
    if (GO_EXECUTABLE)
        message("Bindings for Go will be generated using ${GO_EXECUTABLE}")
        set(SWIG_MODULE_NAME "bhost-go")
        set(CMAKE_SWIG_FLAGS -c++ -go -cgo -intgosize 64)
        message("CMAKE_SWIG_FLAGS: ${CMAKE_SWIG_FLAGS}")

        swig_add_library(${SWIG_MODULE_NAME} 
            TYPE SHARED 
            LANGUAGE go 
            OUTPUT_DIR basic_host
            SOURCES ${IPATH}/${SWIG_FILE}
        )

        target_include_directories(${SWIG_MODULE_NAME} PRIVATE ${HIDAPI_INCLUDE_DIR})

        swig_link_libraries(${SWIG_MODULE_NAME} PUBLIC stduuid)
        swig_link_libraries(${SWIG_MODULE_NAME} PUBLIC fmt::fmt-header-only)
        swig_link_libraries(${SWIG_MODULE_NAME} PUBLIC nlohmann_json nlohmann_json::nlohmann_json)

        set_target_properties(${SWIG_MODULE_NAME} PROPERTIES SWIG_USE_TARGET_INCLUDE_DIRECTORIES TRUE)
    else()
        message("Go executable not found, golang integration not done")
    endif()
else()
    message("Swig not found, integration not done")
endif()


basic_host.i

%module basic_host
%{
    #include <atomic>
    #include <map>
    #include <memory>
    #include <iostream>

    #include <fmt/format.h>
    #include "../../examples/basic_host/command_line.h"
    #include "../../examples/basic_host/command_line.cpp"
%}
%include <std_string.i>

%include "../../examples/basic_host/command_line.h"

basic_host.go

// Removed because useless (see solution)

basic_hostGO_wrap.cxx:

// Removed because useless (see solution)

错误信息:

5>Swig compile go-integration/basic_host/basic_host.i for go
5>basic_hostGO_wrap.cxx
5>...\_build\basic_host\basic_hostGO_wrap.cxx(204,36): error C3646: '__attribute__': unknown override specifier
5>...\_build\basic_host\basic_hostGO_wrap.cxx(204,50): error C2059: syntax error: '('
5>...\_build\basic_host\basic_hostGO_wrap.cxx(204,58): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
5>...\_build\basic_host\basic_hostGO_wrap.cxx(212,7): warning C4551: function call missing argument list
5>...\_build\basic_host\basic_hostGO_wrap.cxx(222,5): error C2065: '__packed__': undeclared identifier
5>...\_build\basic_host\basic_hostGO_wrap.cxx(222,22): error C2146: syntax error: missing ';' before identifier 'a'
5>...\_build\basic_host\basic_hostGO_wrap.cxx(222,22): error C2065: 'a': undeclared identifier
5>...\_build\basic_host\basic_hostGO_wrap.cxx(223,3): error C2065: 'a': undeclared identifier
5>...\_build\basic_host\basic_hostGO_wrap.cxx(224,27): error C2065: 'a': undeclared identifier
5>...\_build\basic_host\basic_hostGO_wrap.cxx(224,43): error C2065: 'a': undeclared identifier

如果您有任何想法,我很乐意听取。

找到了避免这些错误的方法,并且 DLL 在 MSVC 中为 SWIG 构建良好:

在 .i 文件的开头添加这些行:

%begin %{
#define __attribute__(x)
%}

%include <windows.i>