Using visual studio code to write CPP how to correctly reference the functions of other files

after configuring the CPP development environment with Visual studio code, you want to reference other files in the main function, but always report Undefined symbols for architecture x86Secret64: error

under Mac. The

code is as follows:
a.h file

int func();

a.cpp file

-sharpinclude <iostream>
-sharpinclude "a.h"
using namespace std;
int func(){
    return 0;
}

main.cpp file

-sharpinclude <iostream>

-sharpinclude "a.h"

using namespace std;
int main()
{
    int b = func();
    cout << b << endl;
}

Press F5 to start debugging output:

> Executing task: gPP /Users/stanhu/Desktop/Git/Foundation/CPP_Learn/main.cpp -o /Users/stanhu/Desktop/Git/Foundation/CPP_Learn/main.out -g -Wall -fcolor-diagnostics -std=cPP11 <

Undefined symbols for architecture x86_64:
  "func()", referenced from:
      _main in main-d53c96.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 terminal process terminated with exit code: 1

how to solve this problem. In addition, how to properly reference the h file of a third party?

Apr.05,2021

because gPP will not compile other CPP files correctly, you can see the VSCode command:
gPP / Users/stanhu/Desktop/Git/Foundation/CPP_Learn/main.cpp-o / Users/stanhu/Desktop/Git/Foundation/CPP_Learn/main.out-g-Wall-fcolor-diagnostics-std=cPP11
compiles only the main.cpp file but not the a.cpp file. So there will be a symbols for architecture x864th error
, so the right thing to do is: gPP main.cpp a.cpp-o main.out
and then execute. / main.out
is no problem, so now the problem comes out, so how can VSCode compile all linked CPP files correctly.


to link cpp files correctly, you need to specify header files and third-party dependent libraries through the -L , -I options.

recommended reading article: https://blog.csdn.net/sunshin.


add xx.out to program in your c configuration lauch.json file

"program": "${workspaceFolder} / ${fileBasenameNoExtension} / XXX.out".

Menu