连接protobuf库的问题

时间:2013-02-03 03:46:13

标签: protocol-buffers

我不熟悉makefile,这是我的makefile

CC = gcc

TARGET = sample_client sample_server

CFLAGS += -g -I/usr/include
LDFLAGS += -g -lprotobuf-c -L/usr/lib

all:    $(TARGET)

$(TARGET):  lsp.o lspmessage.pb-c.o

%.o:    %.c
    $(CC) -c $(CFLAGS) $< -o $@

clean:
    rm -f *.o 
    rm -f $(TARGET)

当我运行make -f Make时,我收到了这些错误。

**@**:./$ make -f Makefile
gcc -g -I/usr/include  -g -lprotobuf-c -L/usr/lib  sample_client.c lsp.o lspmessage.pb-c.o   -o sample_client
lspmessage.pb-c.o: In function `lspmessage__get_packed_size':
./lspmessage.pb-c.c:19: undefined reference to `protobuf_c_message_get_packed_size'
lspmessage.pb-c.o: In function `lspmessage__pack':
./lspmessage.pb-c.c:26: undefined reference to `protobuf_c_message_pack'
lspmessage.pb-c.o: In function `lspmessage__pack_to_buffer':
./lspmessage.pb-c.c:33: undefined reference to `protobuf_c_message_pack_to_buffer'
lspmessage.pb-c.o: In function `lspmessage__unpack':
/home/**/Downloads/CSCE662/HW/skeleton-code/lspmessage.pb-c.c:41: undefined reference to `protobuf_c_message_unpack'
lspmessage.pb-c.o: In function `lspmessage__free_unpacked':
./lspmessage.pb-c.c:50: undefined reference to `protobuf_c_message_free_unpacked'
collect2: error: ld returned 1 exit status
make: *** [sample_client] Error 1

我不知道原因。谢谢!

1 个答案:

答案 0 :(得分:4)

C链接器的工作原理是只搜索文件后面的库。

尝试重新排序这样的行:

gcc -g -I/usr/include -g sample_client.c lsp.o lspmessage.pb-c.o -o sample_client -L/usr/lib -lprotobuf-c

您可以先从命令行运行该命令,以查看问题是否已修复。之后,要修复您的Makefile,请参阅此问题How to use LDFLAGS in makefile