Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the maximum reliability:</p> <ul> <li>Compile the C source with a C compiler.</li> <li>Compile the C++ source with a C++ compiler</li> <li>Preferably, write the main() function in C++.</li> <li>Link the program with a C++ compiler.</li> </ul> <p>Make sure that the C headers are either themselves aware of C++ or that the C++ code includes the C headers inside an <code>extern "C" { ... }</code> block.</p> <p>Either (C header file <code>cheader.h</code>):</p> <pre><code>#ifndef CHEADER_H_INCLUDED #define CHEADER_H_INCLUDED #ifdef __cplusplus extern "C" { #endif ...main contents of header... #ifdef __cplusplus } #endif #endif /* CHEADER_H_INCLUDED */ </code></pre> <p>or (C++ source code):</p> <pre><code>extern "C" { #include "cheader.h" } </code></pre> <p>Modern C style is very close to the common subset of the C and C++ languages. However, arbitrary C code is not C++ code for any of a very large number of reasons, and simply calling the C source files C++ source files (by changing the extension, or simply by compiling with the C++ compiler) is not guaranteed to be successful. In general, it is easier to compile C as C and C++ as C++ and then link the resulting object files with the C++ compiler (to ensure the correct support libraries are invoked).</p> <p>However, if the MSVC compiler is saying that programs using MFC have to be written solely in C++ (<em>MFC requires C++ compilation (use a .cpp suffix)</em> is the reported error), then you may have no choice but to ensure that your C code is compilable as C++ code. That means you'll have to cast the return values from <code>malloc()</code> et al; you have to worry about other places where you do not use a cast to convert a <code>void *</code> into some other pointer type; you have to worry about <code>sizeof('a') == 4</code> in C and <code>sizeof('a') == 1</code> in C++; you have to ensure that every function is declared before it is used; you have to ensure your C code does not use any C++ keywords (<code>typename</code>, <code>class</code> in particular; also <code>inline</code> sometimes — but the complete list is quite large).</p> <p>In some circles, you'd have to worry about the use of features in C99 that are not in C++2003 or C++2011, such as flexible array members, designated initializers, compound literals, variable-length arrays, and so on. However, if the C code is for MSVC, then that probably isn't going to be a problem; those features are not supported by the MSVC C compiler (it only supports C89, not C99).</p> <p>FWIW: I have a script to hunt down C++ keywords. It contains the following comment:</p> <pre><code># http://en.cppreference.com/w/cpp/keywords # plus JL annotations # and C (&lt;iso646.h&gt;) # and_eq C (&lt;iso646.h&gt;) # alignas (C++11 feature) # alignof (C++11 feature) # asm C (core) # auto(1) C (core) # bitand C (&lt;iso646.h&gt;) # bitor C (&lt;iso646.h&gt;) # bool C99 (&lt;stdbool.h&gt;) # break C (core) # case C (core) # catch # char C (core) # char16_t (C++11 feature) # char32_t (C++11 feature) # class # compl C (&lt;iso646.h&gt;) # const C (core) # constexpr (C++11 feature) # const_cast # continue C (core) # decltype (C++11 feature) # default(1) C (core) # delete(1) # double C (core) # dynamic_cast # else C (core) # enum C (core) # explicit # export # extern C (core) # false C99 (&lt;stdbool.h&gt;) # float C (core) # for C (core) # friend # goto C (core) # if C (core) # inline C (core) # int C (core) # long C (core) # mutable # namespace # new # noexcept (C++11 feature) # not C (&lt;iso646.h&gt;) # not_eq C (&lt;iso646.h&gt;) # nullptr (C++11 feature) # operator # or C (&lt;iso646.h&gt;) # or_eq C (&lt;iso646.h&gt;) # private # protected # public # register C (core) # reinterpret_cast # return C (core) # short C (core) # signed C (core) # sizeof C (core) # static C (core) # static_assert (C++11 feature) # static_cast # struct C (core) # switch C (core) # template # this # thread_local (C++11 feature) # throw # true C99 (&lt;stdbool.h&gt;) # try # typedef C (core) # typeid # typename # union C (core) # unsigned C (core) # using(1) # virtual # void C (core) # volatile C (core) # wchar_t C (core) # while C (core) # xor C (&lt;iso646.h&gt;) # xor_eq C (&lt;iso646.h&gt;) </code></pre> <p>The <code>(1)</code> suffixes is a footnote at CPP Reference:</p> <ul> <li><code>(1)</code> — meaning changed in C++11 </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload