0

I am very new at c++ programming and programming in general. Recently we've been adding header files and other source files (implementation) to our programs in class. I've tried writing the simplest of programs to make sure I understand the basics for including multiple files into one program but they will not compile, giving me linker errors.

namely:

Undefined symbols for architecture x86_64:
  "FooBar::printSomething()", referenced from:
      _main in main-d52d70.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I compile in the terminal with "g++ main.cpp implementation.cpp", everything works just fine.

my code:

main.cpp

#include "header.h"

int main()
{
    FooBar object;
    object.printSomething();
    return 0;
}

header.h

#ifndef _Header_h_
#define _Header_h_

#include <iostream>

using namespace std;

class FooBar{
    public:
        void printSomething();

    private:
        string helloWorld;
};

#endif

implementation.cpp

#include "header.h"

void FooBar::printSomething(){
    helloWorld = "Hello World!\n";
    cout << helloWorld;
}

I love CodeRunner but this is really frustrating me.

Thank you!

itsDave
  • 1
  • 2

1 Answers1

0

For anyone with a similar question I have found the solution:

I erred in making the header file and implementation file of the class in the header file different in name.

header.h should be named foobar.h

implementation.cpp should be named foobar.cpp

when I changed the names of the files to foobar.h and foobar.cpp the program compiled and ran just fine in CodeRunner.

My professor told me that the header file for a class and its implementation file should have the same name with different filetypes, namely x.cpp and x.h.

itsDave
  • 1
  • 2