1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-09 03:11:23 +08:00

Added Windows DLL export nonsense to make class static constants visible to apps

This commit is contained in:
fpagliughi 2023-11-20 19:30:04 -05:00
parent 6f79d51d53
commit 02cf646d84
20 changed files with 213 additions and 123 deletions

View File

@ -7,7 +7,7 @@
#*******************************************************************************
# Copyright (c) 2016-2017, Guilherme Maciel Ferreira
# Copyright (c) 2017-2018, Frank Pagliughi
# Copyright (c) 2017-2023, Frank Pagliughi
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
@ -40,6 +40,9 @@ endif()
add_subdirectory(mqtt)
include(GenerateExportHeader)
#generate_export_header(mylibrary)
## --- Library dependencies ---
set (THREADS_PREFER_PTHREAD_FLAG ON)
@ -75,14 +78,16 @@ add_library(paho-cpp-objs OBJECT
#target_include_directories(OBJS PRIVATE ${PAHO_MQTT_C_INCLUDE_DIRS})
target_include_directories(paho-cpp-objs
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
PRIVATE
${PAHO_MQTT_C_INCLUDE_DIRS}
src
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
PRIVATE
${PAHO_MQTT_C_INCLUDE_DIRS}
src
)
target_compile_definitions(paho-cpp-objs PRIVATE PAHO_MQTTPP_EXPORTS)
# --- Warnings ---
# Maybe set '-Werror' for Release builds?
@ -103,7 +108,7 @@ if(PAHO_BUILD_SHARED)
PRIVATE ${LIBS_SYSTEM}
PUBLIC PahoMqttC::PahoMqttC Threads::Threads)
# It would be nice to exort the include paths from the obj lib, but we
# It would be nice to export the include paths from the obj lib, but we
# get an export error. Perhaps in a future version?
#
# CMake Error: install(EXPORT "PahoMqttCpp" ...) includes target "paho-mqttpp3"
@ -143,6 +148,7 @@ if(PAHO_BUILD_STATIC)
target_include_directories(paho-mqttpp3-static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)

View File

@ -23,12 +23,15 @@
namespace mqtt {
const std::chrono::seconds client::DFLT_TIMEOUT = std::chrono::seconds(30);
const int client::DFLT_QOS = 1;
/////////////////////////////////////////////////////////////////////////////
PAHO_MQTTPP_EXPORT const std::chrono::seconds client::DFLT_TIMEOUT
= std::chrono::seconds(30);
PAHO_MQTTPP_EXPORT const int client::DFLT_QOS = 1;
// --------------------------------------------------------------------------
client::client(const string& serverURI, const string& clientId,
iclient_persistence* persistence /*=nullptr*/)
: cli_(serverURI, clientId, persistence),

View File

@ -25,18 +25,20 @@ namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT =
PAHO_MQTTPP_EXPORT const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT =
MQTTAsync_connectOptions_initializer;
const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT5 =
PAHO_MQTTPP_EXPORT const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT5 =
MQTTAsync_connectOptions_initializer5;
const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT_WS =
PAHO_MQTTPP_EXPORT const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT_WS =
MQTTAsync_connectOptions_initializer_ws;
const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT5_WS =
PAHO_MQTTPP_EXPORT const MQTTAsync_connectOptions connect_options::DFLT_C_STRUCT5_WS =
MQTTAsync_connectOptions_initializer5_ws;
// --------------------------------------------------------------------------
connect_options::connect_options(int ver /*=MQTTVERSION_DEFAULT*/)
{
opts_ = (ver < MQTTVERSION_5) ? DFLT_C_STRUCT : DFLT_C_STRUCT5;
@ -368,8 +370,8 @@ void connect_options::set_https_proxy(const string& httpsProxy)
/////////////////////////////////////////////////////////////////////////////
// connect_data
const MQTTAsync_connectData connect_data::DFLT_C_STRUCT =
MQTTAsync_connectData_initializer;
PAHO_MQTTPP_EXPORT const MQTTAsync_connectData connect_data::DFLT_C_STRUCT
= MQTTAsync_connectData_initializer;
connect_data::connect_data() : data_(DFLT_C_STRUCT)
{

View File

@ -25,10 +25,10 @@ namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
const int message::DFLT_QOS = 0;
const bool message::DFLT_RETAINED = false;
PAHO_MQTTPP_EXPORT const int message::DFLT_QOS = 0;
PAHO_MQTTPP_EXPORT const bool message::DFLT_RETAINED = false;
const MQTTAsync_message message::DFLT_C_STRUCT = MQTTAsync_message_initializer;
PAHO_MQTTPP_EXPORT const MQTTAsync_message message::DFLT_C_STRUCT = MQTTAsync_message_initializer;
// --------------------------------------------------------------------------

View File

@ -1,5 +1,5 @@
#*******************************************************************************
# Copyright (c) 2016-2018
# Copyright (c) 2016-2023
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
@ -27,10 +27,12 @@ install(
delivery_token.h
disconnect_options.h
exception.h
export.h
iaction_listener.h
iasync_client.h
iclient_persistence.h
message.h
platform.h
properties.h
response_options.h
server_response.h

View File

@ -38,9 +38,9 @@ namespace mqtt {
class client : private callback
{
/** An arbitrary, but relatively long timeout */
static const std::chrono::seconds DFLT_TIMEOUT;
PAHO_MQTTPP_EXPORT static const std::chrono::seconds DFLT_TIMEOUT;
/** The default quality of service */
static const int DFLT_QOS; // =1;
PAHO_MQTTPP_EXPORT static const int DFLT_QOS; // =1;
/** The actual client */
async_client cli_;

View File

@ -32,6 +32,7 @@
#include "mqtt/string_collection.h"
#include "mqtt/will_options.h"
#include "mqtt/ssl_options.h"
#include "mqtt/platform.h"
#include <vector>
#include <map>
#include <chrono>
@ -47,16 +48,16 @@ namespace mqtt {
class connect_options
{
/** The default C struct for non-WebSocket connections */
static const MQTTAsync_connectOptions DFLT_C_STRUCT;
PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT;
/** The default C struct for non-Websocket MQTT v5 connections */
static const MQTTAsync_connectOptions DFLT_C_STRUCT5;
PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT5;
/** The default C struct for WebSocket connections */
static const MQTTAsync_connectOptions DFLT_C_STRUCT_WS;
PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT_WS;
/** The default C struct for Websocket MQTT v5 connections */
static const MQTTAsync_connectOptions DFLT_C_STRUCT5_WS;
PAHO_MQTTPP_EXPORT static const MQTTAsync_connectOptions DFLT_C_STRUCT5_WS;
/** The underlying C connection options */
MQTTAsync_connectOptions opts_;
@ -585,7 +586,7 @@ using connect_options_ptr = connect_options::ptr_t;
class connect_data
{
/** The default C struct */
static const MQTTAsync_connectData DFLT_C_STRUCT;
PAHO_MQTTPP_EXPORT static const MQTTAsync_connectData DFLT_C_STRUCT;
/** The underlying C connect data */
MQTTAsync_connectData data_;

45
src/mqtt/export.h Normal file
View File

@ -0,0 +1,45 @@
/////////////////////////////////////////////////////////////////////////////
/// @file export.h
/// Library symbol export definitions, primarily for Windows MSVC DLL's
/// @date November 20, 2023
/// @author Frank Pagliughi
/////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Copyright (c) 2023 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
* Frank Pagliughi - MQTT v5 support
*******************************************************************************/
#ifndef __mqtt_export_h
#define __mqtt_export_h
#if defined(_WIN32) && defined(_MSC_VER)
#if defined(PAHO_MQTTPP_EXPORTS)
#define PAHO_MQTTPP_EXPORT __declspec(dllexport)
#elif defined(PAHO_MQTTPP_IMPORTS)
#define PAHO_MQTTPP_EXPORT __declspec(dllimport)
#else
#define PAHO_MQTTPP_EXPORT
#endif
#else
#if defined(PAHO_MQTTPP_EXPORTS)
#define PAHO_MQTTPP_EXPORT __attribute__ ((visibility ("default")))
#else
#define PAHO_MQTTPP_EXPORT
#endif
#endif
#endif // __mqtt_export_h

View File

@ -29,6 +29,7 @@
#include "mqtt/buffer_ref.h"
#include "mqtt/properties.h"
#include "mqtt/exception.h"
#include "mqtt/platform.h"
#include <memory>
namespace mqtt {
@ -55,13 +56,13 @@ class message
{
public:
/** The default QoS for a message */
static const int DFLT_QOS; // =0
PAHO_MQTTPP_EXPORT static const int DFLT_QOS; // =0
/** The default retained flag */
static const bool DFLT_RETAINED; // =false
PAHO_MQTTPP_EXPORT static const bool DFLT_RETAINED; // =false
private:
/** Initializer for the C struct (from the C library) */
static const MQTTAsync_message DFLT_C_STRUCT;
PAHO_MQTTPP_EXPORT static const MQTTAsync_message DFLT_C_STRUCT;
/** The underlying C message struct */
MQTTAsync_message msg_;

30
src/mqtt/platform.h Normal file
View File

@ -0,0 +1,30 @@
/////////////////////////////////////////////////////////////////////////////
/// @file platform.h
/// Paho MQTT platform-specific code
/// @date Nov 19, 2023
/// @author Frank Pagliughi
/////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Copyright (c) 2023 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
#ifndef __mqtt_platform_h
#define __mqtt_platform_h
#include "mqtt/export.h"
#endif // __mqtt_platform_h

View File

@ -6,7 +6,7 @@
/////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Copyright (c) 2019 Frank Pagliughi <fpagliughi@mindspring.com>
* Copyright (c) 2019-2023 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
@ -31,6 +31,7 @@ extern "C" {
#include "mqtt/types.h"
#include "mqtt/buffer_ref.h"
#include "mqtt/exception.h"
#include "mqtt/platform.h"
#include <tuple>
#include <initializer_list>
@ -254,7 +255,7 @@ inline string_pair get<string_pair>(const property& prop) {
class properties
{
/** The default C struct */
static const MQTTProperties DFLT_C_STRUCT;
PAHO_MQTTPP_EXPORT static const MQTTProperties DFLT_C_STRUCT;
/** The underlying C properties struct */
MQTTProperties props_;

View File

@ -31,6 +31,7 @@
#include "mqtt/message.h"
#include "mqtt/topic.h"
#include "mqtt/types.h"
#include "mqtt/platform.h"
#include <vector>
#include <functional>
@ -64,7 +65,7 @@ public:
private:
/** The default C struct */
static const MQTTAsync_SSLOptions DFLT_C_STRUCT ;
PAHO_MQTTPP_EXPORT static const MQTTAsync_SSLOptions DFLT_C_STRUCT ;
/** The underlying C SSL options */
MQTTAsync_SSLOptions opts_;

View File

@ -27,6 +27,7 @@
#include "MQTTAsync.h"
#include "MQTTSubscribeOpts.h"
#include "mqtt/types.h"
#include "mqtt/platform.h"
namespace mqtt {
@ -53,17 +54,17 @@ public:
using const_ptr_t = std::shared_ptr<const subscribe_options>;
/** Don't receive our own publications */
static const bool SUBSCRIBE_NO_LOCAL; // =true;
PAHO_MQTTPP_EXPORT static const bool SUBSCRIBE_NO_LOCAL; // =true;
/** Receive our own publications */
static const bool SUBSCRIBE_LOCAL; // =false;
PAHO_MQTTPP_EXPORT static const bool SUBSCRIBE_LOCAL; // =false;
/**
* Retain flag is only set on publications sent by a broker if in
* response to a subscribe request
*/
static const bool NO_RETAIN_AS_PUBLISHED; // =false;
PAHO_MQTTPP_EXPORT static const bool NO_RETAIN_AS_PUBLISHED; // =false;
/** Keep the retain flag as on the original publish message */
static const bool RETAIN_AS_PUBLISHED; // =true;
PAHO_MQTTPP_EXPORT static const bool RETAIN_AS_PUBLISHED; // =true;
/** The options for subscription retain handling */
enum RetainHandling {

View File

@ -30,6 +30,7 @@
#include "mqtt/types.h"
#include "mqtt/message.h"
#include "mqtt/topic.h"
#include "mqtt/platform.h"
namespace mqtt {
@ -49,13 +50,13 @@ class will_options
{
public:
/** The default QoS for the LWT, if unspecified */
static const int DFLT_QOS; // =0;
PAHO_MQTTPP_EXPORT static const int DFLT_QOS; // =0;
/** The defalut retained flag for LWT, if unspecified */
static const bool DFLT_RETAINED; // =false;
PAHO_MQTTPP_EXPORT static const bool DFLT_RETAINED; // =false;
private:
/** A default C struct to support re-initializing variables */
static const MQTTAsync_willOptions DFLT_C_STRUCT;
PAHO_MQTTPP_EXPORT static const MQTTAsync_willOptions DFLT_C_STRUCT;
/** The underlying C LWT options */
MQTTAsync_willOptions opts_;

View File

@ -154,7 +154,8 @@ property& property::operator=(property&& rhs)
/////////////////////////////////////////////////////////////////////////////
const MQTTProperties properties::DFLT_C_STRUCT = MQTTProperties_initializer;
PAHO_MQTTPP_EXPORT const MQTTProperties properties::DFLT_C_STRUCT
= MQTTProperties_initializer;
properties::properties() : props_{DFLT_C_STRUCT}
{

View File

@ -31,82 +31,66 @@ find_package(Threads REQUIRED)
## include directories
#include_directories(${CMAKE_CURRENT_SOURCE_DIR})
## binary files
add_executable(async_publish async_publish.cpp)
add_executable(async_publish_time async_publish_time.cpp)
add_executable(async_subscribe async_subscribe.cpp)
add_executable(async_consume async_consume.cpp)
add_executable(async_consume_v5 async_consume_v5.cpp)
add_executable(sync_publish sync_publish.cpp)
add_executable(sync_consume sync_consume.cpp)
add_executable(sync_consume_v5 sync_consume_v5.cpp)
add_executable(sync_reconnect sync_reconnect.cpp)
add_executable(data_publish data_publish.cpp)
add_executable(topic_publish topic_publish.cpp)
add_executable(rpc_math_cli rpc_math_cli.cpp)
add_executable(rpc_math_srvr rpc_math_srvr.cpp)
add_executable(mqttpp_chat mqttpp_chat.cpp)
add_executable(multithr_pub_sub multithr_pub_sub.cpp)
add_executable(ws_publish ws_publish.cpp)
add_executable(pub_speed_test pub_speed_test.cpp)
## link binaries
target_link_libraries(async_publish ${PAHO_CPP_LIB})
target_link_libraries(async_publish_time ${PAHO_CPP_LIB})
target_link_libraries(async_subscribe ${PAHO_CPP_LIB})
target_link_libraries(async_consume ${PAHO_CPP_LIB})
target_link_libraries(async_consume_v5 ${PAHO_CPP_LIB})
target_link_libraries(sync_publish ${PAHO_CPP_LIB})
target_link_libraries(sync_consume ${PAHO_CPP_LIB})
target_link_libraries(sync_consume_v5 ${PAHO_CPP_LIB})
target_link_libraries(sync_reconnect ${PAHO_CPP_LIB})
target_link_libraries(data_publish ${PAHO_CPP_LIB})
target_link_libraries(topic_publish ${PAHO_CPP_LIB})
target_link_libraries(rpc_math_cli ${PAHO_CPP_LIB})
target_link_libraries(rpc_math_srvr ${PAHO_CPP_LIB})
target_link_libraries(mqttpp_chat ${PAHO_CPP_LIB})
target_link_libraries(multithr_pub_sub ${PAHO_CPP_LIB})
target_link_libraries(ws_publish ${PAHO_CPP_LIB})
target_link_libraries(pub_speed_test ${PAHO_CPP_LIB})
set(INSTALL_TARGETS
async_publish
async_publish_time
async_subscribe
async_consume
async_consume_v5
sync_publish
sync_consume
sync_consume_v5
sync_reconnect
data_publish
rpc_math_cli
rpc_math_srvr
mqttpp_chat
topic_publish
multithr_pub_sub
ws_publish
pub_speed_test
# The sample applications
set(EXECUTABLES
async_publish
async_publish_time
async_subscribe
async_consume
async_consume_v5
sync_publish
sync_consume
sync_consume_v5
sync_reconnect
data_publish
rpc_math_cli
rpc_math_srvr
mqttpp_chat
topic_publish
multithr_pub_sub
ws_publish
pub_speed_test
)
if(PAHO_WITH_SSL)
## SSL binary files
add_executable(ssl_publish ssl_publish.cpp)
# These will only be built if SSL selected
set(SSL_EXECUTABLES
ssl_publish
)
## link SSL binaries
target_link_libraries(ssl_publish ${PAHO_CPP_LIB})
set(INSTALL_TARGETS
${INSTALL_TARGETS}
ssl_publish
)
add_definitions(-DOPENSSL)
endif()
## Build the samples
foreach(EXECUTABLE ${EXECUTABLES})
add_executable(${EXECUTABLE} ${EXECUTABLE}.cpp)
target_link_libraries(${EXECUTABLE} ${PAHO_CPP_LIB})
if(PAHO_BUILD_SHARED)
target_compile_definitions(${EXECUTABLE} PUBLIC PAHO_MQTTPP_IMPORTS)
if(MSVC AND PAHO_BUILD_STATIC)
target_link_libraries(${EXECUTABLE} ${LIBS_SYSTEM})
endif()
endif()
endforeach()
## install binaries
install(TARGETS ${INSTALL_TARGETS} EXPORT PahoMqttCppSamples
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
install(TARGETS ${EXECUTABLES} EXPORT PahoMqttCppSamples
RUNTIME DESTINATION bin
)
## Build the SSL/TLS samples, if selected
if(PAHO_WITH_SSL)
foreach(EXECUTABLE ${SSL_EXECUTABLES})
add_executable(${EXECUTABLE} ${EXECUTABLE}.cpp)
target_link_libraries(${EXECUTABLE} ${PAHO_CPP_LIB})
if(PAHO_BUILD_SHARED)
target_compile_definitions(${EXECUTABLE} PUBLIC PAHO_MQTTPP_IMPORTS)
if(MSVC AND PAHO_BUILD_STATIC)
target_link_libraries(${EXECUTABLE} ${LIBS_SYSTEM})
endif()
endif()
target_compile_definitions(${EXECUTABLE} PUBLIC OPENSSL)
endforeach()
install(TARGETS ${SSL_EXECUTABLES} EXPORT PahoMqttCppSamples
RUNTIME DESTINATION bin
)
endif()

View File

@ -24,7 +24,10 @@ namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
const MQTTAsync_SSLOptions ssl_options::DFLT_C_STRUCT = MQTTAsync_SSLOptions_initializer;
PAHO_MQTTPP_EXPORT const MQTTAsync_SSLOptions ssl_options::DFLT_C_STRUCT
= MQTTAsync_SSLOptions_initializer;
// --------------------------------------------------------------------------
ssl_options::ssl_options() : opts_(DFLT_C_STRUCT)
{

View File

@ -22,11 +22,11 @@ namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
const bool subscribe_options::SUBSCRIBE_NO_LOCAL = true;
const bool subscribe_options::SUBSCRIBE_LOCAL = false;
PAHO_MQTTPP_EXPORT const bool subscribe_options::SUBSCRIBE_NO_LOCAL = true;
PAHO_MQTTPP_EXPORT const bool subscribe_options::SUBSCRIBE_LOCAL = false;
const bool subscribe_options::NO_RETAIN_AS_PUBLISHED = false;
const bool subscribe_options::RETAIN_AS_PUBLISHED = true;
PAHO_MQTTPP_EXPORT const bool subscribe_options::NO_RETAIN_AS_PUBLISHED = false;
PAHO_MQTTPP_EXPORT const bool subscribe_options::RETAIN_AS_PUBLISHED = true;
/////////////////////////////////////////////////////////////////////////////
// end namespace 'mqtt'

View File

@ -22,10 +22,10 @@
namespace mqtt {
const int will_options::DFLT_QOS = 0;
const bool will_options::DFLT_RETAINED = false;
PAHO_MQTTPP_EXPORT const int will_options::DFLT_QOS = 0;
PAHO_MQTTPP_EXPORT const bool will_options::DFLT_RETAINED = false;
const MQTTAsync_willOptions will_options::DFLT_C_STRUCT = MQTTAsync_willOptions_initializer;
PAHO_MQTTPP_EXPORT const MQTTAsync_willOptions will_options::DFLT_C_STRUCT = MQTTAsync_willOptions_initializer;
/////////////////////////////////////////////////////////////////////////////

View File

@ -67,6 +67,14 @@ message(STATUS "Using library for unit tests: ${PAHO_CPP_LIB}")
target_link_libraries(unit_tests ${PAHO_CPP_LIB} Catch2::Catch2)
if(PAHO_BUILD_SHARED)
target_compile_definitions(unit_tests PUBLIC PAHO_MQTTPP_IMPORTS)
if(MSVC AND PAHO_BUILD_STATIC)
target_link_libraries(unit_tests ${LIBS_SYSTEM})
endif()
endif()
include(CTest)
include(Catch)