C 对mosquitto_lib_init的未定义引用

C 对mosquitto_lib_init的未定义引用,c,mqtt,mosquitto,C,Mqtt,Mosquitto,问题 对MOSQUITO_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):未定义对mosquitto_new' Network.c:(.text+0x1cf):对mosquitto\u connect'的未定义引用 Network.c

问题

  • 对MOSQUITO_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):未定义对
    mosquitto_new' Network.c:(.text+0x1cf):对
    mosquitto\u connect'的未定义引用
    Network.c:(.text+0x1d8):对
    mosquitto_lib_init'的未定义引用 makefile:30:目标“Protheus”的配方失败 Network.c:(.text+0x1f2):对
    mosquitto\u subscribe'的未定义引用
    Network.c:(.text+0x203):对
    mosquito\u message\u callback\u set'的未定义引用 Network.c:(.text+0x215):对
    mosquitto\u loop\u forever'的未定义引用
    Network.c:(.text+0x21d):对
    mosquitto\u destroy'的未定义引用 Network.c:(.text+0x222):对“mosquitto_lib_cleanup”的未定义引用 collect2:错误:ld返回了1个退出状态 make:**[Protheus]错误1

  • 我尝试使用mosquito.h运行代码,但无法编译该源代码

  • 当我试着运行代码时,我的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_连接(){

    void my_message_回调(struct mosquitto*mosq、void*obj、const struct mosquitto_message*message){

    printf(“获取消息:%s\n”,(char*)消息->负载)

}


有人能帮我吗?

未定义的引用是一个链接错误。您没有链接到包含该代码的库。
   if (ip_address()){


        return 0;
    }

return 1;}
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");
}