Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you mix C and C++ code together, you can run into trouble because the two languages have different <em>linkages</em>. That is, if you were to crack open the object files for a compiled C and C++ file, you would find that the names inside of those object files were different.</p> <p>In particular, C++ compilers tend to use a technique called "name mangling" in which function names are mixed with extra information about the types of their arguments. For example, a function</p> <pre><code>char foo(int); </code></pre> <p>might actually have the internal name</p> <pre><code>char@foo@int </code></pre> <p>inside of the generated object file. The problem is that C code does not do this, so if you compiled a C file and tried to make a reference to the name <code>foo</code>, the linker wouldn't find it, because the name of the function in the generated C++ file is <code>char@foo@int</code> instead of <code>foo</code>.</p> <p>To fix this, C++ has a feature that allows you to explicitly tell the linker not to mangle the name and to make the generated code look like it was intended for a C program. To do this, you can declare C++ functions like this:</p> <pre><code>extern "C" char foo(int); </code></pre> <p>Now, the generated object file will contain the name <code>foo</code> without adornment in a way that is compatible with what the C code expects.</p> <p>To fix your problem, try adding one of these <code>extern "C"</code> declarations to your C++ source file that defines <code>reciprocal</code>.</p> <p>A note: if you have a project that mixes C and C++ code, you should <em>always</em> have the <code>main</code> function compiled as C++ code. C++ introduces extra initialization and cleanup code into its executables that are not present in C code. If you compile <code>main</code> as C code, this extra logic might not get added into the program, and so you may get inexplicable crashes at runtime.</p> <p>Hope this helps!</p>
 

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