0

I'm relatively new to coding in C++ and have started working with main and header files and I've created a program to test it out, however, the following program results in the following compiler error:

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

This is my current code.

test.cpp

#include <iostream>
#include "test.h"
using namespace std;

int main() {
    Print();
    
    return 0;
}

test.h


#ifndef TEST_H
#define TEST_H

void Print();

#endif

test1.cpp

#include <iostream>
#include "test.h"
using namespace std;

void Print() {
    cout << "Hello" << endl;
}

Nothing I've found online has helped me and my only assumptions are that my compiler isn't set up correctly. I've tried compiling both of these of files by typing in "g++ test.cpp test1.cpp", as well, but yields similar results. I would like to note that I am on Mac as well. Please feel free to leave any comments or suggestions for how I've asked this question, this is my first time on stack overflow.

user4581301
  • 33,082
  • 7
  • 33
  • 54

1 Answers1

0

Your code seems just fine, I’ll will try to compile it on vscode myself. One problem could be your vscode launch.json. I have had the same problem as what I am describing. Vscode can be quite “Finicky” with low level languages like c and c++. In your launch file (or tasks file if it calls something in tasks) make sure you compile *.cpp files in the folder. I would look something like {folder}/**.cpp. To find the exact command look on vscode’s official site.

Given that you tried compiling your code outside of vscode and assuming that everything is in the same folder, it might be a coding problem. Again, your code looks fine. Go online and find some learning to code website with multiple file coding. Copy their code and do the same thing that you did with your code. If it doesn’t work there is definitely a problem with how to code is being compiled. Otherwise revise your code and compare to find the problem.

Also, could you provide the terminal command vscode prints (I am also on Mac). It will be in the terminal tab in the g++ section, not in bash)

P.S. I am also new to stack overflow so any suggestions you have for me will be great.

  • If you're not providing an answer, please refrain for using answers for other purposes. – sweenish Apr 23 '21 at 23:30
  • But that is an answer right? How is it not? – Asubramanian08 Apr 25 '21 at 01:48
  • You are unsure about every aspect of what you've typed ("seems", "could", don't know which file you mean between launch.json and tasks.json, telling them to do research instead of providing a method, request to provide more information). – sweenish Apr 25 '21 at 12:45