Skip to content

Commit c8fc858

Browse files
committed
Merge branch 'Pybind11Version3NamingFix' into 'master'
Tell linker to keep pybind11_init_xxx symbol See merge request ogs/ogs!5401
2 parents e1a2f9c + 65ae566 commit c8fc858

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

Applications/CLI/CMakeLists.txt

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,53 @@ if(OGS_BUILD_WHEEL)
22
return()
33
endif()
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(
4390
target_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+
5397
ogs_add_executable(ogs ogs.cpp CommandLineArgumentParser.cpp)
5498

5599
target_link_libraries(

Applications/CLI/ogs_embedded_python.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,10 @@ PYBIND11_EMBEDDED_MODULE(OpenGeoSys, m)
4141
)");
4242
}
4343

44-
#ifndef OGS_BUILD_SHARED_LIBS
45-
46-
// Hackish trick that hopefully ensures that the linker won't strip the symbol
47-
// pointed to by p from the library being built.
48-
template <typename T>
49-
void mark_used(T p)
50-
{
51-
volatile T vp = p;
52-
vp = vp;
53-
}
54-
55-
#endif // OGS_BUILD_SHARED_LIBS
56-
5744
namespace ApplicationsLib
5845
{
5946
pybind11::scoped_interpreter setupEmbeddedPython()
6047
{
61-
#ifndef OGS_BUILD_SHARED_LIBS
62-
// pybind11_init_impl_OpenGeoSys is the function initializing the embedded
63-
// OpenGeoSys Python module. The name is generated by pybind11. If it is not
64-
// obvious that this symbol is actually used, the linker might remove it
65-
// under certain circumstances.
66-
mark_used(&pybind11_init_impl_OpenGeoSys);
67-
#endif
68-
6948
// Allows ogs to be interrupted by SIGINT, which otherwise is handled by
7049
// python. See
7150
// https://docs.python.org/3/c-api/exceptions.html#c.PyErr_CheckSignals and

0 commit comments

Comments
 (0)