5

I installed Visual Studio Code on Mac with Catalina in order to learn C++. Installed extensions C/C++, C/C++ Extension Pack, C++ Intellisense, CMake Tools and Code Runner.

To test VSCode I tried running the following code:

bye.cpp:

#include <iostream>

void tryMe(int s) {
    std::cout << "ok";
}

bye.h:

void tryMe(int s);

hello.cpp:

#include <iostream>
#include "bye.h"

int main() {
    tryMe(3);
    return 0;
}

But it doesn't run as it results on compiling error:

$ cd "/Users/x/Workspace/LearnCPP/" && g++ hello.cpp -o hello && "/Users/x/Workspace/LearnCPP/"hello
Undefined symbols for architecture x86_64:
  "tryMe(int)", referenced from:
      _main in hello-ef5e99.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I understand why the problem is happening: the compilation is not including the bye.cpp file so it doesn't recognise the function. If I compile through the Terminal using g++ hello.cpp bye.cpp -o hello it compiles good and runs as expected.

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-x64"
        }
    ],
    "version": 4

I've searched and seen some articles referring to a "task" file but couldn't understand how to implement it or from where does it come from.

user4581301
  • 33,082
  • 7
  • 33
  • 54
elanonrigby
  • 479
  • 1
  • 6
  • 14
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 273K Apr 06 '21 at 21:36
  • 2
    The compiler itself only deals with [*translation units*](https://en.wikipedia.org/wiki/Translation_unit_(programming)) which is a single source file and all its included header files. It has no knowledge of other source files, and you must explicitly build and link with all source files. – Some programmer dude Apr 06 '21 at 21:37
  • 2
    As soon as you get more than a single source file in your project, I suggest you use some kind of project or build system that handles the building for you correctly with all involved source files. [CMake](https://cmake.org) is currently quite popular. There are plenty of online tutorials and examples on how to integrate CMake and the build-files it generate into Visual Studio Code. – Some programmer dude Apr 06 '21 at 21:38
  • @Someprogrammerdude so there's no option on VSCode to compile and run multiple files on my project and I have to do it externally? I can't find a way to control the compiling arguments on VSCode. – elanonrigby Apr 06 '21 at 21:42
  • Visual Studio Code is, at its most basic level, just a plain text editor. If you want built-in project management and handling of multiple source file may I suggest hat you use a full IDE like for example Visual Studio Community? There are also other free and open-source IDE's that use MinGW if that's your wish. – Some programmer dude Apr 06 '21 at 21:46
  • Unrelated. Careful with the tags. This isn't C code and if you're planning on constructing a compiler I salute you, but this question's not about that. – user4581301 Apr 06 '21 at 21:47
  • Possible duplicate: https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files https://stackoverflow.com/questions/51720769/how-to-use-visual-studio-code-to-compile-multi-cpp-file You need to change your tasks.json file. – Jerry Jeremiah Apr 06 '21 at 21:49

3 Answers3

3

I've searched and seen some articles referring to a "task" file but couldn't understand how to implement it or from where does it come from.

I had this problem as well and found this Visual Studio Code Doc showing that in the task.json, one of the command args has to be changed from "${file}" to "${workspaceFolder}/*.cpp" in other to build all the .cpp files.

Ping34
  • 206
  • 1
  • 10
0

you can try adding your command line to tasks.json file. To be able to create tasks.json file you can type and select "tasks:Configure Default Build Task" and then create tasks.json from template. Open the tasks.json file and adapt the following lines:

"label": "The label of your program"

command: your terminal command that works

As an example:

"command": "g++",
"args":[
    "-o",
    "hello", => The executable name
    "hello.cpp",
    "bye.cpp" => all the cpp files that needs to build
]

Leave the rest of the file as it is.

akhos
  • 1
  • 2
0

Building on @Ping34 's answer, if you want this to work with any folder (relative path), not just Visual Studio Code's default workspace path, one thing needs to be tweaked.

In "tasks.json", inside "tasks": [], inside "args": [] change

"${file}"

to

"*.cpp"

instead of

"${workspaceFolder}/*.cpp"

Notes

According to Visual Studio Code documentation, "${workspaceFolder}/*.cpp" "will build all .cpp files in your current folder." This is rather misleading. It seems to build all .cpp files in your Visual Studio Code workspace folder, not necessarily the same path where the .cpp file you're currently trying to build is.

Visual Studio Code predefined variable definitions:

${workspaceFolder} - "the path of the folder opened in VS Code"

${file} - "the current opened file"

I would assume the reason why "*.cpp" works is because ${file} only opens one single file, so linking fails because linking requires multiple files, so "*.cpp" is essentially a wildcard for multiple .cpp files.

CreativiTimothy
  • 103
  • 1
  • 8