1

I keep getting this error when I try compiling my code and I cannot seem to understand why.

Undefined symbols for architecture x86_64:
  "Location::Location()", referenced from:
      _main in main-64d6fd.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 use "g++ *main.cpp" to try and compile it, but I get the same error no matter what I do.

#include <iostream>
#include "location-proj.h"
using namespace std;
int main() {

    Location loc;
    return 0;
}
#include <iostream>

using namespace std;

class Location {
public:
    Location();

private:
    int row, col;
    unsigned int nextDirection; //Uses ENUM as input
};
#include "location-proj.h"
Location::Location() {
    this->row = 0;
    this->col = 0;
    this->nextDirection = 0;
}
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • What happened when you used "-v to see invocation"? – Scott Hunter Jun 21 '19 at 00:08
  • you have to compile and link all of your files, so in this case you should compile with g++ main.cpp location.cpp – R4444 Jun 21 '19 at 00:11
  • You are probably only compiling `main.cpp`, you have to give `g++` the full list of the `.cpp` files otherwise it won't know where it is supposed to find implementation for `Location::Location()`. – Havenard Jun 21 '19 at 00:13
  • Ex: `g++ -o program main.cpp location-proj.cpp` – Havenard Jun 21 '19 at 00:14
  • 1
    Thank you so much!! the "g++ main.cpp location.cpp" worked for me – billythegoat Jun 21 '19 at 00:17
  • Look into using Cmake, it helps wrap all the logic of linking header and cpp files together without needing to spam the command line. – Alex Hodges Jun 21 '19 at 00:20
  • 1
    One thing I really I like about this question is either the asker is coding methodically and making incremental changes between tests or they have provided a very decent Minimal example. Either one is commendable. – user4581301 Jun 21 '19 at 00:22

0 Answers0