Skip to content

Commit 719823c

Browse files
authored
Turn libfmt-c into a shared library (#4812)
Linux distros don't like having static archives. Set fmt-c to shared just like the main fmt library.
1 parent 62abc0f commit 719823c

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,19 @@ target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1)
417417
target_compile_features(fmt-header-only INTERFACE cxx_std_11)
418418
setup_target(fmt-header-only INTERFACE)
419419

420-
add_library(fmt-c STATIC src/fmt-c.cc)
420+
add_library(fmt-c src/fmt-c.cc)
421421
target_compile_features(fmt-c INTERFACE c_std_11)
422+
if (BUILD_SHARED_LIBS)
423+
target_compile_definitions(
424+
fmt-c
425+
PRIVATE FMT_LIB_EXPORT
426+
INTERFACE FMT_SHARED)
427+
endif ()
428+
set_target_properties(
429+
fmt-c
430+
PROPERTIES VERSION ${FMT_VERSION}
431+
SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}
432+
DEBUG_POSTFIX "${FMT_DEBUG_POSTFIX}")
422433
if (MSVC)
423434
target_compile_options(fmt-c PUBLIC /Zc:preprocessor)
424435
endif ()

include/fmt/fmt-c.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@
1212
#include <stddef.h> // size_t
1313
#include <stdio.h> // FILE
1414

15+
#if defined(__GNUC__)
16+
# define FMT_CVISIBILITY(value) __attribute__((visibility(value)))
17+
#else
18+
# define FMT_CVISIBILITY(value)
19+
#endif
20+
21+
#if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
22+
# if defined(FMT_LIB_EXPORT)
23+
# define FMT_CAPI __declspec(dllexport)
24+
# elif defined(FMT_SHARED)
25+
# define FMT_CAPI __declspec(dllimport)
26+
# endif
27+
#elif defined(FMT_LIB_EXPORT) || defined(FMT_SHARED)
28+
# define FMT_CAPI FMT_CVISIBILITY("default")
29+
#endif
30+
#ifndef FMT_CAPI
31+
# define FMT_CAPI
32+
#endif
33+
1534
#ifdef __cplusplus
1635
extern "C" {
1736
#endif
@@ -45,10 +64,10 @@ typedef struct {
4564

4665
enum { fmt_error = -1, fmt_error_invalid_arg = -2 };
4766

48-
int fmt_vformat(char* buffer, size_t size, const char* fmt, const fmt_arg* args,
49-
size_t num_args);
50-
int fmt_vprint(FILE* stream, const char* fmt, const fmt_arg* args,
51-
size_t num_args);
67+
int FMT_CAPI fmt_vformat(char* buffer, size_t size, const char* fmt,
68+
const fmt_arg* args, size_t num_args);
69+
int FMT_CAPI fmt_vprint(FILE* stream, const char* fmt, const fmt_arg* args,
70+
size_t num_args);
5271

5372
#ifdef __cplusplus
5473
}
@@ -92,7 +111,7 @@ static inline fmt_arg fmt_from_ptr(const void* x) {
92111
return (fmt_arg){.type = fmt_pointer, .value.pointer = x};
93112
}
94113

95-
void fmt_unsupported_type(void);
114+
void FMT_CAPI fmt_unsupported_type(void);
96115

97116
# if !defined(_MSC_VER) || defined(__clang__)
98117
typedef signed char fmt_signed_char;

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,5 @@ endif ()
243243

244244
enable_language(C)
245245
add_executable(c-test c-test.c)
246-
target_link_libraries(c-test PRIVATE fmt::fmt-c)
246+
target_link_libraries(c-test PUBLIC fmt::fmt-c)
247247
add_test(NAME c-test COMMAND c-test)

0 commit comments

Comments
 (0)