title: Undefined reference to 'print(int)' [Linker error]
body: <p>Assignment says: Write a program that consists of two source-files. The first (Main.c) contains the main() function and gives the variable i a value. The second source-file (Print.c) multiplies i by 2 and prints it. Print.c contains the function print() which can be called from main().</p>
  
  <p>In my attempt to do this assignment, I created three files:
  main.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "print.h"
  using namespace std;
  
  // Ex 1-5-3
  
  // Global variable
  int i = 2;
  
  
  int main() {
      print(i);
  
      return 0;
  }
  </code></pre>
  
  <p>print.cpp:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "print.h"
  using namespace std;
  
  // Ex 1-5-3
  
  // Fetch global variable from main.cpp
  extern int i;
  
  void print(int i) {
      printf("%d", 2*i);
  }
  </code></pre>
  
  <p>print.h:</p>
  
  <pre><code>#ifndef GLOBAL_H // head guards
  #define GLOBAL_H
  
  void print(int i);
  
  #endif
  </code></pre>
  
  <p>I compiled print.cpp and when I tried to compile and run main.cpp, it says:
  [Linker error] undefined reference to 'print(int)'</p>
  
  <p>Why isn't it accepting my definition of void print (int i) in print.cpp and referring to it through the header print.h?  Thanks!</p>