I’m currently trying to integrate a C++ lib with SWIG which is a wrapper generator for various language (UseSWIG). I want to integrate to golang and to do so, I’m using cmake to build it easily.
My cmake is working when I’m building and it generates the wrapper file and the go file for having my go module.
But in MSVC, I get some errors from the generated file. I also tried to directly invoke my go module (by go run …) but it was missing libs which is why I choose to build it with Cmake in the first place(link ext libs, etc…) for instance linking the <fmt/format.h>
which is get by vcpkg in the 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)
Errors message:
5>Swig compile go-integration/basic_host/basic_host.i for go 5>basic_hostGO_wrap.cxx 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(204,36): error C3646: '__attribute__': unknown override specifier 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(204,50): error C2059: syntax error: '(' 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(204,58): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(212,7): warning C4551: function call missing argument list 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(222,5): error C2065: '__packed__': undeclared identifier 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(222,22): error C2146: syntax error: missing ';' before identifier 'a' 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(222,22): error C2065: 'a': undeclared identifier 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(223,3): error C2065: 'a': undeclared identifier 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(224,27): error C2065: 'a': undeclared identifier 5>..._buildbasic_hostbasic_hostGO_wrap.cxx(224,43): error C2065: 'a': undeclared identifier
If you have any ideas, I would be glad to hear it.
Answer
Found a way to avoid those errors and the DLL is building fine for SWIG in MSVC:
Add those lines at the beginning of your .i file:
%begin %{ #define __attribute__(x) %} %include <windows.i>