Swift does not understand C++ even in header files. C does not have namespaces, so when the Swift compiler comes across the word namespace
it's going to think the same as the C compiler would, which is that it is the name of a variable. That's not all though. Swift also won't understand other C++ keywords like class
nor will it understand C++ style name mangling, even though it does its own name mangling, nor export "C" { ... }
.
If you have a C++ header file that you want to import into Swift, you have to make sure all the C++ stuff is hidden with #ifdef __cplusplus
just like if you are including the header in a C program. Also, all the function declarations will need to be extern "C"
to disable name mangling.
You will need an alternate declaration for classes, you can use void*
or I found an incomplete struct
type works quite well and you'll need to create C wrapper functions to call functions defined in the class. Something like the following might work (I haven't tested it).
#if defined __cplusplus
extern "C" {
#endif
#if defiend __cplusplus
class Foo
{
void bar(int c);
}
#endif
struct FooHandle;
void Foo_bar(struct FooHandle* foo, int c);
#if defined __cplusplus
}
#endif
And you'll need to define the shim function in a C++ file
#include MyHeader.h
void Foo_bar(struct FooHandle* foo, int c)
{
((Foo*) foo)->bar(c);
}
Apologies if I got the C++ wrong, I haven't used it seriously since 1998