Skip to content

Commit a80aa4d

Browse files
jll63claude
andcommitted
samples/windll: remove _WIN32 guards, Windows-only sample
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cb0a856 commit a80aa4d

4 files changed

Lines changed: 23 additions & 50 deletions

File tree

samples/windll/CMakeLists.txt

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,25 @@
33
# See accompanying file LICENSE_1_0.txt
44
# or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
# Illustrates the exe-exports-data / DLL-imports-data pattern on Windows:
7-
# the exe defines and exports a variable; the DLL imports and reads it.
8-
#
9-
# On non-Windows both compilation units are linked into a single executable.
6+
# Illustrates the dllvar technique: the exe owns (exports) a variable; the
7+
# DLL imports it. Both see static_answer<answer_dllvar, void>::value as the
8+
# same linker symbol because the varying base class does not enter the mangled
9+
# name; SFINAE selects dllexport in the owner and dllimport in the client.
1010

11-
if (WIN32 OR CYGWIN)
12-
# Exe exports the registry state and loads the plugin at runtime.
13-
add_executable(windll_main main.cpp)
14-
target_compile_definitions(windll_main PRIVATE WINDLL_OWNER)
15-
set_target_properties(windll_main PROPERTIES ENABLE_EXPORTS ON)
11+
if (NOT (WIN32 OR CYGWIN))
12+
return()
13+
endif()
1614

17-
# DLL imports the variable from the exe.
18-
add_library(windll_plugin SHARED plugin.cpp)
19-
target_link_libraries(windll_plugin PRIVATE windll_main)
20-
set_target_properties(windll_plugin PROPERTIES ENABLE_EXPORTS ON)
15+
add_executable(windll_main main.cpp)
16+
target_compile_definitions(windll_main PRIVATE WINDLL_OWNER)
17+
set_target_properties(windll_main PROPERTIES ENABLE_EXPORTS ON)
2118

22-
# windll_main loads windll_plugin at runtime; build both before testing.
23-
add_custom_target(windll_all ALL DEPENDS windll_main windll_plugin)
19+
add_library(windll_plugin SHARED plugin.cpp)
20+
target_link_libraries(windll_plugin PRIVATE windll_main)
2421

25-
add_test(
26-
NAME windll
27-
COMMAND windll_main
28-
WORKING_DIRECTORY $<TARGET_FILE_DIR:windll_main>)
29-
else()
30-
# Non-Windows: single executable, no DLL decoration needed.
31-
add_executable(windll_main main.cpp plugin.cpp)
32-
target_compile_definitions(windll_main PRIVATE WINDLL_OWNER)
22+
add_custom_target(windll_all ALL DEPENDS windll_main windll_plugin)
3323

34-
add_test(NAME windll COMMAND windll_main)
35-
endif()
24+
add_test(
25+
NAME windll
26+
COMMAND windll_main
27+
WORKING_DIRECTORY $<TARGET_FILE_DIR:windll_main>)

samples/windll/main.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@
77
#define WINDLL_OWNER
88
#include "shared.hpp"
99
#include <cassert>
10-
#ifdef _WIN32
1110
#include <windows.h>
12-
#endif
1311

1412
int main() {
1513
answer() = 42;
16-
#ifdef _WIN32
1714
auto plugin = LoadLibraryA("windll_plugin.dll");
1815
assert(plugin);
1916
auto get_answer = reinterpret_cast<int(*)()>(
2017
GetProcAddress(plugin, "get_answer"));
2118
assert(get_answer() == 42);
2219
FreeLibrary(plugin);
23-
#else
24-
extern int get_answer();
25-
assert(get_answer() == 42);
26-
#endif
2720
}

samples/windll/plugin.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
// Client: answer_dllvar derives from dllimport_tag here (no WINDLL_OWNER).
77
#include "shared.hpp"
88

9-
#ifdef _WIN32
10-
#define EXPORT __declspec(dllexport)
11-
#else
12-
#define EXPORT
13-
#endif
14-
15-
// answer() returns a reference to the same static_answer<answer_dllvar, void>::value
16-
// that main.cpp owns — one shared linker symbol, bound via dllimport.
17-
extern "C" EXPORT int get_answer() { return answer(); }
9+
// answer() returns a reference to static_answer<answer_dllvar, void>::value —
10+
// the one shared linker symbol — bound via dllimport.
11+
extern "C" __declspec(dllexport) int get_answer() { return answer(); }

samples/windll/shared.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,20 @@ struct dllimport_tag {};
1515
// A base class does not appear in a class's mangled name, so
1616
// static_answer<answer_dllvar>::value resolves to the SAME linker symbol in
1717
// all modules — only the export/import attribute differs.
18-
#ifdef _WIN32
19-
# ifdef WINDLL_OWNER
18+
#ifdef WINDLL_OWNER
2019
struct answer_dllvar : dllexport_tag {};
21-
# else
22-
struct answer_dllvar : dllimport_tag {};
23-
# endif
2420
#else
25-
struct answer_dllvar {}; // no decoration needed on ELF platforms
21+
struct answer_dllvar : dllimport_tag {};
2622
#endif
2723

28-
// Primary template: plain static variable (non-Windows and catch-all).
24+
// Primary template: plain static variable (catch-all).
2925
template<class Tag, typename = void>
3026
struct static_answer {
3127
static int value;
3228
};
3329
template<class Tag, typename Enable>
3430
int static_answer<Tag, Enable>::value;
3531

36-
#ifdef _WIN32
3732
// Specialization for dllexport: selected when Tag derives from dllexport_tag.
3833
// enable_if_t<true> evaluates to void, so the instantiation is
3934
// static_answer<answer_dllvar, void> — same mangled name as the dllimport case.
@@ -52,7 +47,6 @@ struct static_answer<Tag,
5247
std::enable_if_t<std::is_base_of_v<dllimport_tag, Tag>>> {
5348
static __declspec(dllimport) int value;
5449
};
55-
#endif
5650

5751
// Accessor: always refers to static_answer<answer_dllvar, void>::value —
5852
// the one shared linker symbol — regardless of which specialization is active.

0 commit comments

Comments
 (0)