急求问 Cgo 关于 undefined (type *TEST_ST has no field or method test1)是什么原因????

JGbooks · 2018-07-18 13:08:45 · 1894 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2018-07-18 13:08:45 的主题,其中的信息可能已经有所发展或是发生改变。

环境:

gcc 版本:gcc version 5.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
mingw :64位 5.1.1
go版本:go version go1.10.3 windows/amd64
编译环境:LiteIDE、goland均可

测试程序如下:

test.h

#ifndef _TEST_H_
#define _TEST_H_

#include <stdio.h>
#include <stdlib.h>

struct test1 {
    int id;
    char *name;
};

struct test2 {
    int age;
    int high;
};

typedef struct {
    struct test1 num1;
    struct test2 num2;
}test_st;

#endif

test.c

#include "test.h"

void init (test_st *p){
    test_st test;
    p->num1.id = 0;
    p->num1.name = "hi";
    p->num2.age = 90;
    p->num2.high = 195;
}

test.go

package main

/*
#include "test.h"
void test_printf(struct test1 *p) 
{
        printf("%d %\r\n", p->id, p->name);
}
*/
import "C"

type TEST_ST struct {
    t C.test_st
}

func NewTest_ST() *TEST_ST {
    return &TEST_ST{}
}

func (h *TEST_ST) Pri() {
    C.test_printf(h.test1) //这里会发生错误,这是怎么回事,是wingw问题吗?
}

func main() {
    h := NewTest_ST()
    h.Pri()
}

错误输出:

.\test.go:20: h.test1 undefined (type *TEST_ST has no field or method test1)

错误: 进程退出代码 2.

现阶段状况

1、windows 32位可以通过,环境同上,位数为32位
2、Windows 64位 一直如上错误输出,请问是什么原因??

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1894 次点击  
加入收藏 微博
4 回复  |  直到 2018-07-19 15:36:49
JGbooks
JGbooks · #1 · 7年之前

修改程序:C.test_printf(&h.t.num1) //这里会发生错误,这是怎么回事,是wingw问题吗?

修改程序:打印出修改 printf("%d %s\r\n", p->id, p->name);

然而测试通过,但是还是遇到

.\engine.go:395: h.m.linfo undefined (type _Ctype_struct___0 has no field or method linfo)

虽然程序不一样,但是设计方法一样。windows32位 linux64位均可以,就是windows 64 不行???

abin
abin · #2 · 7年之前

修改了下

package main

/*
#include "test.h"
void test_printf(struct test1 *p)
{
    printf("%d %s\r\n", p->id, p->name);
}
*/
import "C"

type TEST_ST struct {
    t C.test_st
}

func NewTest_ST() *TEST_ST {
    return &TEST_ST{}
}

func (h *TEST_ST) Pri() {
    C.test_printf(&h.t.num1) 
}

func main() {
    h := NewTest_ST()
    h.Pri()
}
JGbooks
JGbooks · #3 · 7年之前

关于:.\engine.go:395: h.m.linfo undefined (type _Ctype_struct___0 has no field or method linfo)问题!!!

`1、产生原因是go 不能识别 C语言结构体中及结构体成员`

     1.1  可能是由于编译器的原因,本人测试windows32 linux64 均可以,go版本都是1.10.3, 

     1.2   也可能是Mingw的原因感觉这个影响不大,因为上述程序可以测过。

2、最终只能重新修改程序,完美通过

    2.1  建议对于go程序中需要调用C结构体的结构体成员(相当于num1)时,最好在声明go结构体时,进行显示声明。不推荐使用C语言封装一个大结构体。
JGbooks
JGbooks · #4 · 7年之前

关于Cgo调用动态库发现的一些问题

Cgo 条件编译设置

#cgo  CFLAGS: -Wall
#cgo CFLAGS:-I./include
#cgo windows,386 LDFLAGS:-L./lib/win/386
#cgo windows,amd64 LDFLAGS:-L./lib/win/amd64
#cgo windows LDFLAGS: -lpthreadGC2 -lengine -lm
#cgo linux LDFLAGS:-L./lib/linux
#cgo linux LDFLAGS:-lengine -lpthread -lm

如果是将动态库编译进exe 文件就忽略如下

Cgo 调用动态库时,工程设计时,创建lib 链接动态库时可自行寻找lib中动态库,如果lib下面时2级目录,目前测试在执行程序中不能直接链接

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传