gpt4 book ai didi

c - 在 OSX 上将 Mosquitto 与 gcc 链接起来

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:29 24 4
gpt4 key购买 nike

我是 Mosquitto 的新手,我想写一个简单的 C 客户端连接到 Mosquitto 的测试服务器:http://test.mosquitto.org/

这是简单的 C 客户端的代码,它是 Mosquitto 站点上找到的示例的 99.9%:http://pastie.org/private/orwicqjfjz8g8biurznca

编辑 1:

我按照评论写了一个makefile而不是做

gcc -o test test.c

生成文件如下所示:

CC = gcc
CFLAGS = -I
DEPS = mosquitto.h

LIBS = -llibmosquitto

%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

make: test.c
$(CC) -m32 -Wall -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

这是我得到的输出,它似乎是将 mosquitto 库与 gcc 链接起来的一些问题:

Undefined symbols for architecture i386:
"_mosquitto_connect", referenced from:
_main in cc6Blyda.o
"_mosquitto_connect_callback_set", referenced from:
_main in cc6Blyda.o
"_mosquitto_destroy", referenced from:
_main in cc6Blyda.o
"_mosquitto_lib_cleanup", referenced from:
_main in cc6Blyda.o
"_mosquitto_lib_init", referenced from:
_main in cc6Blyda.o
"_mosquitto_log_callback_set", referenced from:
_main in cc6Blyda.o
"_mosquitto_loop", referenced from:
_main in cc6Blyda.o
"_mosquitto_message_callback_set", referenced from:
_main in cc6Blyda.o
"_mosquitto_new", referenced from:
_main in cc6Blyda.o
"_mosquitto_subscribe", referenced from:
_my_connect_callback in cc6Blyda.o
"_mosquitto_subscribe_callback_set", referenced from:
_main in cc6Blyda.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
make: *** [make] Error 1

注意:我使用 homebrew 安装 mosquitto,所以 lib 的路径是

/usr/local/Cellar/mosquitto/1.1/

感谢任何帮助!!

问候

最佳答案

我通过我的 makefile 中的一些尝试错误解决了链接问题。

这是最终的 makefile 的样子,没有任何链接问题:

CC = gcc

LIBS = -lmosquitto

%.o: %.c
$(CC) -c -o $@ $<

make: test.c
$(CC) -Wall -o test $^ $(LIBS)

.PHONY: clean

谢谢

关于c - 在 OSX 上将 Mosquitto 与 gcc 链接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16035935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com