自从在 Golang 中删除了一些 C 之后,SWIG for go 是否已经过时了?

Has SWIG for go become outdated since the removal of some C in Golang?

我一直在尝试为 open-vcdiff 生成一个 Go 包装器,所以我通过以下尝试第一次了解了 SWIG:

godelta.swig

%module godelta
%include "include/godelta.hpp"

godelta.hpp

#include "config.h"
#include <assert.h>
#include <errno.h>
#ifdef WIN32
#include <fcntl.h>
#include <io.h>
#endif  // WIN32
#include <stdio.h>
#include "google/vcdecoder.h"
#include "google/vcencoder.h"
#include "google/jsonwriter.h"
#include "google/encodetable.h"
#include "unique_ptr.h" // auto_ptr, unique_ptr

#ifndef HAS_GLOBAL_STRING

#endif  // !HAS_GLOBAL_STRING

static const size_t kDefaultMaxTargetSize = 1 << 26;      // 64 MB

namespace godelta {

    class Godelta {
    public:
        Godelta();
        ~Godelta();

        bool Encode();
        bool Decode();
        bool DecodeAndCompare();

        int opt_buffersize;
        int opt_max_target_window_size;
        int opt_max_target_file_size;

    private:
        input file
        static bool FileSize(FILE* file, size_t* file_size);
        bool OpenFileForReading(const string& file_name,
                            const char* file_type,
                            FILE** file,
                            std::vector<char>* buffer);
        bool OpenDictionary();
        bool OpenInputFile() {
            return OpenFileForReading(input_file_name_,
                                  input_file_type_,
                                  &input_file_,
                                  &input_buffer_);
        }

        bool OpenOutputFile();
        bool OpenOutputFileForCompare() {
            return OpenFileForReading(output_file_name_,
                                  output_file_type_,
                                  &output_file_,
                                  &compare_buffer_);
        }

        bool ReadInput(size_t* bytes_read);
        bool WriteOutput(const string& output);
        bool CompareOutput(const string& output);

        std::vector<char> dictionary_;

        UNIQUE_PTR<open_vcdiff::HashedDictionary> hashed_dictionary_;

        const char* input_file_type_;
        const char* output_file_type_;

        string input_file_name_;
        string output_file_name_;

        FILE* input_file_;
        FILE* output_file_;

        std::vector<char> input_buffer_;
        std::vector<char> compare_buffer_;

        Godelta(const Godelta&);  // NOLINT
        void operator=(const Godelta&);
    };

} // namespace godelta

Swig 运行良好,生成文件 godelta_wrap.cxxgodelta_gc.cgodelta.go。我现在面临的问题是它正在生成包含:

#include "runtime.h"
#include "cgocall.h"

导致错误:

godelta_gc.c:2:10: fatal error: 'runtime.h' file not found

我到处找那个文件,甚至搜索了 GitHub 上的 Go 存储库。我只是在 Go 中找到了那个文件的替代品。

对了,我的go版本是:

go version go1.9 darwin/amd64

叮当声:

Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

SWIG:

SWIG Version 3.0.12
Compiled with g++ [x86_64-apple-darwin16.7.0]
Configured options: +pcre

任何关于这个主题的见解都将受到高度赞赏,特别是如果它使我能够 运行 go build 在包裹上没有这个错误:-)

我的 Swig 命令似乎缺少一个标志。我原来的命令是这样的:

swig -go -intgosize 64 godelta.swig

但正如我最终提交的 issue 中指出的那样,我应该包括 -c++-cgo,最后是:

swig -c++ -go -cgo -intgosize 64 godelta.swig

现在工作正常:-)