include: add CMOCKA_DLLEXTERN for MSVC DLLs

Add a new macro called `CMOCKA_DLLEXTERN` that represents Windows DLL
storage class specifiers.

Essentially, in order to publically export some data in a DLL, you must
either declare the function/data with `__declspec(dllexport)`, or list
the function/data in a `.def` file (which is what the CMocka project
does).

In order to import functions from a DLL, `__declspec(dllimport)` may
be used for a performance increase. However,
**`__declspec(dllimport)` is required for importing data from a DLL**!

The new `CMOCKA_DLLEXTERN` macro takes care of this for us, when we're
using MSVC.

See https://github.com/MicrosoftDocs/cpp-docs/blob/main/docs/cpp/definitions-and-declarations-cpp.md

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Alois Klink 2023-07-29 03:31:41 +01:00 committed by Andreas Schneider
parent 1b019882ba
commit 1442e46446
3 changed files with 35 additions and 8 deletions

View File

@ -20,6 +20,14 @@
#ifdef _WIN32
# ifdef _MSC_VER
# ifndef CMOCKA_STATIC
# ifdef CMOCKA_EXPORTS
#define CMOCKA_DLLEXTERN __declspec(dllexport)
# else
#define CMOCKA_DLLEXTERN __declspec(dllimport)
# endif /* CMOCKA_EXPORTS */
# endif /* ndef CMOCKA_STATIC */
#define __func__ __FUNCTION__
# ifndef inline
@ -38,6 +46,18 @@ int __stdcall IsDebuggerPresent();
# endif /* _MSC_VER */
#endif /* _WIN32 */
/**
* @def CMOCKA_DLLEXTERN
* This attribute is needed when dynamically linking to a data object in a DLL.
* It's optional (but increases performance) for dynamically linking to
* functions in a DLL.
* @see
* https://github.com/MicrosoftDocs/cpp-docs/blob/bd5a4fbd8ea3dd47b5c7a228c266cdddcaca0e00/docs/cpp/dllexport-dllimport.md
*/
#ifndef CMOCKA_DLLEXTERN
#define CMOCKA_DLLEXTERN // only needed on MSVC compiler when using a DLL
#endif /* ndef CMOCKA_DLLEXTERN */
/**
* @defgroup cmocka The CMocka API
*
@ -2780,9 +2800,9 @@ typedef struct CheckParameterEvent {
} CheckParameterEvent;
/* Used by expect_assert_failure() and mock_assert(). */
extern int global_expecting_assert;
extern jmp_buf global_expect_assert_env;
extern const char * global_last_failed_assert;
CMOCKA_DLLEXTERN extern int global_expecting_assert;
CMOCKA_DLLEXTERN extern jmp_buf global_expect_assert_env;
CMOCKA_DLLEXTERN extern const char * global_last_failed_assert;
/* Retrieves a value for the given function, as set by "will_return". */
CMockaValueData _mock(const char *const function,

View File

@ -37,7 +37,11 @@ target_compile_options(cmocka
target_compile_definitions(cmocka
PRIVATE
_GNU_SOURCE
_XOPEN_SOURCE=700)
_XOPEN_SOURCE=700
PUBLIC
# hides a warning on Windows when using static lib
"$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:CMOCKA_STATIC>"
)
if (CMOCKA_PLATFORM_INCLUDE)
target_compile_options(cmocka
@ -90,7 +94,10 @@ if (UNIT_TESTING)
target_compile_definitions(cmocka-static
PRIVATE
_GNU_SOURCE
_XOPEN_SOURCE=700)
_XOPEN_SOURCE=700
PUBLIC
CMOCKA_STATIC # hides a warning on Windows
)
if (CMOCKA_PLATFORM_INCLUDE)
target_compile_options(cmocka-static

View File

@ -52,9 +52,9 @@ EXPORTS
cmocka_set_message_output
cmocka_set_test_filter
cmocka_set_skip_filter
global_expect_assert_env
global_expecting_assert
global_last_failed_assert
global_expect_assert_env DATA
global_expecting_assert DATA
global_last_failed_assert DATA
mock_assert
print_error
print_message