[C++-sig] How can I have a global C/C++ variable that is shared between Python C/C++ extensions?

octalber fest octalberfest at gmail.com
Thu Jun 13 00:23:53 CEST 2013


Dear list,

Apologies if this question is slightly off-topic, but I thought I might get
answers here rather than the other Python mailing lists. I have a rather
specific issue with creating a Python C++ extension that I'm really stuck
on it. These are the details...

--

I have written a Python package almost completely in C++. The reason for
doing so is because I want to manually wrap an existing C++ library, but
that is irrelevant here.

This Python package consists of a number of different extension modules,
all of which I compile with distutils in a 'setup.py' script. These
extension modules can be interrelated, in which case I link them by passing
the shared library to the Extension constructor. To be clear, suppose I
have two Python C++ modules, A and B, where B uses functions defined in A.
These normally compile into A.so and B.so. Since B uses functions defined
in A, I compile the A module as usual then I pass ':A.so' as a library to
the libraries keyword in the Extension constructor for the B module. (The
':' lets g++ deal with the fact that the library does not start with the
usual 'lib' prefix.) This works fine for linking functions and classes.

My problem is as follows: I have defined some extern global C++ variables
in A. While doing what I have described allows B to access functions in A,
it actually seems to create a COPY of any global data defined in A. This is
a real problem for me.

It seems to me that the issue is essentially similar to having global
variables across shared libraries. The solutions to this that I have found
online do not seem to solve the problem.

Any help would be so very appreciated!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20130612/ea18bd7c/attachment.html>


More information about the Cplusplus-sig mailing list