VisualStudio2022 调试 Linux版本 mosquitto

该文指导读者如何下载并安装mosquitto、cJSON和openssl库,然后在Linux系统中编译C++项目。在VisualStudio中创建跨平台项目后,遇到找不到libmosquitto.so.1共享库的问题,通过编辑ld配置文件和更新缓存来解决链接错误。
摘要由CSDN通过智能技术生成

下载:mosquitto-2.0.9.tar.gz,cJSON-master.zip,openssl-1.1.0l.tar.gz

解压 tar -xvf  mosquitto-2.0.9.tar.gz

cd  mosquitto-2.0.9

make && make install 

tar -xvf openssl-1.1.0l.tar.gz

Openssl 安装,( --prefix )参数为欲安装之目录

./config --prefix=/usr/local/openssl

执行命令

 ./config -t

make && make install

cJSON-master.zip

make && make install 

按照教程创建工程

在 Visual Studio 中创建 C++ 跨平台项目 | Microsoft Learn

 


	/* Create a new client instance.
	 * id = NULL -> ask the broker to generate a client id for us
	 * clean session = true -> the broker should remove old sessions when we connect
	 * obj = NULL -> we aren't passing any of our private data for callbacks
	 */
	mosq = mosquitto_new(NULL, true, NULL);
	if (mosq == NULL) {
		fprintf(stderr, "Error: Out of memory.\n");
		return 1;
	}

	/* Configure callbacks. This should be done before connecting ideally. */
	mosquitto_connect_callback_set(mosq, on_connect);
	mosquitto_publish_callback_set(mosq, on_publish);

	/* Connect to test.mosquitto.org on port 1883, with a keepalive of 60 seconds.
	 * This call makes the socket connection only, it does not complete the MQTT
	 * CONNECT/CONNACK flow, you should use mosquitto_loop_start() or
	 * mosquitto_loop_forever() for processing net traffic. */
	rc = mosquitto_connect(mosq, "test.mosquitto.org", 1883, 60);
	if (rc != MOSQ_ERR_SUCCESS) {
		mosquitto_destroy(mosq);
		fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
		return 1;
	}

	/* Run the network loop in a background thread, this call returns quickly. */
	rc = mosquitto_loop_start(mosq);
	if (rc != MOSQ_ERR_SUCCESS) {
		mosquitto_destroy(mosq);
		fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
		return 1;
	}

	/* At this point the client is connected to the network socket, but may not
	 * have completed CONNECT/CONNACK.
	 * It is fairly safe to start queuing messages at this point, but if you
	 * want to be really sure you should wait until after a successful call to
	 * the connect callback.
	 * In this case we know it is 1 second before we start publishing.
	 */
	while (1) {
		publish_sensor_data(mosq);
	}

	mosquitto_lib_cleanup();
	return 0;
}

将头文件放在本地  mosquitto.h 和main.cpp 在同一个目录

编译时候发现找不到链接库处理方式

出现问题解决

: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

解决方法

/etc/ld.so.conf.d/路径下创建任意一个.conf文件,把lib文件的路径写在里面,比如

sudo gedit /etc/ld.so.conf.d/mosqutto.conf
# 写入  存放库的目录
/usr/local/lib
# 保存退出
 
# 更新缓存
sudo ldconfig

参考连接 (206条消息) error while loading shared libraries: libxxx.so通用解决方法_czafrost的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值