10

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:

the a.h file

int func();

the a.cpp file

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

int func(){
    return 111;
}

the main.cpp file

#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 an '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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Hu Gforce
  • 371
  • 1
  • 2
  • 9

4 Answers4

11

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.

Michael Ross
  • 111
  • 1
  • 2
8

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:

  • Make - the main standard build system on Linux but difficult to learn and fiddly
  • CMake - visual studio code has some support for cmake
  • Gyp - can generate make files
  • Scons - python like build scripts
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    So should I change IDE to develop ffmpeg related project? it looks like vs code is not appropriate. – Hu Gforce Aug 08 '18 at 02:16
  • 1
    Nothing wrong with visual studio, just with building files individually, you can call any of the above build systems from visual studio – Alan Birtles Aug 08 '18 at 06:01
  • I would also mention [meson](https://mesonbuild.com/index.html) build system - a more recent alternative to cmake. It works rather well with vscode, e.g. [here](https://github.com/microsoft/vscode-cpptools/issues/4919#issuecomment-583819686) one can check how to set up debugger breakpoints. – Maverick Sep 20 '21 at 22:39
8

In your VS Code workspace directory, there will be .vscode directory under which there will be tasks.json which is for Building tasks configuration. There in args change "${file}" to "*.cpp"

This will compile all the .cpp files under the directory of your opened file in VS code editor.

Basically command parameter and it's arg is creating a command for compile a file and generating it's executable. Notice that.

enter image description here

I found this video very helpful in explaining all such details: https://www.youtube.com/watch?v=H76uhpnDZUk

Om Sao
  • 7,064
  • 2
  • 47
  • 61
5

This is what worked for me on MAC on the VSCode tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ - Build and debug active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${fileDirname}/**.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Raghav
  • 8,772
  • 6
  • 82
  • 106