#3733·mosquitto

2.1.2: mosquittopp_static is built with empty ${C_SRC} and ${LIBRARIES} (WITH_STATIC_LIBRARIES=ON fails to compile)

Author: AndreasWinter19Created Sep 4, 2026Updated Sep 4, 2026
LabelsStatus: Available

Description

With -DWITH_STATIC_LIBRARIES=ON, building mosquittopp_static fails:

include/mosquitto/libcommon_cjson.h:31:10: fatal error: cjson/cJSON.h: No such file or directory
   31 | #include <cjson/cJSON.h>

Cause

lib/CMakeLists.txt adds the C++ subdirectory on line 3, before the variables that subdirectory consumes are defined:

cmake
1: option(WITH_LIB_CPP "Build C++ library?" ON)
2: if(WITH_LIB_CPP)
3: 	add_subdirectory(cpp)     # <-- here
4: endif()
5:
6: set(C_SRC ...)              # <-- defined afterwards
...
54: set(LIBRARIES common-options libmosquitto_common)

CMake copies the parent directory scope into the subdirectory at the point add_subdirectory() is called, so in lib/cpp/CMakeLists.txt both ${C_SRC} (line 40) and ${LIBRARIES} (line 55) expand to nothing:

cmake
add_library(mosquittopp_static STATIC
    ${C_SRC}      # empty
    ${CPP_SRC}
)
target_link_libraries(mosquittopp_static PRIVATE ${LIBRARIES})   # links nothing

Because ${LIBRARIES} is empty, mosquittopp_static never gets libmosquitto_common, and therefore never gets the cJSON include directory. In 2.1 this became a hard error because mosquitto.h now includes mosquitto/libcommon.h, which includes <cjson/cJSON.h>.

The same ordering exists in 2.0.x and on master, so mosquittopp_static has never actually contained the C sources — it was silent until 2.1 made the missing include fatal.

Note that simply moving add_subdirectory(cpp) to the end of lib/CMakeLists.txt is not sufficient: the entries in C_SRC are relative to lib/, so they do not resolve from lib/cpp/ and configuration then fails with Cannot find source file: actions_publish.c.

Reproduce

bash
git clone --branch v2.1.2 https://github.com/eclipse-mosquitto/mosquitto
cd mosquitto
cmake -B build -DWITH_STATIC_LIBRARIES=ON -DWITH_BROKER=OFF -DWITH_APPS=OFF \
      -DWITH_CLIENTS=OFF -DWITH_PLUGINS=OFF -DWITH_DOCS=OFF -DWITH_TESTS=OFF
cmake --build build

Version

2.1.2 (also present on master)

Source: eclipse-mosquitto/mosquitto