8000 Fix support for building static library with Visual Studio by chrisdembia · Pull Request #61 · docopt/docopt.cpp · GitHub
[go: up one dir, main page]

Skip to content

Fix support for building static library with Visual Studio #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix EXPORTS macros for building static library on windows.
  • Loading branch information
chrisdembia committed Oct 17, 2016
commit f84c6f4e8f44c26a2e6cdf42cb324c53613071c0
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ set(docopt_HEADERS
#============================================================================
# Compile targets
#============================================================================
if(XCODE)
if(MSVC OR XCODE)
# MSVC requires __declspec() attributes, which are achieved via the
# DOCOPT_DLL and DOCOPT_EXPORTS macros below. Since those macros are only
# defined when building a shared library, we must build the shared and
# static libraries completely separately.
# Xcode does not support libraries with only object files as sources.
# See https://cmake.org/cmake/help/v3.0/command/add_library.html?highlight=add_library
add_library(docopt SHARED ${docopt_SOURCES} ${docopt_HEADERS})
add_library(docopt_s STATIC ${docopt_SOURCES} ${docopt_HEADERS})
else()
# If not using Xcode, we will create an intermediate object target to avoid
# compiling the source code twice.
# If not using MSVC or Xcode, we will create an intermediate object target
# to avoid compiling the source code twice.
add_library(docopt_o OBJECT ${docopt_SOURCES} ${docopt_HEADERS})
set_target_properties(docopt_o PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

Expand All @@ -57,6 +61,15 @@ endif()
target_include_directories(docopt PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> $<INSTALL_INTERFACE:include/docopt>)
target_include_directories(docopt_s PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> $<INSTALL_INTERFACE:include/docopt>)

if(MSVC)
# DOCOPT_DLL: Must be specified when building *and* when using the DLL.
# That's what the "PUBLIC" means.
# DOCOPT_EXPORTS: Must use __declspec(dllexport) when building the DLL.
# "PRIVATE" means it's only defined when building the DLL.
target_compile_definitions(docopt PUBLIC DOCOPT_DLL
PRIVATE DOCOPT_EXPORTS)
endif()

if(NOT MSVC)
set_target_properties(docopt PROPERTIES OUTPUT_NAME docopt)
set_target_properties(docopt_s PROPERTIES OUTPUT_NAME docopt)
Expand Down
27 changes: 23 additions & 4 deletions docopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@
#include <string>

#ifdef DOCOPT_HEADER_ONLY
#define DOCOPT_INLINE inline
#define DOCOPT_INLINE inline
#define DOCOPT_API
#else
#define DOCOPT_INLINE
#define DOCOPT_INLINE

// With Microsoft Visual Studio, export certain symbols so they
// are available to users of docopt.dll (shared library). The DOCOPT_DLL
// macro should be defined if building a DLL (with Visual Studio),
// and by clients using the DLL. The CMakeLists.txt and the
// docopt-config.cmake it generates handle this.
#ifdef DOCOPT_DLL
// Whoever is *building* the DLL should define DOCOPT_EXPORTS.
// The CMakeLists.txt that comes with docopt does this.
// Clients of docopt.dll should NOT define DOCOPT_EXPORTS.
#ifdef DOCOPT_EXPORTS
#define DOCOPT_API __declspec(dllexport)
#else
#define DOCOPT_API __declspec(dllimport)
#endif
#else
#define DOCOPT_API
#endif
#endif

namespace docopt {
Expand Down Expand Up @@ -48,7 +67,7 @@ namespace docopt {
/// @throws DocoptExitHelp if 'help' is true and the user has passed the '--help' argument
/// @throws DocoptExitVersion if 'version' is true and the user has passed the '--version' argument
/// @throws DocoptArgumentError if the user's argv did not match the usage patterns
std::map<std::string, value> docopt_parse(std::string const& doc,
std::map<std::string, value> DOCOPT_API docopt_parse(std::string const& doc,
std::vector<std::string> const& argv,
bool help = true,
bool version = true,
Expand All @@ -61,7 +80,7 @@ namespace docopt {
/// * DocoptExitHelp - print usage string and terminate (with exit code 0)
/// * DocoptExitVersion - print version and terminate (with exit code 0)
/// * DocoptArgumentError - print error and usage string and terminate (with exit code -1)
std::map<std::string, value> docopt(std::string const& doc,
std::map<std::string, value> DOCOPT_API docopt(std::string const& doc,
std::vector<std::string> const& argv,
bool help = true,
std::string const& version = {},
Expand Down
0