0

I'vi made a minimum project that uses mosquitto with CMake. The project contains a single msqTest.cpp file which has a main with some mosquitto calls.

In my CMakeLists.txt i link mosquitto_static.lib

Im building the mosquitto library with the standard CMakeLists.

When building my project I get the following linker error:

msqTest.obj : error LNK2019: unresolved external symbol __imp_mosquitto_lib_init referenced in function main 

This error occurs for every call to the library.

What I've done is:

Download and build Mosquitto from source: from here

mkdir build/win
cd build/win
cmake ../..
cmake --build . --config Release

Builds using the standart CMakeLists without problems, and results in a:

mosquitto_static.lib

Since build static is = on

option(WITH_STATIC_LIBRARIES "Build static versions of the libmosquitto/pp libraries?" ON)

In the root project "msqTest" i do the same

mkdir build
cd build
cmake ..
cmake --build . --config Release

with the CMakeLists file:

cmake_minimum_required(VERSION 3.10)
project(Test VERSION 1.99)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

if (WIN32)
    add_definitions("-DNOMINMAX")               # Remove collision of windows.h and std:: min() and max() functions
    add_definitions("-D_TIMESPEC_DEFINED")      # Remove collision of timespec in mosquitto and pthread
    add_definitions(/MP)                        # Multithreading build
    # add_definitions("-DWINDOWS_EXPORT_ALL_SYMBOLS")
    # add_definitions(/NODEFAULTLIB:library)      # Remove library type collisions
endif()

add_executable(Test msqTest.cpp)

# add_subdirectory(mosquitto)

target_include_directories(Test PUBLIC
    # New local download
    "${CMAKE_CURRENT_SOURCE_DIR}/mosquitto/include"      # Contains mosquitto.h
)



target_link_libraries(Test PUBLIC
    # New local build
    "${CMAKE_CURRENT_SOURCE_DIR}/mosquitto/build/win/lib/Release/mosquitto_static.lib"
)

And the msqTest.cpp containing main

#include <stdlib.h>
#include <stdio.h>
extern "C" {
#include "mosquitto.h"
}

int main(int argc, char *argv[])
{
    int rc;
    struct mosquitto_message *msg;

    mosquitto_lib_init();

    rc = mosquitto_subscribe_simple(
            &msg, 1, true,
            "irc/#", 0,
            "test.mosquitto.org", 1883,
            NULL, 60, true,
            NULL, NULL,
            NULL, NULL);

    if(rc){
        printf("Error: %s\n", mosquitto_strerror(rc));
        mosquitto_lib_cleanup();
        return rc;
    }

    printf("%s %s\n", msg->topic, (char *)msg->payload);
    mosquitto_message_free(&msg);

    mosquitto_lib_cleanup();


    system("pause");
    return 0;
}

I'm not sure what I have to do for the linker to see the functions in the .lib file.

Do I need to make them public somehow?

My directory looks like:

testMosquitto
├── CMakeLists.txt
├── msqTest.cpp
├── build
│   ├── build stuff
├── mosquitto (downloaded)
│   ├── CMakeLists.txt
│   ├── entire mosquitto repository
│   ├── build
│   │   ├── win
│   │   │   ├── build stuff
Malle
  • 1
  • 1
  • Since you use mosquitto as a **static** library, you need to define macro `LIBMOSQUITTO_STATIC`. Otherwise the header [mosquitto.h](https://github.com/eclipse/mosquitto/blob/master/include/mosquitto.h#L38) marks the mosquitto functions as imported (note on the `__imp_` prefix in the error message). See also [that answer](https://stackoverflow.com/questions/5159353/how-can-i-get-rid-of-the-imp-prefix-in-the-linker-in-vc) about similar error message. – Tsyvarev Feb 08 '23 at 17:36
  • Thank you very much @Tsyvarev! For everyone seeing this later i added `add_definitions("-DLIBMOSQUITTO_STATIC")` to the precompiler definition list. This makes mosquitto.h define define `libmosq_EXPORT` – Malle Feb 09 '23 at 09:35

0 Answers0