microsoft / microsoft/vcpkg

[paho-mqttpp3] Windows shared library requires manual PAHO_MQTTPP_IMPORTS define to avoid LNK2001

Open Beginner friendly
#52,261 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
CMake
Stars
27.5k
Forks
7.7k
Avg merge
2d 19h
Merged PRs (30d)
321

Description

**Describe the bug**

When using the shared library version of `paho-mqttpp3` installed via vcpkg on Windows, linking a CMake project that consumes `PahoMqttCpp::paho-mqttpp3` fails with unresolved external symbol `mqtt::message::EMPTY_STR` (LNK2001). The user must manually add `PAHO_MQTTPP_IMPORTS` to the compile definitions, which is not documented nor automatically handled by the CMake target.

**Environment**
- OS: Windows 11
- Compiler: MSVC 2022 (19.44.35227.0)
- vcpkg commit: latest
- paho-mqttpp3 version: 1.6.0

**To Reproduce**

1. Install the package:
```cmd
.\vcpkg.exe install paho-mqttpp3:x64-windows
```

2. Create a minimal CMake project that consumes the library:

**CMakeLists.txt**
```cmake
cmake_minimum_required(VERSION 3.20)
project(test_paho)
find_package(PahoMqttCpp CONFIG REQUIRED)
add_executable(test_paho main.cpp)
target_link_libraries(test_paho PRIVATE PahoMqttCpp::paho-mqttpp3)
```

**main.cpp**
```cpp
#include
int main() { return 0; }
```

3. Configure and build:
```cmd
cmake -B build -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake
cmake --build build
```

4. Observe linker error:
```
main.cpp.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<...> const mqtt::message::EMPTY_STR"
test_paho.exe : fatal error LNK1120: 1 unresolved externals
```

**Expected behavior**

The library should link without requiring any manual compile definitions. The CMake target `PahoMqttCpp::paho-mqttpp3` should automatically propagate `PAHO_MQTTPP_IMPORTS` on Windows for the shared library configuration.

**Failure logs**

```
main.cpp.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string,class std::allocator > const mqtt::message::EMPTY_STR" (?EMPTY_STR@message@mqtt@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)
test_paho.exe : fatal error LNK1120: 1 unresolved externals
```

**Additional context**

- Upstream issue discussing the problem: https://github.com/eclipse-paho/paho.mqtt.cpp/issues/552
- The root cause is that the header `export.h` uses `PAHO_MQTTPP_IMPORTS` to switch to `__declspec(dllimport)`. Without it, static members are not imported correctly.
- A workaround exists: add `target_compile_definitions(your_target PRIVATE PAHO_MQTTPP_IMPORTS)`. This should be handled automatically by vcpkg's CMake configuration.

**Suggested fix for vcpkg port**

Patch the generated `PahoMqttCppTargets.cmake` file to add `INTERFACE_COMPILE_DEFINITIONS "PAHO_MQTTPP_IMPORTS"` for the shared library target on Windows.

Here is a patch for `ports/paho-mqttpp3/portfile.cmake`:

```diff
--- a/ports/paho-mqttpp3/portfile.cmake
+++ b/ports/paho-mqttpp3/portfile.cmake
@@ -40,6 +40,15 @@ vcpkg_cmake_configure(
vcpkg_cmake_install()
vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/PahoMqttCpp)

+# For Windows shared library, add required compile definition PAHO_MQTTPP_IMPORTS
+if(VCPKG_TARGET_IS_WINDOWS AND NOT VCPKG_LIBRARY_LINKAGE STREQUAL "static")
+ set(_targets_file "${CURRENT_PACKAGES_DIR}/share/${PORT}/PahoMqttCppTargets.cmake")
+ if(EXISTS "${_targets_file}")
+ vcpkg_replace_string("${_targets_file}"
+ "INTERFACE_LINK_LIBRARIES"
+ "INTERFACE_COMPILE_DEFINITIONS \"PAHO_MQTTPP_IMPORTS\"\n INTERFACE_LINK_LIBRARIES")
+ endif()
+endif()
+
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
```

This adds the missing definition only for Windows shared builds, leaving static builds and other platforms unchanged.

Contributor guide

Open the contributing guide

Research direction

Start with ports/paho-mqttpp3/portfile.cmake and inspect the generated PahoMqttCppTargets.cmake after vcpkg_cmake_config_fixup. Build the supplied minimal CMake project on Windows with the shared package, then verify that the target propagates PAHO_MQTTPP_IMPORTS without a consumer-side definition. Confirm static builds and non-Windows configurations remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp
Domain
build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.