我正在尝试为mosquitto pub命令设置一个函数。此命令保存在主.c文件的单独.c文件中。一切都编译得很好但是当我从外部.c文件中调用函数(在外部.c文件中调用)时没有任何反应。相反,如果我在没有外部.c文件的情况下在其自己的编译程序中调用该函数,则该函数将按预期运行。意识到我做错了什么,我尝试通过一个简单的printf函数做一些简单的事情并在外部.c文件中调用它,它编译得很好但仍然不打印我的数据。我究竟做错了什么?我也在.h中定义了函数。
void mosquittoto(void)
{
struct mosquitto *mosq = NULL;
// Initialize the Mosquitto library
mosquitto_lib_init();
// Create a new Mosquitto runtime instance with a random client ID,
// and no application-specific callback data.
mosq = mosquitto_new (NULL, true, NULL);
if (!mosq)
{
fprintf (stderr, "Can't initialize Mosquitto library\n");
exit (-1);
}
mosquitto_username_pw_set (mosq, MQTT_USERNAME, MQTT_PASSWORD);
// Establish a connection to the MQTT server. Do not use a keep-alive ping
int ret = mosquitto_connect (mosq, MQTT_HOSTNAME, MQTT_PORT, 0);
if (ret)
{
fprintf (stderr, "Can't connect to Mosquitto server\n");
exit (-1);
}
int i;
char text[20];
for (i = 0; i < 10; i++)
{
//sprintf (text, "Hello, World %d", i);
// Publish the message to the topic
ret = mosquitto_publish (mosq, NULL, MQTT_TOPIC,
strlen (epc4pub), epc4pub, 0, false);
if (ret)
{
fprintf (stderr, "Can't publish to Mosquitto server\n");
exit (-1);
}
}
// We need a short delay here, to prevent the Mosquitto library being
// torn down by the operating system before all the network operations
// are finished.
sleep (1);
// Tidy up
mosquitto_disconnect (mosq);
mosquitto_destroy (mosq);
mosquitto_lib_cleanup();
}
我在.h文件中定义的函数为:
void mosquittoto(void);
谢谢大家的帮助!