SWIG - Problem with namespaces

2024/10/8 14:39:58

I'm having trouble getting the following simple example to work with SWIG 1.3.40 (and I also tried 1.3.31). The Foo structure comes through as a Python module as long as I don't wrap it in a namespace, but as soon as I do I get a compilation error in the generated test_wrap.c.

test.h:

#ifndef __TEST_H__
#define __TEST_H__#define USE_NS 1#if USE_NS
namespace ns {
#endifstruct Foo {float a;float b;float func();
};#if USE_NS
}
#endif#endif

test.cpp

#include "test.h"#if USE_NS
namespace ns {
#endiffloat Foo::func()
{return a;
}#if USE_NS
}
#endif

test.i

%module test
%{
#include "test.h"
%}%include "test.h"

I run the following commands for building a bundle on OSX 10.6.3:

swig -python test.i
g++ -c -m64 -fPIC test.cpp
g++ -c -m64 -fPIC -I/usr/local/include -I/opt/local/include -I/opt/local/Library/Frameworks/Python.framework/Headers test_wrap.c
g++ -o _test.so -bundle -flat_namespace -undefined suppress test_wrap.o test.o -L/usr/local/lib -L/opt/local/lib -lpython2.6

This works, but only if I take out the namespace. I though SWIG handled namespaces automatically in simple cases like this. What am I doing wrong?

This is the error that I get - it looks like SWIG references a 'ns' and a 'namespace' symbol which are undefined.

test_wrap.c: In function ‘int Swig_var_ns_set(PyObject*)’:
test_wrap.c:2721: error: expected primary-expression before ‘=’ token
test_wrap.c:2721: error: expected primary-expression before ‘namespace’
test_wrap.c:2721: error: expected `)' before ‘namespace’
test_wrap.c:2721: error: expected `)' before ‘;’ token
test_wrap.c: In function ‘PyObject* Swig_var_ns_get()’:
test_wrap.c:2733: error: expected primary-expression before ‘void’
test_wrap.c:2733: error: expected `)' before ‘void’
Answer

In your test.i file, add a "using namespace ns" line after the #include. Without that, your swig wrapper code won't know to look for Foo in the "ns" namespace.

https://en.xdnf.cn/q/70111.html

Related Q&A

Django access to subclasses items from abstract class

class Animal(models.Model):....class Meta:abstract = Trueclass Cat(models.Model, Animal):...class Dog(models.Model, Animal):....I want to be able to return all instances of querysets of all the subclas…

Django BinaryField retrieved as memory position?

Ive written a short unit test with the following code:my_object = MyObject() my_object.data = b12345my_object.save() saved_object = MyObject.objects.first()assert saved_object.data == my_object.datawhe…

difflib.SequenceMatcher isjunk argument not considered?

In the python difflib library, is the SequenceMatcher class behaving unexpectedly, or am I misreading what the supposed behavior is?Why does the isjunk argument seem to not make any difference in this…

PyCharm Code Folding/Outlining Generates Wrong Boundaries

Im having a very frustrating issue with PyCharm in that it does not want to properly outline the code so that blocks fold correctly. Ive looked all over the place and couldnt find any help with this pa…

How to clear the conda environment variables?

While I was setting an environment variable on a conda base env, I made an error in the path that was supposed to be assigned to the variable. I was trying to set the $PYSPARK_PYTHON env variable on th…

Create duplicates in the list

I havelist = [a, b, c, d]andnumbers = [2, 4, 3, 1]I want to get a list of the type of:new_list = [a, a, b, b, b, b, c, c, c, d]This is what I have so far:new_list=[] for i in numbers: for x in list: f…

cxfreeze aiohttp cannot import compat

Im trying to use cx_freeze to build a binary dist for an web application written in Python 3 using the aiohttp package.Basically I did:cxfreeze server.pyand got a dist outputBut when running the ./serv…

Python open() requires full path [duplicate]

This question already has answers here:open() gives FileNotFoundError / IOError: [Errno 2] No such file or directory(11 answers)Closed 9 months ago.I am writing a script to read a csv file. The csv fil…

Pandas to parquet file

I am trying to save a pandas object to parquet with the following code: LABL = datetime.now().strftime("%Y%m%d_%H%M%S") df.to_parquet("/data/TargetData_Raw_{}.parquet".format(LABL))…

Wildcard in dictionary key

Suppose I have a dictionary:rank_dict = {V*: 1, A*: 2, V: 3,A: 4}As you can see, I have added a * to the end of one V. Whereas a 3 may be the value for just V, I want another key for V1, V2, V2234432, …