Note that there are some explanatory texts on larger screens.

plurals
  1. POLinking error in simple C program using a C++ header
    text
    copied!<p>I am following an intro tutorial <a href="http://www.linuxinsight.com/files/alp/alp-ch01-getting-started.pdf" rel="nofollow">http://www.linuxinsight.com/files/alp/alp-ch01-getting-started.pdf</a>.</p> <p>I have created the source files main.c, reciprocal.cpp and reciprocal.hpp. I have been able to successfully compile these files. The problem comes when I go to link them and I receive the following error message:</p> <pre><code>main.o: In function `main': main.c:(.text+0x30): undefined reference to `reciprocal' collect2: ld returned 1 exit status </code></pre> <p>I gather that this is somehow not using the header files correctly, however I do not really know what to try as I've followed the steps quite thoroughly.</p> <p>If anyone has an idea why its throwing that error I would appreciate it very much.</p> <p>Thanks</p> <p>**UPDATE Here is the code for the three source files:</p> <p>main.c</p> <pre><code>#include &lt;stdio.h&gt; #include "reciprocal.hpp" int main (int argc, char **argv) { int i; i = atoi(argv[1]); printf("The reciprocal of %d is %g\n", i, reciprocal(i)); return 0; } </code></pre> <p>reciprocal.cpp</p> <pre><code>#include &lt;cassert&gt; #include "reciprocal.hpp" double reciprocal(int i) { assert(i != 0); return 1.0/i; } </code></pre> <p>reciprocal.hpp</p> <pre><code>#ifdef __cplusplus extern "C" { #endif double reciprocal(int i); #ifdef __cplusplus } #endif </code></pre> <p>I have actually modified reciprocal.hpp since originally posting. The only change I've made was to remove the extern keyword from the function signature. It previously read </p> <pre><code>extern double reciprocal(int i); </code></pre> <p>This change allowed me to link the program and I can now run it. I think what may have been occurring is that the second extern was overriding the first one. If anyone has any insight on this, I would be interested in knowing.</p> <p>Thanks for your assistance everyone.</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