While I was doing `cinterop` for `stack` header fi...
# kotlin-native
r
While I was doing
cinterop
for
stack
header file in c++. I encountered
Copy code
Exception in thread "main" java.lang.Error: /usr/include/c++/7/bits/functexcept.h:42:1: error: unknown type name 'namespace'.
Is kotlin/native unable to determine
namespace
?
o
C++ interop is not supported
r
I saw discussion related to this. Are there any plans for this? because STL for c++ provides many data structures (vectors, stacks, queues etc). Currently I am unable use stack for linux target. Any help regarding "How could I use Stack data structure for linux target"
m
The only way is to wrap it to plain C functions.
r
I dont follow. How could wrap C++ in C . Any leads ?
m
You can have C header, included both in Kotlin interop and in separately compiled library, implemented in C++
r
Are you trying to say to do something like this
Copy code
// *.h file
 // ...
 #ifdef __cplusplus
 #define EXTERNC extern "C"
 #else
 #define EXTERNC
 #endif

 typedef void* mylibrary_mytype_t;

 EXTERNC mylibrary_mytype_t mylibrary_mytype_init();
 EXTERNC void mylibrary_mytype_destroy(mylibrary_mytype_t mytype);
 EXTERNC void mylibrary_mytype_doit(mylibrary_mytype_t self, int param);

 #undef EXTERNC
 // ...


 // *.cpp file
 mylibrary_mytype_t mylibrary_mytype_init() {
   return new MyType;
 }

 void mylibrary_mytype_destroy(mylibrary_mytype_t untyped_ptr) {
    MyType* typed_ptr = static_cast<MyType*>(untyped_ptr);
    delete typed_ptr;
 }

 void mylibrary_mytype_doit(mylibrary_mytype_t untyped_self, int param) {
    MyType* typed_self = static_cast<MyType*>(untyped_self);
    typed_self->doIt(param);
 }
m
Sure, something like this should work.
r
I still have one query . This is the example for my custom header file and using extern , refer the C++ implementation But here I am using
stl_stack.h
and its throwing exception for
namespace
. So do I have to create
my own stl_stack.h
& using extern refer the C++ implementation ?
m
Hm, why not just define in C header
struct stl_stack;
- then it can be used for function parameters as pointer - and implement it in C++ part?
r
Header files are declarations . Implementation of header files are given by .c or .cpp files . So
original stl_stack.h
is also only declaration and some
stl_stack.cpp
is it's implementation (AFAIU). So I will create my own
stl_stack.h
and use that
stl_stack.cpp
@msink I meant this for C++ part
Copy code
//.... *.h file
 #ifdef __cplusplus
 #define EXTERNC extern "C"
 #else
 #define EXTERNC
 #endif 
 
 typedef struct stl_stack;
 typdef void* T
 
 EXTERNC void push(T item);
 EXTERNC T pop();
 EXTERNC int size();
 
 // ......  stl_stack.cpp
 
 #include<stack>
 using namespace std;
 template <typename T>
 stack<T> s;
 
 void push(T item){
    s.push(item);
 }
 
 T pop(){
    s.pop()
 }
m
If you want to use this from Kotlin - why do you want this implemented in C++, why not use normal Kotlin containers? Anyway generics are not supported in C.
r
I just wanted to use c++ Implemented Stack
m
Sorry, but I personally doubt that it is good idea. Better write it in pure Kotlin.
r
I was playing with C-interop . Wanted to know the limitations. In final library. I would use pure Kotlin only.