【发布时间】:2019-08-14 21:27:56
【问题描述】:
我无法使用 Code Runner 扩展从 VStudio Code 运行我的 .cpp 文件。
当我在 main 中将 #include "test.h" 替换为 #include "test.cpp" 时,它可以正常工作,但将其替换回来会出现以下错误;
[运行] 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): 未定义对“MyClass::foo()”collect2.exe 的引用:错误:ld 返回 1 个退出状态
[Done] 在 1.1 秒内以 code=1 退出
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;
}
测试.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
}
有没有办法更改 Code Runner 扩展以调用 g++ 命令,其中包含正在运行的文件目录中的所有 .cpp 文件?
例如: 它正在运行以下命令;
cd "c:\Users\drees\Desktop\TestRun\" && g++ main.cpp -o main && "c:\Users\drees\Desktop\TestRun\"main
我可以把它改成这样吗?
cd "c:\Users\drees\Desktop\TestRun\" && g++ *.cpp -o main && "c:\Users\drees\Desktop\TestRun\"main
【问题讨论】:
-
错误似乎就在这里:cd "c:\Users\dree\Desktop\TestRun\" && g++ main.cpp -o main && 请注意,它不是在构建
test.cpp,您可能必须显示您的json文件才能获得帮助。 -
@drescherjm 你的意思是 settings.json 吗?
-
@dree 不,这意味着您必须将文件 test.cpp 添加到您的 VS Code 项目中。 VS Code(与任何其他 IDE 一样)必须被告知要编译哪些文件,它不能自己解决。
-
@john 我知道,但是有没有办法修复 Code Runner 扩展以运行正在运行的文件目录中的所有 .cpp 文件?
"g++ *.cpp -o $fileNameWithoutExt && ./$fileNameWithoutExt.exe"