I am a beginner in C++, I encountered a big problem when learning, I don't know if it is a problem with my editor or my computer, there is always an error, I look for a lot of answers, just can't solve it, please help me, thank you very much
I'm using Monterey version 12.6, the MacOS operating system
I use VSCode as my development tool My tool has extensions for C++ Here is my code:
// main.cpp
#include <iostream>
using namespace std;
#include "swap.h"
int main()
{
int a = 10;
int b = 20;
swap(a, b);
return 0;
}
// swap.h
#include <iostream>
using namespace std;
void swap(int a, int b);
// swap.cpp
#include "swap.h"
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
When I run this program, there is an error in the console Undefined symbols for architecture x86_64: "swap(int, int)", referenced from: _main in main-7515ff.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Attached is my screenshot:
I searched for a lot of answers, and it took me a long time, but I still couldn't get the answer. Please help me. Thank you very much


swap.cppon the command line.using namespace std;, do it after any#includes, otherwise it can lead to some very interesting bugs. When coming across link/compile errors, always include how you’re building your code in your question. And finally, even though compilers and what not aren’t flawless, they’re used by millions of people every day. 99.99% of the time, if there’s an issue, it’s something the programmer is doing wrong, not the compiler.