0

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:

enter image description here

enter image description here

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

3
  • Specify swap.cpp on the command line. Commented Feb 24, 2023 at 6:18
  • Some pointers regarding things that aren’t related to your current problem, but may cause you things in the future: 1. If you want to use 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. Commented Feb 24, 2023 at 6:25
  • Thank you very much for the reminder I solved this problem and it is already working. :) Commented Feb 24, 2023 at 6:55

1 Answer 1

1

What that error means is that g++ sees that you are referencing a function named swap that takes 2 int parameters, but it doesn’t see a definition of that function in any of the files you gave it.

You need to add swap.cpp to the sources list, g++ doesn’t know that you want to include this file automatically (it sees that you want swap.h, but the fact that they’re related is just a matter of convention; Not something a compiler can know about on its own without you telling it):

g++ main.cpp swap.cpp -o main

Or alternatively (a bit wordier, but this is closer to how build tools typically do it, which you’ll come across soon in your learning):

g++ -c main.cpp -o main.o
g++ -c swap.cpp -o swap.o
g++ main.o swap.o -o main
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the reminder I solved this problem and it is already working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.