title: Initialisation of C++ structures from C
body: <p>I'm trying to use opencv <code>C++</code> library with another lib in <code>C</code>. So I've defined a structure in an <code>.hpp</code> file which I've added to the <code>.h</code> of the <code>C</code> library</p>
  
  <pre><code>typedef struct {
    cv::FileStorage fs;
    cv::Mat mat;
  } myCPPStruct;
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  typedef struct {    
      myCPPStruct * mycppstruct;    
  } myCStruct;
  
  void initialize_myCPPStruct(myCPPStruct * mycppstruct);
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>In the <code>.c</code> files the initializer for myCStruct calls <code>initialize_myCPPStruct</code> which is defined in a <code>.cpp</code> file somewhat as:</p>
  
  <pre><code>void initialize_myCPPStruct(myCPPStruct * mycppstruct){
    mycppstruct = {};
    mycppstruct-&gt;fs = cv::FileStorage( "file.txt", cv::FileStorage::READ );
    mycppstruct-&gt;mat = cv::Mat(3,3,CV_8UC3);
  }
  </code></pre>
  
  <p>But once it tries to allocate a value to <code>mycppstruct-&gt;fs</code>, it gets a segmentation fault. I assume this is because there is no memory allocation in C, but I've tried</p>
  
  <p><code>std::memset(stitcher-&gt;fs,0,sizeof(cv::FileStorage));</code></p>
  
  <p>which also doesn't work as it can't parse <code>cv:FileStorage</code> as <code>void *</code>.</p>
  
  <p>Will I have to use the deprecated C OpenCV library to make this work?</p>
  
  <p>EDIT - More details about the compilation.</p>
  
  <p>For the <code>C</code> lib, I recompile (without linking ie with the <code>-c</code> option)all the <code>.c</code>  files where I've added <code>C++</code> functions or structures using <code>g++</code> while making sure I add the <code>__cplusplus</code> guards in the <code>.h</code> files. All the <code>.c</code> files without <code>C++</code> code already have <code>.o</code> files compiled with <code>gcc</code>. I then compile the whole program with <code>g++</code> while making sure to include the relevant library files. There are no compiler errors.</p>