1

我已经使用本教程安装mosquitto lib在我的 Rpi 中。 已经和 mosquitto 服务器守护进程在./usr/local/binmosquitto_pub and mosquitto_sub/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 中的 mosquitto lib 链接为

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 文件可能有什么问题?

4

2 回答 2

2

使用 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})

将为您设置所有编译选项、链接选项、链接目标等。

于 2021-03-12T09:46:12.220 回答
0

mosquitto 库称为 mosquitto 而不是 libmosquitto。

JimsFridge: JimsFridge.cpp StopWatch.cpp
    $(CXX) $^ -o $@ -lwiringPi -lstdc++ -lmosquitto
于 2017-10-08T03:50:55.477 回答