I have followed some instructions to construct Visual studio code C/C++ compile and debug environment.But g++ compiler can only compile the selected cpp file, so the included .h file associated the cpp file can not compiled. then the terminal shows 'Undefined symbols for architecture x86_64' error. the code as below:
int func();
include <iostream>
include "a.h"
using namespace std;
int func(){
return 111;
}
include "a.h"
using namespace std;
int main()
{
int b = func();
cout << b << endl;
}
Visual studio code will use the command as below
g++ directory/main.cpp -o directory/main.out -g -Wall -fcolor- diagnostics -std=c++11
this command will raise 'Undefined symbols for architecture x86_64' error I can fix it with this new command
g++ main.cpp a.cpp -o main.out.
So the problem is how to config these json files to fix the g++ compile issue. And when I want to use some libraries such as FFMpeg, how can I link the FFMpeg .h file correctly.
You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}/*.cpp" instead of ${file} .This will build all .cpp files in your current folder.
VS Code is first and foremost an editor, and relies on command-line tools to do much of the development workflow. The C/C++ extension does not include a C++ compiler or debugger. You will need to install these tools or use those already installed on your computer.
Launch VS Code and press the “Ctrl” and “P” keys simultaneously to search for a file to open in the current project. Type in the file name. To open the new file in a temporary tab, click on it once. To open the new file in a separate window that you can choose to close manually, double-click it.
Visual C++ For creating more code files to go into a project, use the "Add New Item" under the "Project" menu to add new C++ code files. An executable can consist of many files, but can have only one main() function!
For very simple projects you can simply pass multiple cpp files to the compiler in a single command, e.g:
g++ main.cpp a.cpp -o main.out
You can simply change your compile command in tasks.json to this value.
However as your project grows you will find this approach causes you more and more problems. I'd recommend you look into a proper build system, there are many to choose from including:
One way I have gotten it to work is by going into your build task, and instead of saying "g++ ${file}"
, instead you can set the target file to get compiled as "g++ ${fileDirname}/**.cpp"
which will compile all the .cpp files in the directory.
This way you can use the same build task for a project where you may have multiple programs in different folders, all under the same umbrella directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With