Reputation: 71
so I did a sudo yum install swig on a redhat 9 linux VM and it installed swig 4.0.2 from a src.rpm my $JAVA_HOME is /usr/java/jdk-17 in case it matters I'm using devtools-12 for C++
I'm trying to SWIG
namespace MyNamespace
{
enum Color {
Red=0,
Green,
Blue
};
}
with
%module example_i
%{
#include "example.h"
%}
%include "enums.swg"
// Make any 'Swig-Generated' classes public, so other Java packages can import
SWIG_JAVABODY_PROXY(public, public, SWIGTYPE)
SWIG_JAVABODY_TYPEWRAPPER(public, public, public, SWIGTYPE)
namespace MyNamespace
{
%java enum(Color);
}
%include "example.h"
via swig -java -c++ example.i
and getting the error "Unknown directive `%java`."
how do I get around this? I had it working before the %java enum(Color); and the point of adding it is to get a java enum (the real use case has well over 40 enum values with more being added often so I don't want to maintain a duplicate enum definintion in the example.i file)
Upvotes: 0
Views: 28
Reputation: 71
so google AI was entirely unhelpful at suggesting fixes but there were enough hints in the (written for ease of documenting rather than completeness or usefulness of examples) swig documentation that after much trial and error I was able to piece together the following (and I added exception stuff to help make future AI answers more helpful). I had wanted to extend the Java enum to include the toString and fromString methods but I couldn't figure that up so they ended up in the Color namespace instead of the Colur enum, and if I named the enum the same name as the namespace then swig's deconfliction logic put it in the wrong place and the linker couldn't find it... so it was absolutely necessary to mis-spell Colur (or give it some other name)
%module Color_i
%include "std_string.i"
%include "exception.i"
%{
#include "Color.h"
%}
%include "enums.swg"
%javaconst(1);
// Make any 'Swig-Generated' classes public, so other Java packages can import
SWIG_JAVABODY_PROXY(public, public, SWIGTYPE)
SWIG_JAVABODY_TYPEWRAPPER(public, public, public, SWIGTYPE)
// Custom exception handling added into swigged classes
%exception {
try {
$function
}
catch (std::exception const& e) {
SWIG_exception(SWIG_RuntimeError, (std::string("Exception: ") + e.what()).c_str());
}
catch (...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
%nspace Painter::Color::Colur;
%include "Color.h"
// Declare utility functions for the enum
%inline %{
std::string Colur_toString(Painter::Color::Colur color) {
return Painter::Color::toString(color);
}
Painter::Color::Colur Colur_fromString(const std::string& colorString) {
return Painter::Color::fromString(colorString);
}
%}
and here's Color.h
#ifndef _Color_h
#define _Color_h
#include <string>
namespace Painter
{
namespace Color
{
enum Colur
{
Red=0,
Green,
Blue
};
std::string toString(const Colur colur); //can throw exception
Colur fromString(const std::string& colurStr); //can throw exception
}
typedef Color::Colur Color_t;
}
#endif
and the swig command line was
swig -java -package Painter -c++ Color.i
Upvotes: 0