5

Can someone explain to me why when I include header files in my code, it is not running in VS Code? (OS: Windows 10)

enter image description here

If I run this code:

#include <iostream>

int main () {

    std::cout << "Hello from C++ " << std::endl;
}

the VS Code is working fine, but if I run this code:

#include <iostream>
#include "./lib/Methods.h"

int main () {

    int a = MyMethod(5);
    std::cout << "Hello from C++ " << std::endl;
    std::cout << "a = " << a << std::endl;
}

I get this error:

PS C:\Users\giorg\Documents\Development\Tests\node-addons-test\src> g++ main.cpp
C:\Users\giorg\AppData\Local\Temp\ccIQQEE1.o:main.cpp:(.text+0x1e): undefined reference to `MyMethod(int)' collect2.exe: error: ld returned 1 exit status PS
C:\Users\giorg\Documents\Development\Tests\node-addons-test\src>

lib/Methods.cpp

int MyMethod(int x) {
    return x * 2;
}

lib/Methods.h

int MyMethod(int x);

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "/src/lib"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17134.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}
Student
  • 805
  • 1
  • 8
  • 11
George C.
  • 6,574
  • 12
  • 55
  • 80
  • if `#include "lib/Methods.h"` works I'd just go with that. – user4581301 Jul 27 '18 at 18:38
  • I recommend adding the commandline VSCode generates and runs to the question. – user4581301 Jul 27 '18 at 18:38
  • @user4581301 yes now the intelisense works but again I get the same error if I run `g++ main.cpp` – George C. Jul 27 '18 at 18:41
  • `undefined reference to `MyMethod(int)'` – George C. Jul 27 '18 at 18:42
  • If you're on Windows and you're writing C++, why not just go for the actual VS, the IntelliSense there is much better than what is currently offered in Code. – Qubit Jul 27 '18 at 18:43
  • Yes I know I have this installed in my PC but I am trying to make node.js add-ons. I have see 10 million downloads of cpp for vscode is so hard to make a simple cpp project that works on VSCODE thanks for your help mate – George C. Jul 27 '18 at 18:45
  • `undefined reference to MyMethod(int)'` is a linker error. The headers are all included and main.cpp compiles, but the linker has no idea where to find the Methods.o that has to go with Methods.h. This is where my general-purpose C++ knowledge runs out. I have no clue how to tell VS Code to compile and link in Method.cpp. Deity-of-choice willing that will be documented somewhere. – user4581301 Jul 27 '18 at 19:03
  • Yes I fixed, I just found is linker error – George C. Jul 27 '18 at 19:03

1 Answers1

6

Finally, I found is a linker error

I just flatten my project files structure to all be in the same root as the main.cpp

and I just run this command g++ *.cpp

George C.
  • 6,574
  • 12
  • 55
  • 80