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