问题
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
回答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.
回答2:
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"
}
来源:https://stackoverflow.com/questions/57502306/running-c-with-vstudio-code-using-coderunner-extension