这两天想要写一个mosquitto的插件,把往来的topic消息存入数据库中。libpq的demo都正常,到编译插件运行时报错找不到符号。但是又在Makefile中插入了链接libpq.so的配置

undefined reference to PQprepare

最先写的Makefile是这样

${PLUGIN_NAME}.so : ${PLUGIN_NAME}.c
        $(CROSS_COMPILE)$(CC) $(PLUGIN_CPPFLAGS)  $(PLUGIN_CFLAGS) $(PLUGIN_LDFLAGS) -lpq -g -fPIC -shared $< -o $@ 

最后一顿操作才发现式链接开关的位置问题-l链接开关要在-o之后

${PLUGIN_NAME}.so : ${PLUGIN_NAME}.c
        $(CROSS_COMPILE)$(CC) $(PLUGIN_CPPFLAGS)  $(PLUGIN_CFLAGS) $(PLUGIN_LDFLAGS) -g -fPIC -shared $< -o $@ -lpq
cc  -I../.. -I../../include -I../../lib -I../../src -I/usr/include/postgresql -I../../deps  -Wall -ggdb -O2 -Wconversion -Wextra -fPIC -g -O0  -L/usr/lib/x86_64-linux-gnu  -g -fPIC -shared mosquitto_payload_modification.c -o mosquitto_payload_modification.so -lpq