0

Although have some questions with the same title but, I believe that my problems are little different.

I have 3 files: main.cpp, car.h and car.cpp.

main.cpp:

#include <string>
#include <iostream>
#include "car.h"

int main(int argc, const char * argv[]) {
    Car v;
    std::cout << v.getName() << std::endl;
    return 0;
};

car.h

#include <string>
#include <iostream>

#ifndef CAR_H
#define CAR_H

class Car {
    public:
        std::string getName();
};

#endif // MAINWINDOW_H

car.cpp

#include "car.h"

std::string Car::getName() {
    return "blah";
};

When i run in terminal with "g++ main.cpp", i got this strange message:

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

It's a newbie question, but could anyone help me?

  • Try `g++ main.cpp car.cpp -o car`. Your result will be in the binary `car`. – Alyssa Haroldsen May 03 '17 at 01:33
  • @Kupiakos This is fantastic. Thank you very much, I spent all day in this problem without answers. Why does this problem occur, and why should I compile passing these parameters? – Londerson Araújo May 03 '17 at 01:37
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alyssa Haroldsen May 03 '17 at 01:39
  • Tank you so much! – Londerson Araújo May 03 '17 at 01:40
  • See [here](http://stackoverflow.com/q/12573816/1530134) for info on what the actual problem is. On the parameters, you need to provide both `.cpp` files to `g++` to allow them to both compile and reference each other. The `-o` flag specifies where to put the output. Otherwise, it would just write to `a.out`. – Alyssa Haroldsen May 03 '17 at 01:41
  • This is the specific answer you should read: http://stackoverflow.com/a/12574400/1530134 – Alyssa Haroldsen May 03 '17 at 01:43

0 Answers0