1

我有几个文件需要编译。这是命令。sample_client.c 依赖于 lsp.o。现在我更改了 lsp.c 和 lsp.h。如何编译以使此更改对 lsp.o 有效?

main函数在sample_client.c中,lsp.c没有main函数。

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

这是我的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)

但是,lprotobuf -c 不能正确链接。

运行 make -f Makefile 我可以得到以下信息,

lspmessage.pb-c.o: In function `lspmessage__get_packed_size':
...: undefined reference to `protobuf_c_message_get_packed_size'
lspmessage.pb-c.o: In function `lspmessage__pack':
...: undefined reference to `protobuf_c_message_pack'

我知道我可以运行这个命令,

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

但是如果我改变 lsp.c 和 lsp.h 怎么办?

4

1 回答 1

0

在我看来你LDFLAGS的不正确。尝试以下操作:

LDFLAGS +=  -L/usr/lib -lprotobuf-c

看起来您的目录-L和库出现故障。

另外,我为你删除了额外的电话-g

于 2013-02-06T22:00:16.140 回答