1

I am new to Mosquitto and I am tying to write a simple C client connecting to Mosquitto's test server: http://test.mosquitto.org/

Here is the code of the simple C client which is 99.9% of an example found on Mosquitto's site: http://pastie.org/private/orwicqjfjz8g8biurznca

EDIT 1:

I followed the comments and wrote a makefile instead of doing

gcc -o test test.c

The makefile looks like this:

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

This is the output i get which seems to be some problem linking mosquitto libs with 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

NOTE: I used homebrew to install the mosquitto so the path to the lib is

/usr/local/Cellar/mosquitto/1.1/

Appreciate any help!!

Regards

1 Answers1

1

I solved the linking problem by some trial error in my makefile.

This is how the final makefile looks like which does not give any linking problems:

CC = gcc

LIBS = -lmosquitto

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

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

.PHONY: clean

Thanks

  • 2
    The `mosquitto` library was compiled in 64 bit mode, while in your test compilation you were explicitly compiling in 32bit mode (the `-m32` flag). This is a very common problem when building in mixed 32/64 bit environments and one of the first things you should do is determine if the compiled code matches the library code using the `file` command, which will tell you whether code is x86_64, i386 or both (which is supported in OS X) – Anya Shenanigans Apr 17 '13 at 10:00
  • Yes, the `-m32` flag was one of the things when removed that made it work. That together with correct linking of the libraries was the key. Cheers – Mathias Fernström Apr 17 '13 at 11:45
  • 1
    Glad to see you got it working, I maintain the mosquitto homebrew package so I'm glad it wasn't a mistake I made in that! Assuming your code #includes mosquitto.h you should not need it listed as DEPS in the makefile either. You've also now commented out the CFLAGS line which means you can remove it and also remove the $(CFLAGS) references. – Andy Piper Apr 17 '13 at 14:14