解决“退出状态 3221225477”的 swig/cgo 调试问题
来源:stackoverflow
时间:2024-03-17 22:03:31 490浏览 收藏
在使用 SWIG 将 C++ 代码与 Go 集成的过程中,我遇到了“退出状态 3221225477”的调试问题。经过调查,我发现该问题与在 C++ 库中包含“iostream”库有关。删除该库后,我能够访问“command_line.h”中的变量,但由于库中需要它,我无法直接将其删除。通过将编译器从 TDM-GCC 更改为 MinGW64,我解决了这个问题,并能够成功构建和使用我的库。
我使用 swig 与 golang 的集成来生成 cgo 文件。我正在链接 c++ 库和 go。
我已经安装了我的软件包: 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 状态 3221225477 并且我的打印问候甚至没有被考虑在内,看来我的程序甚至没有启动...我不知道如何处理这个问题调试工具甚至没有启动。
退出状态似乎与 nil 指针 ref 相关:请参阅此处,这肯定是由于 cgo 集成所致。 我在 windows 10 上使用 mingw 进行构建,在我的配置下:
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 中的函数:
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 ,我就可以毫无问题地访问 "../../examples/basic_host/command_line.h" 的变量。我仍然不知道为什么这个库特别引起问题,但我正在调查它。顺便说一句,我不能直接删除它,因为我的 c++ 库中需要它。
我发现另一个人用 python 构建 c++ 并与我使用 iostream 遇到相同的问题,但没有解释如何做到这一点:
更新#2:
似乎它来自我的编译器,我目前正在使用 tdm gcc,并且在 windows 下出现错误。我尝试使用 wsl2 ubuntu 进行编译并且它可以工作。不过,我仍然需要在 windows 下构建,因为我在库中使用 winsock。
解决方案
我遇到的问题是我在 Windows 上使用的编译器。我曾经用 TDM-GCC 进行编译,但它和 iostreamlib 之间存在已知问题,如 this post suggests...
我刚刚将使用 SWIG 构建的编译器(gcc、g++、..)更改为 MinGW64:download link。之后一切正常,我可以毫无问题地使用我的库。 感谢那些试图帮助我的人。
今天关于《解决“退出状态 3221225477”的 swig/cgo 调试问题》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习