2013-04-16 61 views
1

GCC我是新來Mosquitto和我綁寫一個簡單的C客戶端連接到Mosquitto的測試服務器:http://test.mosquitto.org/鏈接Mosquitto與OSX

下面是簡單的C客戶端的代碼,這是一個99.9% http://pastie.org/private/orwicqjfjz8g8biurznca

編輯1:

我跟着意見,並寫了一個makefile而不是做

gcc -o test test.c 
例如在Mosquitto的網站上發現

生成文件看起來是這樣的:

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

LIBS = -llibmosquitto 

%.o: %.c $(DEPS) 
    $(CC) -c -o [email protected] $< $(CFLAGS) 

make: test.c 
    $(CC) -m32 -Wall -o [email protected] $^ $(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 

注:我用自制安裝mosquitto所以lib的路徑是

/usr/local/Cellar/mosquitto/1.1/ 

感謝任何幫助!

問候

+1

你需要告訴GCC與libmosquitto鏈接爲它是能夠找到mosquitto功能。 –

+1

它看起來像你*編譯*罰款。另一方面,鏈接...... – WhozCraig

回答

1

我通過一些試驗誤差在我的makefile解決了連接問題。

這是最後生成文件看起來像這並沒有給任何連接問題:

CC = gcc 

LIBS = -lmosquitto 

%.o: %.c 
    $(CC) -c -o [email protected] $< 

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

.PHONY: clean 

感謝

+2

'mosquitto'庫是以64位模式編譯的,而在你的測試編譯中,你是以32位模式('-m32'標誌)顯式編譯的。在混合32/64位環境中構建時,這是一個非常常見的問題,您應該做的第一件事情之一是確定編譯後的代碼是否使用'file'命令匹配庫代碼,它會告訴您代碼是否爲x86_64, i386或兩者兼有(OS X支持) – Petesh

+0

是的,'-m32'標誌是被刪除的東西之一,它使它工作。關鍵在於連同圖書館的正確聯繫。歡呼 –

+1

很高興看到你的工作,我保持蚊子自制軟件包,所以我很高興這不是我犯的錯誤!假設你的代碼#包含mosquitto.h,你不需要在makefile中將它列爲DEPS。您現在也註釋了CFLAGS行,這意味着您可以刪除它並刪除$(CFLAGS)引用。 –