2015-04-03 65 views
0

在Go中包含OpenJtalk,文件已成功包含,並且引用的類型沒有問題,但功能會觸發undefined reference錯誤。包含文件中的CGO未定義參考

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() { 

} 

而且奇怪的是,這些相同功能的類型後立即宣佈:

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/

回答

6

您需要添加cgo鏈接器選項(LDFLAGS)以及你的圖書館的路徑和名稱。例如

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

Alternaively(如果有的話):'// #cgo pkg配置:onelib anotherlib' – jimt 2015-04-03 20:49:23

+0

這適用於所有減去'-L /家庭/流浪者/ open_jtalk /仲裁處/ src目錄-llibmecab':**/usr/bin/ld:找不到-llibmecab **。 而且我在該文件夾和'/ usr/local/lib /'中都有** libmecab **,包括靜態和動態。 – localhost 2015-04-03 21:13:12

+0

-lmecab(刪除lib)? – 2015-04-03 21:19:11