title: Combining large C and C++ programs
body: <p>I have read about several methods to combine C and C++ codes, however, I'm still confused on how to proceed in my case. Here is my problem:</p>
  
  <p>I have a relatively large amount of C code (consisting of various <code>.c</code> and <code>.h</code> files) which is used to model solids in finite and discrete elements. This code has relatively short and simple main function with a <code>for</code> loop in which various other functions (from other files) are called sequentially. This code works fine when compiled in both Unix (icc compiler) and Visual Studio.</p>
  
  <p>I have other code in C++ that solves molecular dynamics interactions. This code also consists of various files and runs fine in both Unix (icpc compiler) and VS. Both are stand alone programs with their own input and output set of files.</p>
  
  <p><strong>What I need to do</strong> is to run both programs in such way that my C program "calls" the C++ code in its main loop. Some information needs to be passed both ways between the two codes, which may be in the form of arrays (or pointers). </p>
  
  <p><strong>What is the simplest way to do this?</strong></p>
  
  <p>In particular, I have multiple questions based on recommendations I have read:</p>
  
  <ol>
  <li>Should I wrap my C header files with <code>extern "C" {}</code>?</li>
  <li>Should I use <code>extern "C"</code> in my C functions?</li>
  <li>Or should I use <code>extern "C"</code> in my C++ files? (headers? functions? all of them? or only those I need to call from the C program?)</li>
  <li>In understand I can not have two <code>main</code> functions. Can I simply rename my C++ <code>main</code> function?</li>
  <li>When compiling in unix, should I use both C (icc) and C++ (icpc) compilers for different files? or just the C++ compiler?</li>
  <li>Could it be an option (to simplify things) to convert my <code>main</code> function from C to C++?</li>
  <li>If I don't need to pass information of classes between the two programs, do I need to do anything about them?</li>
  <li>In what order do you suggest this problem to be tackled? (e.g. first have my C program compiled by C++ compiler; second, compile both codes together with no links; third, link the codes; fourth, rename <code>main</code> in C++ and have it "called" by my C code; fifth, implement the transfer of information?)</li>
  <li>Finally, there are some macros in each program, which are repeated (same name, same implementation). Is there a conflict with this? Should I only keep one set of macros?</li>
  </ol>
  
  <p>Sorry for the long text and multiple questions. I am relatively new to C and even newer to C++, so even my vocabulary on these programs is limited.</p>
  
  <p>Thanks for the help. Any hints will be appreciated. If you need additional information, please let me know.</p>
  
  <p><strong>Here is the 'main' function of my C code:</strong></p>
  
  <pre><code>#include "Yproto.h"
  void show_time_info(YDC ydc,CHR Ystage[3]);
  
  main(argc, argv)
    INT argc; char **argv;
  { CHR c1name[300];         /* name of the problem i.e. input file */
    struct YD_struct yd;     /* Y database                          */
    YDC ydc=&amp;(yd.ydc);       /* Y control database                  */
    YDE yde=&amp;(yd.yde);       /* Y element database                  */
    YDI ydi=&amp;(yd.ydi);       /* Y interaction database              */
    YDN ydn=&amp;(yd.ydn);       /* Y node database                     */
    YDB ydb=&amp;(yd.ydb);       /* Y borehole database                 */
    YDS yds=&amp;(yd.yds);       /* Y source (inter. fluid) database    */
    YDO ydo=&amp;(yd.ydo);       /* Y output database                   */
    YDPE ydpe=&amp;(yd.ydpe);    /* Y property database  for elements   */
    YDPN ydpn=&amp;(yd.ydpn);    /* Y property database  for nodes (BC) */
    YDPJ ydpj=&amp;(yd.ydpj);    /* Y property database  for joints     */
    YDPM ydpm=&amp;(yd.ydpm);    /* Y property database  for meshing    */
    INT Tctrlc, itimes=0;
    CHR *p=NULL;
  
    /* get name of the problem */
    if(argv[1]!=NULL)
    { CHRcpy(c1name,argv[1]);
    }
    else
    { CHRwcr(stdout);
      CHRw(stdout,"  please define input file names: "); CHRwcr(stdout);
      CHRw(stdout," &gt;");
      fgets(c1name,sizeof(c1name),stdin);
      if((p=strrchr(c1name,'\n'))!=NULL) *p = '\0';
    }
    strcpy(ydc-&gt;cfiname, c1name);   ydc-&gt;cfiname[255]='\0';
    ydc-&gt;finp=FILENULL; ydc-&gt;fcheck=FILENULL;
  
    /* Process while any input */
    while(Yrd(c1name,&amp;yd)&gt;0)
    { itimes=itimes+1;
      CHRw(stdout,"NEW INPUT: "); CHRw(stdout, c1name); CHRwcr(stdout);
      if(Ycheck(&amp;yd)&lt;0) break; date_and_time(ydc-&gt;cruntime); timestamp();
      CHRw(stdout, "Start calculating ...\n");
      omp_set_num_threads(8);
      for(ydc-&gt;ncstep=ydc-&gt;ncstep;ydc-&gt;ncstep&lt;ydc-&gt;mcstep;ydc-&gt;ncstep++)
      { show_time_info(ydc,"Ymd");                      /* show time information    */
        Ymd(ydc,yde,ydi,ydn,ydpe,ydpn,ydpm);            /* mesh elements            */
  
        /********** HERE IS WHERE I WOULD LIKE TO CALL MY C++ PROGRAM ***************/
  
        Yfd(ydc,yde,ydn,ydi,ydo,ydpe,ydpn,ydpj);        /* nodal forces             */
        Ybor(ydc,yde,ydn,ydb,yds,ydpe,ydpj,ydpn);       /* borholes, inter. fluid   */
        Ycd(ydc,yde,ydi,ydn,ydpe,ydpn);                 /* contact detection        */
        Yid(ydc,yde,ydi,ydn,ydo,ydpe,ydpn, ydpj,ydpm);  /* interaction              */
        Yod(c1name,&amp;yd);                                /* output results           */
        Ysd(ydc,yde,ydn,ydo,ydpe,ydpn );                /* solve equations          */
        Yfrd(ydc,yde,ydi,ydn,ydpe,ydpn,ydpj,ydpm);      /* fracture                 */
        ydc-&gt;dctime=ydc-&gt;dctime+ydc-&gt;dcstec;            /* update time              */
        /* CTRL-C Interruption */
        Tctrlc = enablc(ydc-&gt;dctime, ydc-&gt;ncstep, ydc-&gt;mcstep);
        if(Tctrlc!=1) break;
      }
    }
  
    /* Termination */
    CHRw(stderr,"   ***** Y HAS ORDERLY FINISHED *****");  CHRwcr(stderr);
    CHRw(stderr,"Press a key to continue");  CHRwcr(stderr);
    getchar();
  }
  </code></pre>
  
  <p><strong>UPDATE 24 HOURS AFTER ANSWERS</strong></p>
  
  <p>I followed the recommendations as per provided answers and the solution to my problem turned out to be much simpler than originally thought (although I did have to explore several options before getting it working). The best part is that it works in both Unix and Visual Studio. Here is the summary of steps I took:</p>
  
  <ol>
  <li><p>Convert my main C file to C++. For this, rename the file containing the <code>main</code> function of my C code with a .cpp extension (changed from Y.c to Y.cpp) and change the beginning of the <code>main</code> function from:</p>
  
  <pre><code>main(argc, argv)
    INT argc; char **argv;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>int main(int argc,char **argv)
  </code></pre>
  
  <p>in order to make it C++ 'friendly'. (Note: I understand renaming the file to .cpp is not essential, but I think it's better to do it for clarity).</p></li>
  <li><p>Wrap all my C header files with</p>
  
  <pre><code>#ifdef __cplusplus
  extern "C" {
  #endif
  </code></pre>
  
  <p>at the beginning, and</p>
  
  <pre><code>#ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>at the end.</p></li>
  <li><p>Change the name of my <code>main</code> C++ function and (temporarily) use no arguments. I named it <code>int Ynano()</code>.</p></li>
  <li><p>Create a new header file called Y_NANO.h (Y_NANO.cpp is the name of the file containing the originally main C++ function) with the line:</p>
  
  <pre><code>int Ynano();
  </code></pre></li>
  <li><p>Include the new header in both Y.cpp and Y_NANO.cpp:</p>
  
  <pre><code>#include "Y_NANO.h"
  </code></pre></li>
  <li><p>Call the function <code>Ynano()</code> from the <code>main</code> function in Y.cpp.</p></li>
  <li><p>To compile in Visual Studio, simply put all the source files in the same folder and create a new project. In Unix, I followed the steps given <a href="http://www.cae.tntech.edu/help/programming/mixed_languages" rel="nofollow">here</a>.</p></li>
  </ol>
  
  <p>These steps will only make the programs run together, with no transfer of information between them. To transfer information between the programs, it is necessary to include some parameters as arguments of <code>Ynano()</code>, but that's another story. </p>
  
  <p>Some final comments:</p>
  
  <ul>
  <li>The problem of having repeated macros in different header files does not seem to be a real problem, as long as no file includes both headers (I didn't need to do anything about this).</li>
  <li>Thanks to all who provided answers. They were really helpful. The chosen answer was selected on the basis of completeness, but others were just as good. I hope the thread helps others to do their job, as many other threads have helped me to do the same.</li>
  </ul>