i'm using C++ Audio processing library for my Swift project from https://www.surina.net/soundtouch/sourcecode.html
I have also included those cpp file in my compile sources in Projects-targets-build phases.
When i try to import all of library header file in my bridging header
#import "SoundTouch.h"
i got error when try to compile it
Unknown type of name 'namespace' in STTypes.h
'stdexcept' file not found
i'm using namespace in my header file
namespace soundtouch { ... }
i cannot use several standard library also like string
#include <stdexcept>
#include <string>
what i'm missing here?
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 likeclass
nor will it understand C++ style name mangling, even though it does its own name mangling, norexport "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 beextern "C"
to disable name mangling.You will need an alternate declaration for classes, you can use
void*
or I found an incompletestruct
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).And you'll need to define the shim function in a C++ file
Apologies if I got the C++ wrong, I haven't used it seriously since 1998