如何使用Visual Studio代码编译多cpp文件? [英] How to use visual studio code to compile multi-cpp file?
问题描述
我已经按照一些说明来构建Visual Studio代码C/C ++编译和调试环境.但是g ++编译器只能编译所选的cpp文件,因此无法编译与cpp文件关联的.h文件.然后终端显示体系结构x86_64的未定义符号"错误.代码如下:
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();
a.cpp文件
include <iostream>
include "a.h"
using namespace std;
int func(){
return 111;
}
main.cpp文件
include "a.h"
using namespace std;
int main()
{
int b = func();
cout << b << endl;
}
Visual Studio代码将使用以下命令
Visual studio code will use the command as below
g++ directory/main.cpp -o directory/main.out -g -Wall -fcolor- diagnostics -std=c++11
此命令将引发体系结构x86_64的未定义符号"错误 我可以使用此新命令来修复它
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.
所以问题是如何配置这些json文件来解决g ++编译问题.而且,当我想使用FFMpeg之类的库时,如何正确链接FFMpeg .h文件.
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.
推荐答案
对于非常简单的项目,您可以简单地通过单个命令将多个cpp文件传递给编译器,例如:
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
您可以简单地将task.json中的编译命令更改为该值.
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:
- Make-Linux上的主要标准构建系统,但学习和学习都很困难
- CMake-Visual Studio代码对cmake有所支持
- Gyp-可以生成制作文件
- Scons-类似python的构建脚本
这篇关于如何使用Visual Studio代码编译多cpp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!