静态链接pocketphinx(图书馆)

时间:2014-08-04 15:11:48

标签: c++ linker makefile static-libraries cmusphinx

我正在开发一个带有pocketsphinx的小程序(语音到文本 - 库)。 在Windows上我使用Code :: Blocks作为开发环境,我成功构建了一个程序。 现在我尝试将我的程序移植到Linux,并且我几乎没有问题链接到pocketsphinx。

这是Makefile:

CC = g++
CFLAGS = -Wall -std=c++11
LDFLAGS = -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx

OBJ = obj/Application.o obj/Main.o obj/Recorder.o

all: $(OBJ)
    $(CC) -L/usr/local/lib -o bin/Eve $(OBJ) -s -lsphinxbase -lpocketsphinx

obj/Main.o: src/Main.cpp
    $(CC) $(CFLAGS) $(LDFLAGS) -c src/Main.cpp -o obj/Main.o

obj/Application.o: src/Application.cpp src/Application.hpp
    $(CC) $(CFLAGS) $(LDFLAGS) -c src/Application.cpp -o obj/Application.o

obj/Recorder.o: src/Recorder.cpp src/Recorder.hpp
    $(CC) $(CFLAGS) $(LDFLAGS) -c src/Recorder.cpp -o obj/Recorder.o

与我在Windows上使用的相同,我只是调整了文件路径。

我收到以下错误:

$ make
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Application.cpp -o obj/Application.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Main.cpp -o obj/Main.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Recorder.cpp -o obj/Recorder.o
g++ -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o -s  -lsphinxbase -lpocketsphinx
obj/Recorder.o: In function `Recorder::Recorder()':
Recorder.cpp:(.text+0x1c0): undefined reference to `ad_open_sps'
Recorder.cpp:(.text+0x20d): undefined reference to `ad_read'
Recorder.cpp:(.text+0x215): undefined reference to `cont_ad_init'
obj/Recorder.o: In function `Recorder::~Recorder()':
Recorder.cpp:(.text+0x2f3): undefined reference to `cont_ad_close'
Recorder.cpp:(.text+0x303): undefined reference to `ad_close'
obj/Recorder.o: In function `Recorder::recognizeFromMicrophone()':
Recorder.cpp:(.text+0x37a): undefined reference to `ad_start_rec'
Recorder.cpp:(.text+0x395): undefined reference to `cont_ad_calib'
Recorder.cpp:(.text+0x3f0): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x4e6): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x5b5): undefined reference to `ad_stop_rec'
Recorder.cpp:(.text+0x5d8): undefined reference to `ad_read'
Recorder.cpp:(.text+0x5f4): undefined reference to `cont_ad_reset'
collect2: error: ld returned 1 exit status
make: *** [all] Fehler 1

我不认为这是name mangling问题,因为我使用提供的Makefile自行构建了lib。

如何在没有错误的情况下链接lib?

编辑:我想出了如何让它发挥作用。我只是将目标“all”的规则修改为:

$(CC) -static -L/usr/local/lib -o bin/Eve $(OBJ) -s  -lpocketsphinx -lsphinxbase -lsphinxad -lpthread

1 个答案:

答案 0 :(得分:0)

类似ad_read的函数在libsphinxad库中定义,您需要将它添加到链接器命令行:

g++ -static -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o \
-lpocketsphinx -lsphinxbase -libsphinxad

请注意,图书馆的顺序很重要。