Dave
Dave

Reputation: 8090

Swig given "unknown namespace" errors on namespace aliasing

I have a header file like

#include <MyUtils.h> // defines namespace MyUtils, and MyUtils::Math

namespace mum=MyUtils::Math;

class LocalClass{

    public:
         void eat( const mum::array& arr);
};

I have the usual %{ #include %}, %include structure in my .i file.

When I run swig I get:

Error: Unknown namespace 'MyUtils::Math'

How/why doesn't SWIG know about namespace aliasing? Is there a work around other than using #ifndef SWIG preprocessor macros? (c.f. this discussion (I need to keep my c++ code independent of swig)

SWIG Version 2.0.4

Upvotes: 1

Views: 1103

Answers (2)

Jens Munk
Jens Munk

Reputation: 4725

Suppose to have a header

// MyUtils.h
namespace MyUtils {
    namespace Math {
        typedef int SomeType;
        class array {
            //
        };
    }
}

And another header

// MyHeader.h
#include <MyUtils.h>
namespace mum=MyUtils::Math;

class LocalClass {

    public:
      void eat( const mum::array& arr);
};

If you SWIG file looks like

// MySwigInterfaceFile.i
%module MySwigModule
#include "MyHeader.h

%include "MyHeader.h"

You run into trouble, SWIG will generate code that cannot compile for a number of reason

1) You need to include all headers, SWIG cannot recurse header, i.e. you must include MyUtils.h in your MySwigInterfaceFile.i before MyHeader.h, the same applies for the inclusion using the %include directive

2) You must write using namespace MyUtils::Math; following the inclusions using #include

3) To make SWIG aware of any typedefs inside nested namespaces, you must write

namespace MyUtils {
    namespace Math {
        %typedef int SomeType;
    }
}

before the inclusions using %include

I recommend programmers to start out with many small projects to get hands-on experience with SWIG.

Upvotes: 2

Oliver
Oliver

Reputation: 29571

SWIG does appear to support namespace aliasing. I think the problem is likely in MyUtils.h; maybe there are pre-processor defines that you have to set in order for it to use the namespaces. You can define those on the SWIG command line.

Upvotes: 0

Related Questions