如何使用 Visual Studio 代码编译多 cpp 文件?

How to use visual studio code to compile multi-cpp file?(如何使用 Visual Studio 代码编译多 cpp 文件?)
本文介绍了如何使用 Visual Studio 代码编译多 cpp 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照一些说明构建了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.

推荐答案

我让它工作的一种方法是进入你的构建任务,而不是说 "g++ ${file}",您可以将目标文件设置为编译为 "g++ ${fileDirname}/**.cpp",这将编译目录中的所有 .cpp 文件.

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.

这篇关于如何使用 Visual Studio 代码编译多 cpp 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)