2

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
  • 31,330
  • 6
  • 30
  • 51
elanonrigby
  • 331
  • 1
  • 4
  • 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

1 Answers1

1

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.

Opposite34
  • 11
  • 3