-4
// main.cpp
#include <iostream>
#include "add.h"

int main(){
  std::cout << "Hello World\n";
  std::cout << add(3,4) << std::endl;
  return 0;
}

// add.h
#ifndef add_h
#define add_h

int add(int x, int y);

#endif /* add_h */

// add.cpp
#include "add.h"

int add(int x, int y){
  return x + y;
}

I am compiling with 'g++ -std=c++11 main.cpp -o main'. I keep getting linker errors. I copied it exactly from a tutorial too.

Ron B
  • 5
  • 1

1 Answers1

1

Right now the add.cpp file is not properly linked.

What you need to do:

  1. Create an add.o file by

    g++ -c add.cpp -o add.o
    
  2. Link the .o file to compile

    g++ -std=c++11 add.o main.cpp -o main
    
Pang
  • 9,564
  • 146
  • 81
  • 122
James Maa
  • 1,764
  • 10
  • 20