【问题标题】:Running C++ with VStudio Code using CodeRunner extension使用 CodeRunner 扩展运行带有 VStudio 代码的 C++
【发布时间】: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 &amp;&amp; ./$fileNameWithoutExt.exe"

标签: c++ visual-studio-code


【解决方案1】:

通过更改 Code Runner 扩展的设置解决;

将此添加到我的 settings.json 文件中;

"code-runner.executorMap": {
        "cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $fileNameWithoutExt.exe"
    }

【讨论】:

    【解决方案2】:

    正如drescherjm 所指出的,问题出在传递给编译器的参数上:输出显示使用的命令是g++ main.cpp -o main。没有引用 test.cpp!

    你可能认为告诉 g++ 编译 main.cpp 就足够了,因为它包含了 test.h 并且 显然 test.h 中的任何内容的定义都可以在 test.cpp 中找到。不幸的是,这对你来说是显而易见的,但对编译器来说却不是。同名但扩展名不同的文件(如test.cpp和test.h)相关的事实对我们程序员来说非常方便,但编译器不知道这一点。它只知道 test.h 声明了一个名为 MyClass 的类,但它不知道它的定义在 test.cpp 中。要解决它,您必须像这样调用 g++:

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

    也就是说,您必须明确列出所有必须用于构建 main 的源文件。

    这就是为什么用#include "test.cpp" 替换#include "test.h" 有效的原因:因为这样你告诉编译器获取test.cpp 的内容并将其粘贴到main.cpp 的顶部,以便MyClass 的定义可用.但这是代码异味:如果您包含 .cpp 文件,您可以确定自己做错了什么。编译器不关心扩展名,.h 或 .cpp 与它相同,这就是它起作用的原因。但是你应该避免它。

    【讨论】:

    • 有没有办法让 Code Runner 扩展包含所有 cpp 文件?例如"g++ *.cpp -o $fileNameWithoutExt &amp;&amp; ./$fileNameWithoutExt.exe"。有一个名为 executorMapByFileExtension 的代码运行器设置,但不知道如何使用它。
    【解决方案3】:

    只是想发布对我有帮助的东西。我尝试了dree的答案,但一直出错

    编辑代码运行器执行器映射设置,但使用这个:

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

    【讨论】:

      【解决方案4】:

      这就是我为 C++ 设置代码运行器的方式

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

      这个命令告诉代码运行器

      1. 切换到我当前源代码所在的目录
      2. 编译代码
      3. 执行二进制文件
      4. 已删除二进制文件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多