@@ -2,6 +2,53 @@ if(OGS_BUILD_WHEEL)
22 return ()
33endif ()
44
5+ # CMake function to handle pybind11 version-compatible linker flags
6+ # This prevents the linker from stripping the module initialization function
7+ function (dont_strip_pybind11_init_function target_name module_name)
8+ # Only apply when building static libraries to prevent symbol stripping
9+ if (BUILD_SHARED_LIBS )
10+ return ()
11+ endif ()
12+
13+ set (pybind11_init_function "" )
14+
15+ # Detect pybind11 version
16+ if (DEFINED pybind11_VERSION)
17+ if (pybind11_VERSION VERSION_GREATER_EQUAL "3.0" )
18+ set (pybind11_init_function "pybind11_init_${module_name} " )
19+ else ()
20+ set (pybind11_init_function "pybind11_init_impl_${module_name} " )
21+ endif ()
22+ elseif (DEFINED PYBIND11_VERSION_MAJOR)
23+ if (PYBIND11_VERSION_MAJOR GREATER_EQUAL 3)
24+ set (pybind11_init_function "pybind11_init_${module_name} " )
25+ else ()
26+ set (pybind11_init_function "pybind11_init_impl_${module_name} " )
27+ endif ()
28+ endif ()
29+
30+ message (STATUS "Using pybind11 init function: ${pybind11_init_function} " )
31+
32+ # Linker flags to prevent symbol stripping
33+ if (COMPILER_IS_GCC OR COMPILER_IS_CLANG)
34+ if (APPLE )
35+ target_link_options (${target_name} PRIVATE
36+ "LINKER:-u,_${pybind11_init_function} " )
37+ else ()
38+ target_link_options (${target_name} PRIVATE
39+ "LINKER:--undefined=${pybind11_init_function} " )
40+ endif ()
41+ elseif (COMPILER_IS_MSVC)
42+ target_link_options (${target_name} PRIVATE
43+ "/INCLUDE:${pybind11_init_function} " )
44+ else ()
45+ # Fallback for unsupported compilers
46+ message (WARNING "Unsupported compiler, manual linker configuration may be needed" )
47+ endif ()
48+
49+ message (STATUS "Applied linker flags to prevent stripping of ${pybind11_init_function} " )
50+ endfunction ()
51+
552# Troubleshooting: If you get linker errors, such as ogs.cpp:(.text+0xb4):
653# undefined reference to `_Py_ZeroStruct' it could be that OGS is compiled with
754# the wrong Python version. I (Ch. Leh.) observed the following: The symbol
@@ -43,13 +90,10 @@ target_link_libraries(
4390target_compile_definitions (
4491 ogs_embedded_python
4592 PUBLIC OGS_EMBED_PYTHON_INTERPRETER
46- # Add macro definition, because static libs make special handling
47- # necessary s.t. the embedded OpenGeoSys Python module won't be
48- # removed by the linker.
49- $<$<BOOL :${BUILD_SHARED_LIBS} >:PRIVATE
50- OGS_BUILD_SHARED_LIBS>
5193)
5294
95+ dont_strip_pybind11_init_function(ogs_embedded_python "OpenGeoSys" )
96+
5397ogs_add_executable(ogs ogs.cpp CommandLineArgumentParser.cpp)
5498
5599target_link_libraries (
0 commit comments