Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running C++ with VStudio Code using CodeRunner extension

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
like image 665
dree Avatar asked Oct 19 '25 00:10

dree


1 Answers

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"
    }
like image 155
dree Avatar answered Oct 20 '25 15:10

dree