使用 CodeRunner 扩展使用 VStudio 代码运行 C++ [英] Running C++ with VStudio Code using CodeRunner extension
问题描述
我无法使用 Code Runner 扩展从 VStudio Code 运行我的 .cpp 文件.
I am unable to run my .cpp files from VStudio Code using the Code Runner extension.
当我在 main 中用 #include "test.cpp"
替换 #include "test.h"
时,它工作正常,但将其替换回来会给我以下错误;
When I replace #include "test.h"
with #include "test.cpp"
in main it works fine but replacing it back gives me the following error;
[运行] cd "c:\Users\dree\Desktop\TestRun\" &&g++ main.cpp -o main&&"c:\Users\dres\Desktop\TestRun\"mainc:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe:C:\Users\dree\AppData\Local\Temp\ccm2RSvw.o:main.cpp:(.text+0x15):对`MyClass::foo()' collect2.exe 的未定义引用:错误:ld返回 1 个退出状态
[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] 在 1.1 秒内退出,代码=1
[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
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
}
有没有办法更改 Code Runner 扩展名,以便使用正在运行的文件目录中的所有 .cpp 文件调用 g++ 命令?
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
推荐答案
通过更改 Code Runner 扩展的设置解决;
Solved by changing a setting for Code Runner extension;
将此添加到我的 settings.json 文件中;
Added this into my settings.json file;
"code-runner.executorMap": {
"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $fileNameWithoutExt.exe"
}
这篇关于使用 CodeRunner 扩展使用 VStudio 代码运行 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!