eclipse / paho.mqtt.cpp

Other
1.01k stars 432 forks source link

Linker-Error when compiling with v1.3.2 #482

Closed fgrz closed 2 months ago

fgrz commented 8 months ago

Hi

In our project we used v1.2.0 so far. I now wanted to update to v1.3.2 because we want to use websockets (wss://...) but the project does no longer compile with the new built libs: Severity Code Description Project File Line Suppression State Details Error LNK2001 unresolved external symbol "public: static int const mqtt::message::DFLT_QOS" (?DFLT_QOS@message@mqtt@@2HB) CutCenter C:\git\zcc\CutCenter\Mqtt.lib(MqttAsyncPublisher.obj) 1
Error LNK2001 unresolved external symbol "public: static bool const mqtt::message::DFLT_RETAINED" (?DFLT_RETAINED@message@mqtt@@2_NB) CutCenter C:\git\zcc\CutCenter\Mqtt.lib(MqttAsyncPublisher.obj) 1

The libs are built with a powershell-script (cmake is from VS 2022):

        & $cmakeExecutable -A x64 -G $generatorName -B $pathToSourceC\$buildType\build -S $pathToSourceC `
        -D CMAKE_BUILD_TYPE=$configName `
        -D CMAKE_INSTALL_PREFIX="$pathToBuild\$buildType" `
        -D MSVC_TOOLSET_VERSION=143 `
        -D PAHO_BUILD_SAMPLES=FALSE `
        -D PAHO_ENABLE_TESTING=FALSE `
        -D PAHO_WITH_SSL=TRUE `
        -D OPENSSL_ROOT_DIR="$pathToOpenSsl" `
        -D CMAKE_PDB_OUTPUT_DIRECTORY="$pathToBuild\$buildType\pdbs" `
        -D CMAKE_SHARED_LINKER_FLAGS_INIT=/SOURCELINK:$pathToBuild\paho-mqtt.sourcelink.json 
        & $cmakeExecutable --build "$pathToSourceC\$buildType\build" --target install --config $configName

        #cpp build#
        & $cmakeExecutable -A x64 -G $generatorName -B $pathToSourceCpp\$buildType\build -S $pathToSourceCpp `
        -D CMAKE_BUILD_TYPE=$configName `
        -D CMAKE_INSTALL_PREFIX="$pathToBuild\$buildType" `
        -D PAHO_MQTT_C_LIBRARIES="$pathToBuild\$buildType\lib\paho-mqtt3as.lib" `
        -D PAHO_MQTT_C_INCLUDE_DIRS="$pathToBuild\$buildType\include" `
        -D PAHO_BUILD_SHARED=TRUE `
        -D PAHO_BUILD_STATIC=FALSE `
        -D PAHO_WITH_SSL=TRUE `
        -D OPENSSL_ROOT_DIR="$pathToOpenSsl" `
        -D MSVC_TOOLSET_VERSION=143 `
        -D PAHO_BUILD_SAMPLES=FALSE `
        -D PAHO_BUILD_TESTS=FALSE `
        -D CMAKE_PDB_OUTPUT_DIRECTORY="$pathToBuild\$buildType\pdbs" `
        -D CMAKE_SHARED_LINKER_FLAGS_INIT=/SOURCELINK:$pathToBuild\paho-mqtt.sourcelink.json 
        & $cmakeExecutable --build "$pathToSourceCpp\$buildType\build" --target install --config $configName

The include in our project is done like this:

#pragma comment( lib, "paho-mqtt3as.lib" )
#pragma comment( lib, "paho-mqttpp3.lib" )

The code stops compiling when using this line in our code:

mqtt::message_ptr        publishMsg   = mqtt::make_message( subTopic.toStdString(), msg.toStdString() );

When I modify message.h/message.cpp so DFLT_QOS and DFLT_RETAINED are initialized in the header-file, it works too - but we idealy do not want to change the paho mqtt-source before compile it:

class message
{
public:
    /** The default QoS for a message */
    PAHO_MQTTPP_EXPORT static const int DFLT_QOS = 0;  // =0
    /** The default retained flag */
    PAHO_MQTTPP_EXPORT static const bool DFLT_RETAINED = false;  // =false

Any ideas what I can do so the code compiles without change the paho mqtt-code?

Thanks!

swenp76 commented 6 months ago

Same happened using vcpkg under windows. Got exactly the same linker errors. Using the overloaded mqtt::make_message (string_ref topic, const void *payload, size_t len, int qos, bool retained) function did the trick for me without changing the paho code.

MennoK commented 5 months ago

I'm having the same issue. Currently resolved with suggestion from @swenp76 .

ditroiaj commented 4 months ago

I also experienced the same issue and the suggestion from @swenp76 solved it. Thanks.

fpagliughi commented 3 months ago

I think this tracks to some poorly-thought-out overloads that can collide and cause trouble on different compilers and platforms with different word sizes. There's a related issue from a few months back that was tracked back to the same problem. I will link to it if I find it.

fpagliughi commented 2 months ago

There are a few old tickets regarding some of these problems and similar build issues on Windows. They were caused by several different issues, particularly that CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS does not export all symbols (news to me), and also that there was a change between how C++11 and C++17 handle static class constants. So there was a problem linking an app built for C++17 against the library built for C++11. Clang and gcc on Linux were more forgiving, but MSVC gave link errors.

I believe the export issues are solved in v1.4.0. That should fix the linker issues, regardless of the language version, but the upcoming v1.5 will also move the library to C++17, and further reduce any problems. In that version, the class static constants will also become constexpr whenever possible.

If anyone is still having issues wit this using v1.4.0, please let me know and I will try to get it fixed before the v1.4.1 and v1.5.0 releases.