Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: SWIG doesn't work with MSVC #1886

Closed
phanirithvij opened this issue Oct 11, 2020 · 3 comments
Closed

Go: SWIG doesn't work with MSVC #1886

phanirithvij opened this issue Oct 11, 2020 · 3 comments
Labels

Comments

@phanirithvij
Copy link

I see an issue back from 2013 which might be related #100

Sample code

/* file: example.i */
%module example
%include <windows.i>
%include "example.h"
// file: example.h
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_

#ifndef DLLAPI
#define DLLAPI extern "C" __declspec(dllexport)
#endif

DLLAPI bool example_func();

#endif // _EXAMPLE_H_
/* file: example.cc */
#include "example.h"
#include <iostream>

bool example_func(){
    std::cout << "example" << std::endl;
    return true;
}

Now swig.exe -c++ -go -intgosize 32 example.i generates example_wrap.cxx

Now in a cmd.exe
vcvars32.bat then cl.exe example.cc example_wrap.cxx gives the following errors

Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28314 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

example.cc
example_wrap.cxx
example_wrap.cxx(204): error C3646: '__attribute__': unknown override specifier
example_wrap.cxx(204): error C2059: syntax error: '('
example_wrap.cxx(204): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
example_wrap.cxx(212): warning C4551: function call missing argument list
example_wrap.cxx(222): error C2065: '__packed__': undeclared identifier
example_wrap.cxx(222): error C2146: syntax error: missing ';' before identifier 'a'
example_wrap.cxx(222): error C2065: 'a': undeclared identifier
example_wrap.cxx(223): error C2065: 'a': undeclared identifier
example_wrap.cxx(224): error C2065: 'a': undeclared identifier
example_wrap.cxx(224): error C2065: 'a': undeclared identifier
example_wrap.cxx(274): error C3861: 'example_func': identifier not found
Generating Code...

Also, I read on stackoverflow that only 32 bit compiling is supported on windows https://stackoverflow.com/a/17429595/8608146 is this still true in 2020 (I'm not able to verify it because of the above errors which also occur when using 64 bit i.e. vcvars64.bit and -gointsize 64)

@ojwb
Copy link
Member

ojwb commented Jan 26, 2022

The problem is this code in Lib/go/goruntime.swg:

#ifndef SWIGGO_GCCGO
// Set the host compiler struct attribute that will be
// used to match gc's struct layout. For example, on 386 Windows,
// gcc wants to 8-align int64s, but gc does not.
// Use __gcc_struct__ to work around http://gcc.gnu.org/PR52991 on x86,
// and https://golang.org/issue/5603.
// See: https://github.com/golang/go/blob/fcbf04f9b93b4cd8addd05c2ed784118eb50a46c/src/cmd/cgo/out.go#L663
%insert(runtime) %{
# if !defined(__clang__) && (defined(__i386__) || defined(__x86_64__))
#   define SWIGSTRUCTPACKED __attribute__((__packed__, __gcc_struct__))
# else
#   define SWIGSTRUCTPACKED __attribute__((__packed__))
# endif
%}
#else
# define SWIGSTRUCTPACKED
#endif

The linked file there seems to show that cgo also assumes __attribute__((__packed__)) works, but I guess it doesn't with MSVC.

I'm not familiar enough with Go to give reliable advice here, but I wonder if MSVC just isn't supported by Go. If you know how to specify the equivalent of __attribute__((__packed__)) (might be declspec something) then you could try adding a special case for MSVC to the code I quoted above. If that works, please open a pull request with that change.

@ojwb ojwb changed the title Go: SWIG doesn't work on windows Go: SWIG doesn't work with MSVC Jan 26, 2022
@ojwb ojwb added the Go label Jan 26, 2022
@phanirithvij
Copy link
Author

I believe you are right, cgo doesn't support MSVC yet golang/go#20982.
I was just experimenting with cgo, c++ at that time, and I apologize but I'm not knowledgeable enough to contribute here.

@wsfulton
Copy link
Member

Until Go supports MSVC, also see golang/go#49080, SWIG is not going to support MSVC compilation + Go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants