【问题标题】:How to Link to mosquitto lib in cmake?如何在 cmake 中链接到 mosquitto lib?
【发布时间】:2016-11-18 03:38:07
【问题描述】:

我已经使用这个tutorial 在我的 Rpi 中安装了mosquitto lib/usr/local/bin 具有 mosquitto_pub and mosquitto_sub 并且 mosquitto 服务器守护进程位于 /usr/local/sbin

然后尝试如下链接我的 cmake 文件中的库。

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)

然后我有错误,因为无法为目标 MosquittoTest 指定链接库。

有人在 gcc make as 中链接了 mosquitto 库

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

我的 cmake 文件有什么问题?

【问题讨论】:

  • 现在解决了。我需要将 libmosquittoop-dev 安装到我的系统中。
  • 另一种解决方案是使用 conan 引入 mosquitto,而不是要求安装 debian 软件包,即 mosquitto/1.6.12 确保将 mosquitto:shared=True 放入 conanfile 的 [options] 部分.txt 所以你会有一个库来链接。然后只需将 mosquitto 放入 target_link_libraries 即可。

标签: c++ cmake mosquitto libmosquitto


【解决方案1】:

使用 Modern CMake,您可以像这样使用 pkg-config 导入的目标:

cmake_minimum_required(VERSION 3.0)
project(MosquittoTest)

set(VERSION_MAJOR 0)
set(VERSION_MINOR 1)

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

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

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

所有的编译选项、链接选项、链接目标等都会为你设置好。

【讨论】:

  • 它是否依赖于特定的 libmosquitto-dev 版本?在 ubuntu 18 上,似乎默认的 libmosquitto-dev 不提供 pkg-config 支持
  • @FelixXu 是的,它确实取决于。您也可以使用Conan 来极大地促进您的集成。
【解决方案2】:

mosquitto 库称为 mosquitto 而不是 libmosquitto。

JimsFridge: JimsFridge.cpp StopWatch.cpp
    $(CXX) $^ -o $@ -lwiringPi -lstdc++ -lmosquitto

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多