4

I am unable to run my .cpp files from VStudio Code using the Code Runner extension.

When I replace #include "test.h" with #include "test.cpp" in main it works fine but replacing it back gives me the following error;

[Running] cd "c:\Users\dree\Desktop\TestRun\" && g++ main.cpp -o main && "c:\Users\dres\Desktop\TestRun\"main c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\dree\AppData\Local\Temp\ccm2RSvw.o:main.cpp:(.text+0x15): undefined reference to `MyClass::foo()' collect2.exe: error: ld returned 1 exit status

[Done] exited with code=1 in 1.1 seconds

Main.cpp

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

int main()
{
    MyClass a;
    a.foo();
    return 0;
}

test.cpp

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

void MyClass::foo()
{
    std::cout << "Hello World" << std:: endl;
}

test.h

class MyClass
{
public:
    void foo();
    int bar;
};

settings.json

{
    "workbench.colorTheme": "Visual Studio Dark",
    "workbench.iconTheme": "material-icon-theme",
    "python.linting.flake8Enabled": false,
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
    "editor.minimap.enabled": false,
    "liveServer.settings.donotShowInfoMsg": true,
    "python.pythonPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_86\\python.exe",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "explorer.confirmDelete": false


}

Is there a way to change the Code Runner extension to call the g++ command with all of the .cpp files in the directory of the file being ran?

For example: It is running the following command;

cd "c:\Users\drees\Desktop\TestRun\" && g++ main.cpp -o main && "c:\Users\drees\Desktop\TestRun\"main

Can I somehow change it to this?

cd "c:\Users\drees\Desktop\TestRun\" && g++ *.cpp -o main && "c:\Users\drees\Desktop\TestRun\"main
dree
  • 170
  • 1
  • 11
  • 3
    Looks like the bug is right here: ***cd "c:\Users\dree\Desktop\TestRun\" && g++ main.cpp -o main &&*** Notice it's not building `test.cpp` you probably have to show your `json` files to get help. – drescherjm Aug 14 '19 at 21:29
  • @drescherjm Do you mean settings.json? – dree Aug 14 '19 at 21:33
  • @dree No it means you have to add the file test.cpp to your VS Code project. VS Code (like any other IDE) has to be told which files to compile, it doesn't work it out for itself. – john Aug 14 '19 at 21:43
  • @john I know that, but is there a way to fix the Code Runner extension to run all .cpp files in the directory of the file being ran? `"g++ *.cpp -o $fileNameWithoutExt && ./$fileNameWithoutExt.exe"` – dree Aug 14 '19 at 21:51

4 Answers4

3

Solved by changing a setting for Code Runner extension;

Added this into my settings.json file;

"code-runner.executorMap": {
        "cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $fileNameWithoutExt.exe"
    }
dree
  • 170
  • 1
  • 11
2

This is how I set my code-runner for C++

code-runner.executorMap": {
    "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt && rm ./$fileNameWithoutExt",
}

This command tells code-runner to

  1. Change to directory where my present source code is
  2. Compile code
  3. Execute binary file
  4. Removed binary file
1

As pointed out by drescherjm, the problem is in the arguments passed to the compiler: the output shows that the command that was used is g++ main.cpp -o main. There's no reference to test.cpp!

You probably think that telling g++ to compile main.cpp is enough, because it includes test.h and obviously the definition of whatever is in test.h can be found in test.cpp. Unfortunately, it is obvious to you but not to the compiler. The fact that files with the same name but different extension (like test.cpp and test.h) are related is very convenient for us programmers, but the compiler doesn't know it. All it knows is that test.h declares a class called MyClass, but it has no clue that its definition is inside test.cpp. To solve it, you must call g++ like this:

g++ main.cpp test.cpp -o main

That is, you must explicitly list all the source files that must be used to build main.

That's why replacing #include "test.h" with #include "test.cpp" works: because this way you are telling the compiler to take the content of test.cpp and paste it at the top of main.cpp, so that the definition of MyClass is available. But this is a code smell: if you are including a .cpp file, you can be pretty sure you are doing something wrong. The compiler doesn't care about the extension, .h or .cpp is the same to it, that's why it works. But you should avoid it.

  • Is there a way to change this for the Code Runner extension to include all cpp files? As an example `"g++ *.cpp -o $fileNameWithoutExt && ./$fileNameWithoutExt.exe"`. There is a setting for code-runner called executorMapByFileExtension, but no idea how to use it. – dree Aug 14 '19 at 21:48
1

Just wanted to post the thing that helped me. I tried dree's answer, but kept getting errors

Edit the code-runner executor map settings, but use this:

"cpp": "cd $dir && g++ -o $fileNameWithoutExt *.cpp && $dir$fileNameWithoutExt",