ndarray/Boost.NumPy

Linking with libpython.a

Closed this issue · 2 comments

Some Python installation have a static library libpython.a that gets linked into the test executable build and run by TryLink and TryRun. But then, for embedded Python like you use in those snippets, some care is needed for the link flags, as explained in the official Python doc. Hence the following proposed patch:

diff --git a/SConscript b/SConscript
index 7d9eb74..b88d524 100644
--- a/SConscript
+++ b/SConscript
@@ -58,6 +58,10 @@ int main()
     match = re.search("(python.*)\.(a|so|dylib)", libfile)
     if match:
         context.env.AppendUnique(LIBS=[match.group(1)])
+        if match.group(2) == 'a':
+            flag = distutils.sysconfig.get_config_var('LINKFORSHARED')
+            if flag is not None:
+                context.env.AppendUnique(LDFLAGS=flag)
     flags = [f for f in " ".join(distutils.sysconfig.get_config_vars("MODLIBS", "SHLIBS")).split()
              if f != "-L"]
     context.env.MergeFlags(" ".join(flags))

ok, sorry, I pasted the a diff from the wrong window. Here is what I wanted to propose:

--- a/SConscript
+++ b/SConscript
@@ -58,6 +58,10 @@ int main()
     match = re.search("(python.*)\.(a|so|dylib)", libfile)
     if match:
         context.env.AppendUnique(LIBS=[match.group(1)])
+        if match.group(2) == 'a':
+            flags = distutils.sysconfig.get_config_var('LINKFORSHARED')
+            if flags is not None:
+                context.env.AppendUnique(LINKFLAGS=flags.split())
     flags = [f for f in " ".join(distutils.sysconfig.get_config_vars("MODLIBS", "SHLIBS")).split()
              if f != "-L"]
     context.env.MergeFlags(" ".join(flags))

Actually, my mistake made me realised another problem in the original SConscript. I think one must use LINKFLAGS, and not LDFLAGS to pass arguments to the linker. Which means the fix for the "-F" argument on MacOS X a bit after the point of my patch should not work.

Thanks! I'd previously thought that embedding simply didn't work when Python was compiled with a static library, and I just needed to rewrite these configure tests to avoid embedding. This is a much easier fix.

And you're completely right about LINKFLAGS vs LDFLAGS.

Patch and LINKFLAGS fix applied at 2a41c80