1

I have installed mosquitto lib in my Rpi using this tutorial. /usr/local/bin has mosquitto_pub and mosquitto_sub and the mosquitto server deamon is in /usr/local/sbin.

Then trying to link the library in my cmake file as follow.

cmake_minimum_required(VERSION 2.6)
 
PROJECT(MosquittoTest)
# The version number.
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)

include_directories("${PROJECT_BINARY_DIR}")

# Linked libariries
#For MQTT
#location of raspicam's cmake file is /usr/src/raspicam-0.1.3/build
link_directories(/usr/local/sbin)
target_link_libraries (MosquittoTest  mosquitto)

ADD_EXECUTABLE(MosquittoTest MosquittoTest.cpp)

# add the install targets
install (TARGETS MosquittoTest DESTINATION bin)
install (FILES MosquittoInterface.h DESTINATION include)

Then I have error as Can't specify link library for target MosquittoTest.

Somebody has link the mosquitto lib in gcc make as

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

What could be wrong with my cmake file?

Chnossos
  • 9,971
  • 4
  • 28
  • 40
batuman
  • 7,066
  • 26
  • 107
  • 229
  • 1
    Now it is solved. I need to install libmosquittoop-dev into my system. – batuman Nov 18 '16 at 04:53
  • an alternative solution is to use conan to pull in mosquitto rather than require a debian package install i.e. mosquitto/1.6.12 make sure you put mosquitto:shared=True in the [options] section of the conanfile.txt so you'll have a library to link against. Then just put mosquitto in target_link_libraries and be done with it. – Timothy John Laird Feb 12 '21 at 04:25

2 Answers2

1

Using Modern CMake, you can use pkg-config imported targets like so:

cmake_minimum_required(VERSION 3.0)
project(MosquittoTest VERSION 0.1)

find_package(PkgConfig REQUIRED)
pkg_check_modules(Mosquitto IMPORTED_TARGET libmosquitto REQUIRED)

add_executable(${PROJECT_NAME} MosquittoTest.cpp MosquittoInterface.h)
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
target_link_library(${PROJECT_NAME} PkgConfig::Mosquitto)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME})
install(FILES MosquittoInterface.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

All compile options, link options, link targets, etc... will be set for you.

Chnossos
  • 9,971
  • 4
  • 28
  • 40
  • does it depend on specific libmosquitto-dev version? On ubuntu 18, it seems that the default libmosquitto-dev does not provide pkg-config support –  May 04 '21 at 07:00
  • @FelixXu Yes it does depend. You could also use [Conan](https://conan.io/) to greatly facilitate your integration. – Chnossos Oct 21 '21 at 08:38
0

The mosquitto library is called mosquitto not libmosquitto.

JimsFridge: JimsFridge.cpp StopWatch.cpp
    $(CXX) $^ -o $@ -lwiringPi -lstdc++ -lmosquitto
Chnossos
  • 9,971
  • 4
  • 28
  • 40
Jim Wright
  • 11
  • 3