在包含的文件中,CGO出现了未定义的引用。

huangapple go评论85阅读模式
英文:

CGO undefined reference in included files

问题

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/

英文:

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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdarg.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;

// Main headers 
#include &quot;mecab.h&quot;
#include &quot;njd.h&quot;
#include &quot;jpcommon.h&quot;
#include &quot;HTS_engine.h&quot;

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

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(&amp;open_jtalk.mecab)             // when any function is called the error happens
   C.NJD_initialize(&amp;open_jtalk.njd)
   C.JPCommon_initialize(&amp;open_jtalk.jpcommon)
   C.HTS_Engine_initialize(&amp;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);

Project webpage: http://open-jtalk.sourceforge.net/

答案1

得分: 14

你需要添加cgo链接器选项(LDFLAGS),包括库的路径和名称。例如:

// #cgo CFLAGS: -I你的包含路径
// #cgo LDFLAGS: -L你的库路径 -lyour-library-name-minus-the-lib-part
英文:

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

huangapple
  • 本文由 发表于 2015年4月4日 03:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/29438761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定