Skip to content

Commit 5b521ca

Browse files
author
Bertrand Darbon
authored
Add exports macro to allow Shared Library with hidden symbols by default (e.g. Windows) (#127)
1 parent 1c432e3 commit 5b521ca

23 files changed

+80
-42
lines changed

CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,26 @@ if(BUILD_TRANTOR_SHARED)
2727
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${INSTALL_LIB_DIR}")
2828
endif("${isSystemDir}" STREQUAL "-1")
2929
add_library(${PROJECT_NAME} SHARED)
30+
if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
31+
# Ignore MSVC C4251 and C4275 warning of exporting std objects with no dll export
32+
# We export class to facilitate maintenance, thus if you compile
33+
# drogon on windows as a shared library, you will need to use
34+
# exact same compiler for drogon and your app.
35+
target_compile_options(${PROJECT_NAME} PUBLIC /wd4251 /wd4275)
36+
endif()
3037
else(BUILD_TRANTOR_SHARED)
3138
add_library(${PROJECT_NAME} STATIC)
3239
endif(BUILD_TRANTOR_SHARED)
3340

41+
include(GenerateExportHeader)
42+
generate_export_header(${PROJECT_NAME} EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/exports/trantor/exports.h)
43+
3444
# include directories
3545
target_include_directories(
3646
${PROJECT_NAME}
3747
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
3848
$<INSTALL_INTERFACE:${INSTALL_INCLUDE_DIR}>
49+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/exports>
3950
PRIVATE ${PROJECT_SOURCE_DIR}
4051
${PROJECT_SOURCE_DIR}/trantor/utils
4152
${PROJECT_SOURCE_DIR}/trantor/net
@@ -95,13 +106,13 @@ else()
95106
endif()
96107
target_sources(${PROJECT_NAME} PRIVATE ${TRANTOR_SOURCES})
97108

109+
find_package(Threads)
110+
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
98111
if(WIN32)
99112
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32 Rpcrt4)
100113
if(OpenSSL_FOUND)
101114
target_link_libraries(${PROJECT_NAME} PRIVATE Crypt32 Secur32)
102115
endif(OpenSSL_FOUND)
103-
else(WIN32)
104-
target_link_libraries(${PROJECT_NAME} PRIVATE pthread)
105116
endif(WIN32)
106117

107118
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 14)
@@ -153,9 +164,11 @@ source_group("Public API"
153164
install(TARGETS trantor
154165
# IMPORTANT: Add the trantor library to the "export-set"
155166
EXPORT TrantorTargets
167+
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin
156168
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib
157169
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib)
158-
170+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/exports/trantor/exports.h
171+
DESTINATION ${INSTALL_INCLUDE_DIR}/trantor)
159172
install(FILES ${public_net_headers}
160173
DESTINATION ${INSTALL_INCLUDE_DIR}/trantor/net)
161174
install(FILES ${public_utils_headers}

trantor/net/Channel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <trantor/utils/Logger.h>
1818
#include <trantor/utils/NonCopyable.h>
19+
#include <trantor/exports.h>
1920
#include <functional>
2021
#include <assert.h>
2122
#include <memory>
@@ -28,7 +29,7 @@ class EventLoop;
2829
* events on the socket it manages.
2930
*
3031
*/
31-
class Channel : NonCopyable
32+
class TRANTOR_EXPORT Channel : NonCopyable
3233
{
3334
public:
3435
using EventCallback = std::function<void()>;

trantor/net/EventLoop.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <trantor/utils/NonCopyable.h>
2020
#include <trantor/utils/Date.h>
2121
#include <trantor/utils/LockFreeQueue.h>
22+
#include <trantor/exports.h>
2223
#include <thread>
2324
#include <memory>
2425
#include <vector>
@@ -50,7 +51,7 @@ enum
5051
* the event loop of the thread it belongs to, or call that thread the thread of
5152
* the event loop.
5253
*/
53-
class EventLoop : NonCopyable
54+
class TRANTOR_EXPORT EventLoop : NonCopyable
5455
{
5556
public:
5657
EventLoop();

trantor/net/EventLoopThread.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <trantor/net/EventLoop.h>
1818
#include <trantor/utils/NonCopyable.h>
19+
#include <trantor/exports.h>
1920
#include <mutex>
2021
#include <thread>
2122
#include <memory>
@@ -28,7 +29,7 @@ namespace trantor
2829
* @brief This class represents an event loop thread.
2930
*
3031
*/
31-
class EventLoopThread : NonCopyable
32+
class TRANTOR_EXPORT EventLoopThread : NonCopyable
3233
{
3334
public:
3435
explicit EventLoopThread(const std::string &threadName = "EventLoopThread");

trantor/net/EventLoopThreadPool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#pragma once
1616

1717
#include <trantor/net/EventLoopThread.h>
18+
#include <trantor/exports.h>
1819
#include <vector>
1920
#include <memory>
2021

@@ -24,7 +25,7 @@ namespace trantor
2425
* @brief This class represents a pool of EventLoopThread objects
2526
*
2627
*/
27-
class EventLoopThreadPool : NonCopyable
28+
class TRANTOR_EXPORT EventLoopThreadPool : NonCopyable
2829
{
2930
public:
3031
EventLoopThreadPool() = delete;

trantor/net/InetAddress.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define MUDUO_NET_INETADDRESS_H
2222

2323
#include <trantor/utils/Date.h>
24+
#include <trantor/exports.h>
2425

2526
#ifdef _WIN32
2627
#include <ws2tcpip.h>
@@ -41,7 +42,7 @@ namespace trantor
4142
* @brief Wrapper of sockaddr_in. This is an POD interface class.
4243
*
4344
*/
44-
class InetAddress
45+
class TRANTOR_EXPORT InetAddress
4546
{
4647
public:
4748
/**

trantor/net/Resolver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Author: Tao An
77

88
#pragma once
9+
#include <trantor/exports.h>
910
#include <memory>
1011
#include <trantor/net/EventLoop.h>
1112
#include <trantor/net/InetAddress.h>
@@ -17,7 +18,7 @@ namespace trantor
1718
* @note Although the c-ares library is not essential, it is recommended to
1819
* install it for higher performance
1920
*/
20-
class Resolver
21+
class TRANTOR_EXPORT Resolver
2122
{
2223
public:
2324
using Callback = std::function<void(const trantor::InetAddress&)>;

trantor/net/TcpClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <trantor/net/EventLoop.h>
2121
#include <trantor/net/InetAddress.h>
2222
#include <trantor/net/TcpConnection.h>
23+
#include <trantor/exports.h>
2324
#include <functional>
2425
#include <thread>
2526
#include <atomic>
@@ -33,7 +34,7 @@ class SSLContext;
3334
* @brief This class represents a TCP client.
3435
*
3536
*/
36-
class TcpClient : NonCopyable
37+
class TRANTOR_EXPORT TcpClient : NonCopyable
3738
{
3839
public:
3940
/**

trantor/net/TcpConnection.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
#pragma once
16+
#include <trantor/exports.h>
1617
#include <trantor/net/EventLoop.h>
1718
#include <trantor/net/InetAddress.h>
1819
#include <trantor/utils/NonCopyable.h>
@@ -25,14 +26,15 @@
2526
namespace trantor
2627
{
2728
class SSLContext;
28-
std::shared_ptr<SSLContext> newSSLServerContext(const std::string &certPath,
29-
const std::string &keyPath,
30-
bool useOldTLS = false);
29+
TRANTOR_EXPORT std::shared_ptr<SSLContext> newSSLServerContext(
30+
const std::string &certPath,
31+
const std::string &keyPath,
32+
bool useOldTLS = false);
3133
/**
3234
* @brief This class represents a TCP connection.
3335
*
3436
*/
35-
class TcpConnection
37+
class TRANTOR_EXPORT TcpConnection
3638
{
3739
public:
3840
TcpConnection() = default;

trantor/net/TcpServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <trantor/net/InetAddress.h>
2121
#include <trantor/net/TcpConnection.h>
2222
#include <trantor/utils/TimingWheel.h>
23+
#include <trantor/exports.h>
2324
#include <string>
2425
#include <memory>
2526
#include <set>
@@ -32,7 +33,7 @@ class SSLContext;
3233
* @brief This class represents a TCP server.
3334
*
3435
*/
35-
class TcpServer : NonCopyable
36+
class TRANTOR_EXPORT TcpServer : NonCopyable
3637
{
3738
public:
3839
/**

trantor/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ set_property(TARGET ${targets_list} PROPERTY CXX_EXTENSIONS OFF)
4646

4747
foreach(T ${targets_list})
4848
target_link_libraries(${T} PRIVATE trantor)
49-
endforeach()
49+
endforeach()

trantor/utils/AsyncFileLogger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <trantor/utils/NonCopyable.h>
1818
#include <trantor/utils/Date.h>
19+
#include <trantor/exports.h>
1920
#include <thread>
2021
#include <mutex>
2122
#include <string>
@@ -34,7 +35,7 @@ using StringPtrQueue = std::queue<StringPtr>;
3435
* asynchronously.
3536
*
3637
*/
37-
class AsyncFileLogger : NonCopyable
38+
class TRANTOR_EXPORT AsyncFileLogger : NonCopyable
3839
{
3940
public:
4041
/**

trantor/utils/ConcurrentTaskQueue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#pragma once
1616

1717
#include <trantor/utils/TaskQueue.h>
18+
#include <trantor/exports.h>
1819
#include <list>
1920
#include <memory>
2021
#include <vector>
@@ -28,7 +29,7 @@ namespace trantor
2829
* can be called a threads pool.
2930
*
3031
*/
31-
class ConcurrentTaskQueue : public TaskQueue
32+
class TRANTOR_EXPORT ConcurrentTaskQueue : public TaskQueue
3233
{
3334
public:
3435
/**

trantor/utils/Date.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#pragma once
1616

17+
#include <trantor/exports.h>
1718
#include <stdint.h>
1819
#include <string>
1920

@@ -25,7 +26,7 @@ namespace trantor
2526
* @brief This class represents a time point.
2627
*
2728
*/
28-
class Date
29+
class TRANTOR_EXPORT Date
2930
{
3031
public:
3132
Date() : microSecondsSinceEpoch_(0){};

trantor/utils/LogStream.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,16 @@ Fmt::Fmt(const char *fmt, T val)
258258

259259
// Explicit instantiations
260260

261-
template Fmt::Fmt(const char *fmt, char);
262-
263-
template Fmt::Fmt(const char *fmt, short);
264-
template Fmt::Fmt(const char *fmt, unsigned short);
265-
template Fmt::Fmt(const char *fmt, int);
266-
template Fmt::Fmt(const char *fmt, unsigned int);
267-
template Fmt::Fmt(const char *fmt, long);
268-
template Fmt::Fmt(const char *fmt, unsigned long);
269-
template Fmt::Fmt(const char *fmt, long long);
270-
template Fmt::Fmt(const char *fmt, unsigned long long);
271-
272-
template Fmt::Fmt(const char *fmt, float);
273-
template Fmt::Fmt(const char *fmt, double);
261+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, char);
262+
263+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, short);
264+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned short);
265+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, int);
266+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned int);
267+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, long);
268+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned long);
269+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, long long);
270+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned long long);
271+
272+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, float);
273+
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, double);

trantor/utils/LogStream.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
// Taken from muduo lib and modified. Classes in this file are used internally.
1818
#include <trantor/utils/NonCopyable.h>
19+
#include <trantor/exports.h>
1920

2021
#include <assert.h>
2122
#include <string.h> // memcpy
@@ -115,7 +116,7 @@ class FixedBuffer : NonCopyable
115116

116117
} // namespace detail
117118

118-
class LogStream : NonCopyable
119+
class TRANTOR_EXPORT LogStream : NonCopyable
119120
{
120121
using self = LogStream;
121122

@@ -247,7 +248,7 @@ class LogStream : NonCopyable
247248
std::string exBuffer_;
248249
};
249250

250-
class Fmt // : boost::noncopyable
251+
class TRANTOR_EXPORT Fmt // : boost::noncopyable
251252
{
252253
public:
253254
template <typename T>

trantor/utils/Logger.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ Logger::Logger(SourceFile file, int line, bool)
168168
Logger::~Logger()
169169
{
170170
logStream_ << T(" - ", 3) << sourceFile_ << ':' << fileLine_ << '\n';
171-
Logger::outputFunc_()(logStream_.bufferData(), logStream_.bufferLength());
171+
auto oFunc = Logger::outputFunc_();
172+
if (!oFunc)
173+
return;
174+
oFunc(logStream_.bufferData(), logStream_.bufferLength());
172175
if (level_ >= kError)
173176
Logger::flushFunc_()();
174177
// logStream_.resetBuffer();

trantor/utils/Logger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <trantor/utils/NonCopyable.h>
1818
#include <trantor/utils/Date.h>
1919
#include <trantor/utils/LogStream.h>
20+
#include <trantor/exports.h>
2021
#include <string.h>
2122
#include <functional>
2223
#include <iostream>
@@ -27,7 +28,7 @@ namespace trantor
2728
* @brief This class implements log functions.
2829
*
2930
*/
30-
class Logger : public NonCopyable
31+
class TRANTOR_EXPORT Logger : public NonCopyable
3132
{
3233
public:
3334
enum LogLevel

trantor/utils/MsgBuffer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#pragma once
1616
#include <trantor/utils/NonCopyable.h>
17+
#include <trantor/exports.h>
1718
#include <vector>
1819
#include <string>
1920
#include <algorithm>
@@ -34,7 +35,7 @@ static constexpr char CRLF[]{"\r\n"};
3435
* data.
3536
*
3637
*/
37-
class MsgBuffer
38+
class TRANTOR_EXPORT MsgBuffer
3839
{
3940
public:
4041
/**

trantor/utils/NonCopyable.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
*/
1414

1515
#pragma once
16+
17+
#include <trantor/exports.h>
18+
1619
namespace trantor
1720
{
1821
/**
1922
* @brief This class represents a non-copyable object.
2023
*
2124
*/
22-
class NonCopyable
25+
class TRANTOR_EXPORT NonCopyable
2326
{
2427
protected:
2528
NonCopyable()

0 commit comments

Comments
 (0)