0

I'm learning C++ language on VSCode with Mac, and when I build my project, it throw an error as following:

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

The project has three files, as follows:

hello.cpp

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

int main(){
    cout << "hello" << endl;
    sum(1, 1);
    return 0;
}

sum.h

#include <iostream>
using namespace std;

void sum(int a,  int b);

sum.cpp

#include "sum.h"

void sum(int a, int b)
{
    cout << a+b << endl;
}

Does any else has idea to solve it? Thanks very much!

  • You are not compiling `sum.cpp`. Add it to the list of `.cpp` files that should be compiled, wherever you configured that. It should not say only `hello.cpp`, but `hello.cpp sum.cpp`. – user17732522 May 27 '23 at 09:50
  • Did you edit your tasks.json like the documentation tells to have VSCode build all files instead of just the active file? Some people while reading the documentation skip over this section which explains that by default VSCode builds only the active file and how to fix it to build all files in the folder: [https://code.visualstudio.com/docs/cpp/config-clang-mac#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-clang-mac#_modifying-tasksjson) – drescherjm May 27 '23 at 15:28

0 Answers0