包含文件中的CGO未定义参考 [英] CGO undefined reference in included files

查看:51
本文介绍了包含文件中的CGO未定义参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Go中结束OpenJtalk,成功地包含了文件,并且引用类型没有问题,但是函数触发了 undefined reference 错误.

Wraping up OpenJtalk in Go, files are successfully included and types are referenced without an issue, but functions trigger an undefined reference error.

jtalk.go:

package main

// #cgo CFLAGS: -I/home/vagrant/open_jtalk/njd [...etc]
/*
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>

// Main headers 
#include "mecab.h"
#include "njd.h"
#include "jpcommon.h"
#include "HTS_engine.h"

// Sub headers 
#include "text2mecab.h"
#include "mecab2njd.h"
#include "njd_set_pronunciation.h"
#include "njd_set_digit.h"
#include "njd_set_accent_phrase.h"
#include "njd_set_accent_type.h"
#include "njd_set_unvoiced_vowel.h"
#include "njd_set_long_vowel.h"
#include "njd2jpcommon.h"
*/
import "C"

type Open_JTalk struct {
   mecab C.Mecab           each of these struct references are fine
   njd C.NJD 
   jpcommon C.JPCommon 
   engine C.HTS_Engine 
}

func (open_jtalk *Open_JTalk) Open_JTalk_initialize() {
   C.Mecab_initialize(&open_jtalk.mecab)             // when any function is called the error happens
   C.NJD_initialize(&open_jtalk.njd)
   C.JPCommon_initialize(&open_jtalk.jpcommon)
   C.HTS_Engine_initialize(&open_jtalk.engine)
}

func main() {

}

奇怪的是,这些函数在类型之后立即声明:

And the weird part is that those same functions are declared right after the types:

mecab.h

// line 1584
typedef struct _Mecab{
   char **feature;
   int size;
   mecab_t *mecab;
} Mecab;

BOOL Mecab_initialize(Mecab *m);

项目网页: http://open-jtalk.sourceforge.net/

推荐答案

您需要添加cgo链接器选项( LDFLAGS )以及库的路径和名称.例如

You need to add cgo linker options (LDFLAGS) with the path to and the name of your library. e.g.

// #cgo CFLAGS: -Iyour-include-path
// #cgo LDFLAGS: -Lyour-library-path -lyour-library-name-minus-the-lib-part

这篇关于包含文件中的CGO未定义参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆