0

So I want to use string class and multiset template of C++.

My original code interfaces from Python to C using ctypes and now I am trying to interface C to C++. (If there is any direct interfacing between Python & C++, I would welcome the suggestion but scanning through a couple of Stack Overflow and Stack Exchange, I found this method to be more feasible. @n.m in comments provided solution to this, but I would still like to know if it is possible to interface C & C++ for educational purpose)

So here is a simple example to reproduce what happens when I try to merge with string class...

ex.c

#include <stdio.h>
#include <stdlib.h>

void cppstr(char *);

void str(char *x) { 
    cppstr(x);                    
}

int main() {
    char *x;
    x = (char *)malloc(sizeof(char)*1024);
    str(x);
    return 0;
}

Above is my c file.

ex.cpp

#include <string>
#include <cstring>
using namespace std;
extern "C" void cppstr(char *s) {
   string x = "HELLO" + "WORLD";
   strcpy(s, x.c_str());
}

Above is cpp File.

If I compile using gcc or g++, it doesn't compile and instead gives linker errors related to string.

 compiler ex.c ex.cpp

What is a solution for string and multiset both?

  • 3
    "If there is any direct interfacing between Python & C++" there very much is more than one. Try boost.python or pybind11. It makes no sense to go through C. – n. m. could be an AI Apr 02 '18 at 09:05
  • Thanks @n.m. I will look into boost library, But I would still keep the question open to know if this possible. –  Apr 02 '18 at 09:07
  • Look up "name mangling" or "name decoration". [Here, for example](https://stackoverflow.com/questions/754865/view-compiler-mangled-names-in-c) – Mike Tyukanov Apr 03 '18 at 11:16

0 Answers0