eclipse-paho / paho.mqtt.cpp

Other
1.03k stars 438 forks source link

Build error on Windows 11 - Visual Studio community 2022 - Cmake 3.29.6 #500

Open FrankRobot opened 4 months ago

FrankRobot commented 4 months ago

Hi, I've followed the instructions for building paho.mqtt.c and then paco.mqtt.cpp. I can build paho.mqtt.c, but when I try to build paco.mqtt.cpp I always get this error:

cmake -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-cpp -DPAHO_BUILD_SAMPLES=ON -DPAHO_WITH_SSL=OFF -DCMAKE_PREFIX_PATH=C:\mqtt\paho-c -- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.22631. -- Configuring done (0.2s) CMake Error at src/CMakeLists.txt:82 (target_link_libraries): Target "paho-mqttpp3-static" links to:

eclipse-paho-mqtt-c::paho-mqtt3a-static

but the target was not found. Possible reasons include:

* There is a typo in the target name.
* A find_package call is missing for an IMPORTED target.
* An ALIAS target is missing.

-- Generating done (1.7s) CMake Generate step failed. Build files cannot be regenerated correctly.

fpagliughi commented 4 months ago

Is this with the new v1.4.0?

FrankRobot commented 4 months ago

Hi fpaglughi, Yes, with the v1.4.0. I've tried also the dev branch.

fpagliughi commented 4 months ago

OK, It's looking like the problem is with the build instructions in the README, combined with the new version being more strict about what it links against. Now the shared C++ library only links against the shared C library, and static against static. I may loosen this up a bit, but want to avoid some linker problems when mixing these.

Anyway, the problem is probably that the C library is being built just with the shared DLL, but not that static library, and the C++ library is being built the other way (static but not shared).

The easiest way to avoid this problem is to build both libraries together using the -DPAHO_WITH_MQTT_C=ON CMake flag when building the C++ library. This ensures that both libraries are configured in the same manner, and compatible.

The instructions are near the top of the README and generally apply to all platforms. This is the new recommended way to build the C & C++ libraries when you will primarily just be using the C++ API.

$ git clone https://github.com/eclipse/paho.mqtt.cpp
$ cd paho.mqtt.cpp
$ git co v1.4.0

$ git submodule init
$ git submodule update

$ cmake -Bbuild -H. -DPAHO_WITH_MQTT_C=ON -DPAHO_BUILD_EXAMPLES=ON
$ sudo cmake --build build/ --target install

But you can still both of them separately. On Windows you will just need to be sure to configure and build the static libraries (or turn on shared and turn off static for both libraries).

To build the static libraries for Paho C, just re-run CMake with -DPAHO_BUILD_STATIC=ON, then recompile and install.

After that, just recompile the C++ library and it should build fine.

In the meantime, assuming that works, lets keep this issue open to remind me to fix the README.

fpagliughi commented 4 months ago

Oh, no, wait...

I was sure I tested all the combinations, but I was more concerned with getting PAHO_WITH_MQTT_C working, that I let this slip.

It appears that if you enable both targets (shared and static) in the Paho C library, it is building the static libraries but not exporting the -static CMake target!

As a quick fix, rebuild the Paho C library with static turned on and shared turned off, -DPAHO_BUILD_STATIC=ON -DPAHO_BUILD_SHARED=OFF

Sorry about the confusion. I will try to figure out the problem in the C library build and send a PR upstream.

fpagliughi commented 4 months ago

OK. I sent a PR to the upstream Paho C lib.

FrankRobot commented 4 months ago

Thanks Frank! The workaround works, build ok!

1096233486 commented 3 months ago

Oh, no, wait...

I was sure I tested all the combinations, but I was more concerned with getting PAHO_WITH_MQTT_C working, that I let this slip.

It appears that if you enable both targets (shared and static) in the Paho C library, it is building the static libraries but not exporting the -static CMake target!

As a quick fix, rebuild the Paho C library with static turned on and shared turned off, -DPAHO_BUILD_STATIC=ON -DPAHO_BUILD_SHARED=OFF

Sorry about the confusion. I will try to figure out the problem in the C library build and send a PR upstream.

bulid success,thinks from swpušŸ˜†

acidtonic commented 1 month ago

I am trying to get this working on Alpine and having similar issues but once I try the paho_c bundled, shared=off static=on config, I get errors about missing CMakeLists.txt

This was a mature building project before, kinda bummed about this.

cmake -Bbuild -H. -DCMAKE_CXX_COMPILER=clang++ -DPAHO_WITH_MQTT_C=ON -DPAHO_BUILD_SHARED=OFF -DPAHO_BUILD_STATIC=ON -DPAHO_BUILD_DOCUMENTATION=OFF -DPAHO_BUILD_SAMPLES=FALSE

The source directory
/BUILD/paho.mqtt.cpp/externals/paho-mqtt-c
does not contain a CMakeLists.txt file.
dinesht27 commented 1 month ago

Hello All, I am working on C++ project of compiling and sending messages via MQTT client. Visual Studio Version - 17 2022 Compiler - v142

I have used following steps to build and install the Paho C and C++, and wanted to link the libraries to the C++ Project. Build Steps which was successful. - (Build with Static and no shared libs as previous comments.) C -> cd paho.mqtt.c -> cmake -G "Visual Studio 17 2022" -Ax64 -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-c -DPAHO_BUILD_STATIC=ON -DPAHO_BUILD_SHARED=OFF -> cmake --build build/ --target install --config Release

C++ (For this I have used the following release - paho.mqtt.cpp-1.4.1 (the only version worked for me) ) ->cd paho.mqtt.cpp ->cmake -G "Visual Studio 17 2022" -Ax64 -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-cpp -DPAHO_BUILD_SAMPLES=ON -DPAHO_WITH_SSL=OFF -DCMAKE_PREFIX_PATH=C:\mqtt\paho-c ->cmake --build build/ --target install --config Release

The following is my CmakeList.txt where I also using the async for the target linking of libs CMakeLists.txt

And I also added my test example for tesing this MQTT publisher, - EbMQTTPublisher.cpp.txt

EbMQTTPublisher.h.txt

I have the following build error which I am not able to resolve for the past days -

paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _Cnd_timedwait_for referenced in function "public: bool cdecl std::condition_variable::wait_for<int64,struct std::ratio<1,1000>,class >(class std::unique_lock &,class std::chrono::duration<__int64,struct std::ratio<1,1000> > const &,class )" (??$wait_for@_JU?$ratio@$00$0DOI@@std@@Vlambda_8753e8241f2a5a762f79a862fae56c72>@@@condition_variable@std@@QEAA_NAEAV?$unique_lock@Vmutex@std@@@1@AEBV?$duration@_JU?$ratio@$00$0DOI@@std@@@chrono@1@V<lambda_8753e8241f2a5a762f79a862fae56c72@@@Z) 1>paho-mqttpp3-static.lib(token.obj) : error LNK2001: unresolved external symbol _Cnd_timedwait_for

I would appreciate some suggestion and support here on where I have made things wrong, also I couldnt find much possibilities for installing the dlls, also if you ans this in the side.

Thank you much. BR Dinesh

1096233486 commented 1 month ago

Hello All, I am working on C++ project of compiling and sending messages via MQTT client. Visual Studio Version - 17 2022 Compiler - v142

I have used following steps to build and install the Paho C and C++, and wanted to link the libraries to the C++ Project. Build Steps which was successful. - (Build with Static and no shared libs as previous comments.) C -> cd paho.mqtt.c -> cmake -G "Visual Studio 17 2022" -Ax64 -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-c -DPAHO_BUILD_STATIC=ON -DPAHO_BUILD_SHARED=OFF -> cmake --build build/ --target install --config Release

C++ (For this I have used the following release - paho.mqtt.cpp-1.4.1 (the only version worked for me) ) ->cd paho.mqtt.cpp ->cmake -G "Visual Studio 17 2022" -Ax64 -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-cpp -DPAHO_BUILD_SAMPLES=ON -DPAHO_WITH_SSL=OFF -DCMAKE_PREFIX_PATH=C:\mqtt\paho-c ->cmake --build build/ --target install --config Release

The following is my CmakeList.txt where I also using the async for the target linking of libs CMakeLists.txt

And I also added my test example for tesing this MQTT publisher, - EbMQTTPublisher.cpp.txt

EbMQTTPublisher.h.txt

I have the following build error which I am not able to resolve for the past days -

paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _Cnd_timedwait_for referenced in function "public: bool cdecl std::condition_variable::wait_for<int64,struct std::ratio<1,1000>,class >(class std::unique_lock &,class std::chrono::duration<__int64,struct std::ratio<1,1000> > const &,class )" (??$wait_for@_JU?$ratio@$00$0DOI@@std@@vlambda_8753e8241f2a5a762f79a862fae56c72>@@@condition_variable@std@@QEAA_NAEAV?$unique_lock@Vmutex@std@@@1@AEBV?$duration@_JU?$ratio@$00$0DOI@@std@@@chrono@1@V<lambda_8753e8241f2a5a762f79a862fae56c72@@@z) 1>paho-mqttpp3-static.lib(token.obj) : error LNK2001: unresolved external symbol _Cnd_timedwait_for

I would appreciate some suggestion and support here on where I have made things wrong, also I couldnt find much possibilities for installing the dlls, also if you ans this in the side.

Thank you much. BR Dinesh

Previously, I successfully built using gcc/g++on a Linux system, but did not use VS. I hope these points can help you: 1态 Ensure to build C first and then build CPP 2态At that time, there seemed to be some issues with the static construction of this projectļ¼ŒYou can try building SHARED libraryļ¼Œ-DPAHO_BUILD_STATIC=OFF -DPAHO_BUILD_SHARED=ON(you will get .dll file on windows) 3态If you are building a QT project, you can try using QT's Mqtt library(At that time, I was coding QT project) 4态Seeking help from the versatile GPTšŸ‘šŸ˜‰