0

Newbie developer here. I was following this YouTube tutorial by The Cherno from 2017 and I don't know if anything has changed in the functioning of c++ since then, but the code he shows working in the video at the minute 3:55 seems to result in an error for me.

The tutorial is about learning how header files work, but he first makes an example of linking 2 .cpp files, here is the first one:

#include <iostream>

void Log(const char* message){
    std::cout<<message<<std::endl;
}

int main(){
    Log("Hello World!");
    std::cin.get();
}

Here is the second one:

void Log(const char* message);

void InitLog(){
    Log("Initializing Log");
}

And here's my error:

Lavoro@iMacdiFrancesco untitled folder % cd "/Users/programmer/Desktop/untitled folder/" && g++ Log.cpp -o Log && "/Users/programmer/Desktop/untitled folder/"Log
Undefined symbols for architecture x86_64:
  "Log(char const*)", referenced from:
      InitLog() in Log-07d6ab.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Lavoro@iMacdiFrancesco untitled folder % 

And a different version of the error I got from trying to put the files together in a folder before executing them:

Lavoro@iMacdiFrancesco untitled folder % cd "/Users/programmer/Desktop/untitled folder/" && g++ Log.cpp -o Log && "/Users/programmer/Desktop/untitled folder/"Log
Undefined symbols for architecture x86_64:
  "Log(char const*)", referenced from:
      InitLog() in Log-1d2bd8.o
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Lavoro@iMacdiFrancesco untitled folder %

I also tried doing what he does at the minute 7:00, that is using a header file, but that results in the error below, so I guess there's a problem in my setup rather than in my actual code:

Lavoro@iMacdiFrancesco untitled folder % cd "/Users/programmer/Desktop/untitled folder/" && g++ Main.cpp -o Main && "/Users/programmer/Desktop/untitled folder/"Main
Undefined symbols for architecture x86_64:
  "Log(char const*)", referenced from:
      _main in Main-86283c.o
  "InitLog()", referenced from:
      _main in Main-86283c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Lavoro@iMacdiFrancesco untitled folder % 

I double checked the code and it should be identical to his, also, for context, I'm working on a Mac with Catilina and I'm using Visual Studio Code. Thanks to whatever hero decides to undertake my monstrous code ;)

  • Are `InitLog` and `Log` defined in different files? You only compile one file now it seems. Other than that, your code seems fine [and runs fine](https://godbolt.org/z/Tqr69Y88h) – Ted Lyngmo Jul 01 '21 at 16:46
  • @Lyngmo Yes they are defined in different files as shown by the tutorial. The error shown pops out when compiling the second one. You are right saying that it runs fine as I’m sure the code shown in the tutorial is correct, maybe it’s a problem with my Visual Studio. – Francesco Derme Jul 01 '21 at 16:49
  • 2
    You need to either compile both files at the same time `g++ -o foo Main.cpp Log.cpp` or compile them into 2 object files that you later link. (I did not watch the video btw) – Ted Lyngmo Jul 01 '21 at 16:50
  • Okay thanks, may you also tell me how to do that or point to a resource that explains how to do what you suggested? – Francesco Derme Jul 01 '21 at 16:53
  • You're welcome and sure, I'll link it to a duplicate question with answers - that should have plenty of info I hope. – Ted Lyngmo Jul 01 '21 at 16:53

0 Answers0