[英]How to debug “exit status 3221225477” with swig/cgo?
我正在使用 SWIG 與 golang 的集成來生成 cgo 文件。 我正在鏈接 C++ 庫和 Go。
我已經安裝了我的 package : swig -go -cgo -c++ -intgosize 64./basic_host.i
和go install
並且它發生了。 但是當我在這里啟動我的主要示例時:
package main
import (
"fmt"
"myLib/basic_host"
)
func main() {
fmt.Println("Hello")
basic_host.PrintHelpAndExit("test", 0)
fmt.Println("World!")
}
當我嘗試啟動它時,我得到exit status 3221225477
並且我的打印問候甚至沒有被考慮在內,似乎我的程序甚至沒有啟動......我不知道如何處理這個調試工具是甚至沒有啟動。
退出狀態似乎與 NIL 指針 ref 相關: 請參見此處,肯定是由於 cgo 集成。 我正在使用 MinGW 構建 Windows 10,在我的配置下:
swig -version
SWIG Version 4.0.2
Compiled with i686-w64-mingw32-g++ [i686-w64-mingw32]
Configured options: +pcre
我的 SWIG 接口文件是這樣的(basic_host.i):
%module basic_host
%{
#include <atomic>
#include <map>
#include <iostream>
#include <memory>
#include <myLib/myLib.hpp>
#include "../../examples/basic_host/command_line.h"
#include "../../examples/basic_host/command_line.cpp"
%}
%inline %{
extern void printHelpAndExit(const char* binary, unsigned int exitCode);
%}
這是生成的 go 文件 (basic_host.go):
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 4.0.2
*
* This file is not intended to be easily readable and contains a number of
* coding conventions designed to improve portability and efficiency. Do not make
* changes to this file unless you know what you are doing--modify the SWIG
* interface file instead.
* ----------------------------------------------------------------------------- */
// source: .\basic_host.i
package basic_host
/*
#define intgo swig_intgo
typedef void *swig_voidp;
#include <stdint.h>
typedef long long intgo;
typedef unsigned long long uintgo;
typedef struct { char *p; intgo n; } _gostring_;
typedef struct { void* array; intgo len; intgo cap; } _goslice_;
typedef _gostring_ swig_type_1;
extern void _wrap_Swig_free_basic_host_5ddda8bb50543998(uintptr_t arg1);
extern uintptr_t _wrap_Swig_malloc_basic_host_5ddda8bb50543998(swig_intgo arg1);
extern void _wrap_printHelpAndExit_basic_host_5ddda8bb50543998(swig_type_1 arg1, swig_intgo arg2);
#undef intgo
*/
import "C"
import "unsafe"
import _ "runtime/cgo"
import "sync"
type _ unsafe.Pointer
var Swig_escape_always_false bool
var Swig_escape_val interface{}
type _swig_fnptr *byte
type _swig_memberptr *byte
type _ sync.Mutex
func Swig_free(arg1 uintptr) {
_swig_i_0 := arg1
C._wrap_Swig_free_basic_host_5ddda8bb50543998(C.uintptr_t(_swig_i_0))
}
func Swig_malloc(arg1 int) (_swig_ret uintptr) {
var swig_r uintptr
_swig_i_0 := arg1
swig_r = (uintptr)(C._wrap_Swig_malloc_basic_host_5ddda8bb50543998(C.swig_intgo(_swig_i_0)))
return swig_r
}
func PrintHelpAndExit(arg1 string, arg2 uint) {
_swig_i_0 := arg1
_swig_i_1 := arg2
C._wrap_printHelpAndExit_basic_host_5ddda8bb50543998(*(*C.swig_type_1)(unsafe.Pointer(&_swig_i_0)), C.swig_intgo(_swig_i_1))
if Swig_escape_always_false {
Swig_escape_val = arg1
}
}
這是 c++ 包裝文件(basic_host_wrap.cxx):
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 4.0.2
*
* This file is not intended to be easily readable and contains a number of
* coding conventions designed to improve portability and efficiency. Do not make
* changes to this file unless you know what you are doing--modify the SWIG
* interface file instead.
* ----------------------------------------------------------------------------- */
// source: .\basic_host.i
#define SWIGMODULE basic_host
#ifdef __cplusplus
/* SwigValueWrapper is described in swig.swg */
template<typename T> class SwigValueWrapper {
struct SwigMovePointer {
T *ptr;
SwigMovePointer(T *p) : ptr(p) { }
~SwigMovePointer() { delete ptr; }
SwigMovePointer& operator=(SwigMovePointer& rhs) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = rhs.ptr; rhs.ptr = 0; return *this; }
} pointer;
SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
SwigValueWrapper(const SwigValueWrapper<T>& rhs);
public:
SwigValueWrapper() : pointer(0) { }
SwigValueWrapper& operator=(const T& t) { SwigMovePointer tmp(new T(t)); pointer = tmp; return *this; }
operator T&() const { return *pointer.ptr; }
T *operator&() { return pointer.ptr; }
};
template <typename T> T SwigValueInit() {
return T();
}
#endif
/* -----------------------------------------------------------------------------
* This section contains generic SWIG labels for method/variable
* declarations/attributes, and other compiler dependent labels.
* ----------------------------------------------------------------------------- */
/* template workaround for compilers that cannot correctly implement the C++ standard */
#ifndef SWIGTEMPLATEDISAMBIGUATOR
# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560)
# define SWIGTEMPLATEDISAMBIGUATOR template
# elif defined(__HP_aCC)
/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */
/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */
# define SWIGTEMPLATEDISAMBIGUATOR template
# else
# define SWIGTEMPLATEDISAMBIGUATOR
# endif
#endif
/* inline attribute */
#ifndef SWIGINLINE
# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
# define SWIGINLINE inline
# else
# define SWIGINLINE
# endif
#endif
/* attribute recognised by some compilers to avoid 'unused' warnings */
#ifndef SWIGUNUSED
# if defined(__GNUC__)
# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
# define SWIGUNUSED __attribute__ ((__unused__))
# else
# define SWIGUNUSED
# endif
# elif defined(__ICC)
# define SWIGUNUSED __attribute__ ((__unused__))
# else
# define SWIGUNUSED
# endif
#endif
#ifndef SWIG_MSC_UNSUPPRESS_4505
# if defined(_MSC_VER)
# pragma warning(disable : 4505) /* unreferenced local function has been removed */
# endif
#endif
#ifndef SWIGUNUSEDPARM
# ifdef __cplusplus
# define SWIGUNUSEDPARM(p)
# else
# define SWIGUNUSEDPARM(p) p SWIGUNUSED
# endif
#endif
/* internal SWIG method */
#ifndef SWIGINTERN
# define SWIGINTERN static SWIGUNUSED
#endif
/* internal inline SWIG method */
#ifndef SWIGINTERNINLINE
# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE
#endif
/* exporting methods */
#if defined(__GNUC__)
# if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
# ifndef GCC_HASCLASSVISIBILITY
# define GCC_HASCLASSVISIBILITY
# endif
# endif
#endif
#ifndef SWIGEXPORT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# if defined(STATIC_LINKED)
# define SWIGEXPORT
# else
# define SWIGEXPORT __declspec(dllexport)
# endif
# else
# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
# define SWIGEXPORT __attribute__ ((visibility("default")))
# else
# define SWIGEXPORT
# endif
# endif
#endif
/* calling conventions for Windows */
#ifndef SWIGSTDCALL
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# define SWIGSTDCALL __stdcall
# else
# define SWIGSTDCALL
# endif
#endif
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
# define _CRT_SECURE_NO_DEPRECATE
#endif
/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
# define _SCL_SECURE_NO_DEPRECATE
#endif
/* Deal with Apple's deprecated 'AssertMacros.h' from Carbon-framework */
#if defined(__APPLE__) && !defined(__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES)
# define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
#endif
/* Intel's compiler complains if a variable which was never initialised is
* cast to void, which is a common idiom which we use to indicate that we
* are aware a variable isn't used. So we just silence that warning.
* See: https://github.com/swig/swig/issues/192 for more discussion.
*/
#ifdef __INTEL_COMPILER
# pragma warning disable 592
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
typedef long long intgo;
typedef unsigned long long uintgo;
# if !defined(__clang__) && (defined(__i386__) || defined(__x86_64__))
# define SWIGSTRUCTPACKED __attribute__((__packed__, __gcc_struct__))
# else
# define SWIGSTRUCTPACKED __attribute__((__packed__))
# endif
typedef struct { char *p; intgo n; } _gostring_;
typedef struct { void* array; intgo len; intgo cap; } _goslice_;
#define swiggo_size_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];
#define swiggo_size_assert(t, n) swiggo_size_assert_eq(sizeof(t), n, swiggo_sizeof_##t##_is_not_##n)
swiggo_size_assert(char, 1)
swiggo_size_assert(short, 2)
swiggo_size_assert(int, 4)
typedef long long swiggo_long_long;
swiggo_size_assert(swiggo_long_long, 8)
swiggo_size_assert(float, 4)
swiggo_size_assert(double, 8)
#ifdef __cplusplus
extern "C" {
#endif
extern void crosscall2(void (*fn)(void *, int), void *, int);
extern char* _cgo_topofstack(void) __attribute__ ((weak));
extern void _cgo_allocate(void *, int);
extern void _cgo_panic(void *, int);
#ifdef __cplusplus
}
#endif
static char *_swig_topofstack() {
if (_cgo_topofstack) {
return _cgo_topofstack();
} else {
return 0;
}
}
static void _swig_gopanic(const char *p) {
struct {
const char *p;
} SWIGSTRUCTPACKED a;
a.p = p;
crosscall2(_cgo_panic, &a, (int) sizeof a);
}
#define SWIG_contract_assert(expr, msg) \
if (!(expr)) { _swig_gopanic(msg); } else
static void Swig_free(void* p) {
free(p);
}
static void* Swig_malloc(int c) {
return malloc(c);
}
#include <atomic>
#include <map>
#include <iostream>
#include <memory>
#include <myLib/myLib.hpp>
#include "../../examples/basic_host/command_line.h"
#include "../../examples/basic_host/command_line.cpp"
extern void printHelpAndExit(const char* binary, unsigned int exitCode);
#ifdef __cplusplus
extern "C" {
#endif
void _wrap_Swig_free_basic_host_5ddda8bb50543998(void *_swig_go_0) {
void *arg1 = (void *) 0 ;
arg1 = *(void **)&_swig_go_0;
Swig_free(arg1);
}
void *_wrap_Swig_malloc_basic_host_5ddda8bb50543998(intgo _swig_go_0) {
int arg1 ;
void *result = 0 ;
void *_swig_go_result;
arg1 = (int)_swig_go_0;
result = (void *)Swig_malloc(arg1);
*(void **)&_swig_go_result = (void *)result;
return _swig_go_result;
}
void _wrap_printHelpAndExit_basic_host_5ddda8bb50543998(_gostring_ _swig_go_0, intgo _swig_go_1) {
char *arg1 = (char *) 0 ;
unsigned int arg2 ;
arg1 = (char *)malloc(_swig_go_0.n + 1);
memcpy(arg1, _swig_go_0.p, _swig_go_0.n);
arg1[_swig_go_0.n] = '\0';
arg2 = (unsigned int)_swig_go_1;
printHelpAndExit((char const *)arg1,arg2);
free(arg1);
}
#ifdef __cplusplus
}
#endif
CPP PrintHelpAndExit 中的 function:
void printHelpAndExit(const char* binary, unsigned int exitCode)
{
std::cout << "Usage: " << binary
<< " -t test " << std::endl;
std::cout << std::endl;
std::cout << " -h Prints this help and exit" << std::endl;
std::cout << " -t test test desc "
"desc."
<< std::endl;
exit(exitCode);
}
如果您對構建問題有任何想法或調試可能導致此問題的原因。 我會很高興聽到它。
更新#1:
我發現如果我刪除 lib <iostream>
,我可以毫無問題地訪問我的"../../examples/basic_host/command_line.h"
變量。 我仍然不知道為什么這個庫特別會引起問題,但我正在調查它。 順便說一句,我不能只刪除它,因為我需要它在我的 c++ 庫中。
我發現另一個人用 python 構建 c++ 並遇到與我使用 iostream 相同的問題,但沒有解釋如何做到這一點:
更新#2:
似乎它來自我的編譯器,我目前正在使用TDM GCC並在 windows 下出錯。 我嘗試使用 WSL2 Ubuntu 進行編譯,它可以工作。 但是我仍然需要在 Windows 下構建,因為我在我的庫中使用了winsock。
我遇到的問題是我在 Windows 上使用的編譯器。 我曾經使用TDM-GCC進行編譯,但正如這篇文章所暗示的那樣,這與iostream
lib 之間存在已知問題......
我剛剛將我的編譯器(gcc,g++,..)更改為使用 SWIG 構建到 MinGW64:下載鏈接。 之后一切正常,我可以毫無問題地使用我的庫。 感謝那些試圖幫助我的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.