[C++-sig] library conflict

Matthias G. Imhof mgi at vt.edu
Thu Mar 30 23:56:54 CEST 2006


I am using the C++API to embed python modules in a c++ code. I have a strange 
problem.

The following module mkmodel works when called from interactive python, but 
fails when called from c++.

from math import *
import	sys

def replace(name, ext, num, args) :
         snum = str(num)
	
	...

	      narg = "EE"+str(n)
               arg = str(exp(args[j]))
               line = line.replace(narg, arg)

	      narg = "TT"+str(n)
               arg = str(pow(10.0, args[j]))
               line = line.replace(narg, arg)
	...

def mkmodel(num, args) :
	replace("model", "rec", num, args)
	replace("model", "grd", num, args)
	
	...

	return(0)

def check() :
	print >>sys.stderr, "mkmodel check success"


If either the math commands exp and pow, or these commands and the math import 
are removed, the c++ call works. I traced the problem with the debugger down to 
a problem with libmath.so.

The c++ routine is listed below. The call to mkmodel.check() always succeeds 
independently of the math issue. But mkmodel.mkmodel(...) fails if mkmodel 
contains anything to do with libmath.so. (The PP commands are just simple 
wrappers combining a whole lot of PyXXX calls. They are taken from the 
Programming Python book. The situation remains the same when they are replaced 
with regular PyXXX routines.)

	const char cmd1[] = "import os,sys\nsys.path.append(os.getcwd())\n";
	Py_Initialize();
	PyRun_SimpleString(cmd1);
	
	if (PP_Run_Function("mkmodel", "check", "O", NULL, "()"))
   	{  printf("Can't call mkmodel check\n");
      	   exit(0);
	}
	
	PyObject *tptr = PyTuple_New(*nd);
         for(int i=0; i<*nd; i++)
	{  PyTuple_SetItem(tptr, i, Py_BuildValue("f", model[i]));
	}
	if (PP_Run_Function("mkmodel", "mkmodel", "O", NULL, "(iO)",
			    modelcount, tptr))
   	{  printf("Can't call mkmodel\n");
      	   exit(0);
	}	


ps: I do link with -Xlinker --export-dynamic

Matthias

-- 
Matthias G.Imhof, Ph.D.
Geosciences, Derring Hall 4044 (0420), Virginia Tech, Blacksburg, VA 24061
phone: (540) 231 6004,   fax: (540) 231 3386,
email: mgi at vt.edu,   http://www.geol.vt.edu/profs/mgi

-> Support Directive 10-289, it's not just another good idea ... <-



More information about the Cplusplus-sig mailing list