@@ -191,9 +191,14 @@ set_target_properties(core_objects PROPERTIES
191191)
192192
193193target_include_directories (core_objects PUBLIC ${PYBIND11_INCLUDE_DIRS} )
194- # core_objects contributes to both static and shared variants; compile with
195- # export annotations enabled where definitions are emitted.
196- target_compile_definitions (core_objects PRIVATE BUILDING_NODE_LIBRARY )
194+ # On MSVC when extension modules consume core_static, avoid __declspec
195+ # import/export decoration entirely to prevent unresolved __imp stubs.
196+ if (MSVC )
197+ target_compile_definitions (core_objects PRIVATE NODE_EXPORT= )
198+ else ()
199+ # Non-Windows builds keep explicit export annotations.
200+ target_compile_definitions (core_objects PRIVATE BUILDING_NODE_LIBRARY )
201+ endif ()
197202# Keep pybind11 usage requirements attached to the object target so core
198203# sources that include <pybind11/...> (e.g. numpy.h) always compile.
199204target_link_libraries (core_objects PUBLIC
@@ -214,6 +219,12 @@ target_link_libraries(core_static PUBLIC
214219 ${NUMPY_LIBRARIES}
215220)
216221
222+ if (MSVC )
223+ # Consumers of core_static (core/_cgns/tests modules) must also compile
224+ # with neutral NODE_EXPORT to avoid dllimport decoration.
225+ target_compile_definitions (core_static INTERFACE NODE_EXPORT= )
226+ endif ()
227+
217228add_library (core_shared SHARED $<TARGET_OBJECTS :core_objects >)
218229set_target_properties (core_shared PROPERTIES
219230 CXX_VISIBILITY_PRESET default
@@ -231,17 +242,6 @@ target_link_libraries(core_shared PUBLIC
231242 ${NUMPY_LIBRARIES}
232243)
233244
234- # Linking strategy by platform:
235- # - MSVC: link extension modules against core_shared to avoid static-link
236- # import/export decoration mismatches.
237- # - Other platforms: keep static linkage to preserve previous Linux/macOS
238- # behavior that avoids unresolved symbols from split modules.
239- if (MSVC )
240- set (CORE_LINK_TARGET core_shared)
241- else ()
242- set (CORE_LINK_TARGET core_static)
243- endif ()
244-
245245if (APPLE )
246246 set_target_properties (core_shared PROPERTIES
247247 INSTALL_RPATH "@loader_path"
@@ -307,15 +307,15 @@ if(ENABLE_MPI)
307307endif ()
308308
309309pybind11_add_module (core MODULE src/c++/core_pybind.cpp )
310- target_link_libraries (core PRIVATE ${CORE_LINK_TARGET} )
311- add_dependencies (core ${CORE_LINK_TARGET} )
310+ target_link_libraries (core PRIVATE core_static )
311+ add_dependencies (core core_static )
312312if (APPLE )
313313 target_link_options (core PRIVATE "-Wl,-rpath,@loader_path" )
314314endif ()
315315
316316pybind11_add_module (_cgns MODULE src/c++/cgns_pybind.cpp )
317- target_link_libraries (_cgns PRIVATE ${CORE_LINK_TARGET} )
318- add_dependencies (_cgns ${CORE_LINK_TARGET} )
317+ target_link_libraries (_cgns PRIVATE core_static )
318+ add_dependencies (_cgns core_static )
319319if (APPLE )
320320target_link_options (_cgns PRIVATE "-Wl,-rpath,@loader_path" )
321321endif ()
@@ -335,9 +335,9 @@ install(DIRECTORY include DESTINATION ${SKBUILD_PROJECT_NAME})
335335# if(ENABLE_TESTS)
336336# file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
337337# pybind11_add_module(tests MODULE ${TEST_FILES})
338- # target_link_libraries(tests PRIVATE ${CORE_LINK_TARGET} )
338+ # target_link_libraries(tests PRIVATE core_static )
339339# target_include_directories(tests PRIVATE ${CMAKE_INSTALL_INCLUDEDIR} ${CMAKE_SOURCE_DIR}/tests)
340- # add_dependencies(tests ${CORE_LINK_TARGET} )
340+ # add_dependencies(tests core_static )
341341# if(APPLE)
342342# target_link_options(tests PRIVATE "-Wl,-rpath,@loader_path")
343343# endif()
@@ -359,12 +359,12 @@ if(ENABLE_TESTS)
359359 CXX_VISIBILITY_PRESET default
360360 VISIBILITY_INLINES_HIDDEN OFF
361361 )
362- target_link_libraries (tests PRIVATE ${CORE_LINK_TARGET} )
362+ target_link_libraries (tests PRIVATE core_static )
363363 target_include_directories (tests PRIVATE
364364 ${CMAKE_INSTALL_INCLUDEDIR}
365365 ${CMAKE_SOURCE_DIR} /tests
366366 )
367- add_dependencies (tests ${CORE_LINK_TARGET} )
367+ add_dependencies (tests core_static )
368368
369369 if (APPLE )
370370 target_link_options (tests PRIVATE "-Wl,-rpath,@loader_path" )
0 commit comments