0

I wrote an extremely simple program with 3 files:

hash.cpp

#include <iostream>
#include <string>
#include "hash.h"
using namespace std;

void hashclass::test(){
 cout << "Inside the hashclass function def" << endl;
 cout << "Hello!" << endl; }

hash.h

#include <iostream> 
#include <string>
using namespace std;
#ifndef HASH_H 
#define HASH_H

class hashclass{
public:
void test();
};

#endif

main.cpp

#include <iostream>
#include <string>
#include "hash.h"
#include <cstdlib>
using namespace std;

int main(int argc, char **argv){

  hashclass hashObj;
  hashObj.test();
  return 0;}

When compiling using g++, I am getting the below error:

Undefined symbols for architecture x86_64:

"hashclass::test()", referenced from: _main in main-9f5786.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I tried a lot to figure out what went wrong, I don't see any issue with above code.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
L887
  • 312
  • 1
  • 4
  • 15
  • 1
    Plz share the compilation command of g++? try this g++ hash.cpp main.cpp` – dlmeetei Feb 06 '16 at 22:52
  • Are you compiling both .cpp files? – mathematician1975 Feb 06 '16 at 22:52
  • @mathematician1975 I was using the atom editor and the command line plugin that came with it. I just realized it doesn't compile both the .cpp files, it only compiles the one in the current window :( – L887 Feb 06 '16 at 23:00
  • Did you try this? $ g++ -Wall -o main main.cpp hash.cpp – fa11enprince Feb 06 '16 at 23:00
  • 1
    @L887 you need to compile and link **both** files. Most editors don't do that for you, unless you create a custom build system. The easiest way to do it is to compile from the command line. Of course, for larger projects, you need a `Makefile` or a `CMakeLists.txt` (if using CMake, which I recommend). – vsoftco Feb 06 '16 at 23:01

0 Answers0