0
votes

I am learning C++. I have tried linking separate files in an attempt to have a class in a seperate file.

When I build and run the program I get this error message:

Undefined symbols for architecture x86_64: "Sally::printCrap()", referenced from: _main in Main-8bfa94.o "Sally::Sally()", referenced from: _main in Main-8bfa94.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) [Finished in 0.3s with exit code 1]

Here is my code:

Main.cpp

#include <iostream>
#include <string>

#include "Sally.h"

using namespace std;

int main()
{
    Sally sallyObject;
    Sally *sallyPointer = &sallyObject;

    sallyObject.printCrap();
    sallyPointer->printCrap();
}

Sally.h

#ifndef SALLY_H
#define SALLY_H

class Sally
{
public:
    Sally();
    void printCrap();
};

#endif

Sally.cpp

#include "Sally.h"
#include <iostream>

using namespace std;

Sally::Sally()
{

}

void Sally::printCrap(){
    cout << "Crap \n";
};

Any help would be greatly appreciated!

1
How do you compile the program? Can you post the command you use? My guess is that you don't compile Sally.cpp (and therefore you don't link it). Try g++ main.cpp a.cppvsoftco

1 Answers

3
votes

Just launch the Terminal application of OS X, then type

cd "/Users/BEASTMACHIENEjr/Desktop/C++ Files" to go to that folder, then type g++ Main.cpp Sally.cpp. This will produce a file called a.out, which you can then launch by typing ./a.out

PS: if you use Sublime Text, I recommend creating a Makefile so you can compile code spanned across multiple source files.