问题
对mosquitto_lib_init及其他一些C函数的未定义引用,产生此错误:
gcc -o“Protheus”./src/Brain/Brain.o ./src/Brain/Network.o
./src/Brain/Network.o:在函数mqtt_init_server':
Network.c:(.text+0x1b8): undefined reference to
mosquitto_new'
Network.c :(。text + 0x1cf):对mosquitto_connect'
Network.c:(.text+0x1d8): undefined reference to
mosquitto_lib_init'的未定义引用
makefile:30:目标'Protheus'的配方失败了
Network.c :(。text + 0x1f2):对mosquitto_subscribe'
Network.c:(.text+0x203): undefined reference to
mosquitto_message_callback_set'的未定义引用
Network.c :(。text + 0x215):对mosquitto_loop_forever'
Network.c:(.text+0x21d): undefined reference to
mosquitto_destroy'的未定义引用
Network.c :(。text + 0x222):未定义的引用`mosquitto_lib_cleanup'
collect2:错误:ld返回1退出状态
make:*** [Protheus]错误1
我尝试使用mosquitto.h运行代码,但我无法编译该源代码。
当我尝试运行代码时,我的makefile就坏了。
[MAKEFILE]
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include src/Brain/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: Protheus
# Tool invocations
Protheus: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C Linker'
gcc -o "Protheus" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) Protheus
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets
=============================================== ===================
我的代码是:
int ip_address(){
int fd;
struct ifreq ifr;
char iface[] = "eth0";
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
IFACE = iface;
IP_ADDRESS = inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr);
if (IP_ADDRESS != NULL && strlen(IP_ADDRESS) > 7) {
return 1;
}
return 0; }
int check_connection(){
if (ip_address()){
return 0;
}
return 1;}
void my_message_callback(struct mosquitto * mosq,void * obj,const struct mosquitto_message * message){
printf(“收到消息:%s \ n”,(char *)message-> payload);
}
int mqtt_init_server(){
MOSQUITTO *mosq = NULL;
mosq = mosquitto_new(NULL,0,NULL);
int ret = mosquitto_connect(mosq, MQTT_HOSTNAME,MQTT_PORT,0);
if (0 == ret || !mosquitto_lib_init() || !mosq)
{
fprintf (stderr, "Can't initialize Mosquitto library\n");
return 0;
}
ret = mosquitto_subscribe(mosq,NULL,MQTT_TOPIC,0);
if(ret)
{
fprintf (stderr, "Can't publish to Mosquitto server\n");
return 0;
}
mosquitto_message_callback_set (mosq, my_message_callback);
mosquitto_loop_forever(mosq,-1,1);
mosquitto_destroy (mosq);
mosquitto_lib_cleanup();
return 1;
printf("CONECTED");
}
有人可以帮助我吗?