【问题标题】:Compiling C++ in VSCode gives me an undefined reference在 VSCode 中编译 C++ 给了我一个未定义的引用
【发布时间】:2020-04-09 17:35:28
【问题描述】:

我通常使用 VS19 进行 C++ 编程,但我想试试它是否适用于我的 Macbook 上的 VSCode,所以我编写了一个真正简单的程序:

主要

#include <iostream>
#include "test.hpp"
using namespace std;

int main()
{
  testfile obj(5);
  cout << "main.cpp" << endl;
  obj.output();
  return 0;
}

类头文件(.hpp)

#pragma once
using namespace std;

class testfile
{
private:
  int i;
public:
  testfile(int in) : i(in) {}
  void output();
};

类文件(.cpp)

#include "test.hpp"
#include <iostream>
using namespace std;

void testfile::output()
{
  cout << i << endl;
}

我知道我可以在标头中编写小输出,但如果代码被拆分为许多不同的文件,我想尝试它是否有效。我收到以下错误:

(PATH)..\Temp\ccITg6NM.o:main.cpp:(.text+0x48): 未定义的引用 `testfile::output()' collect2.exe: error: ld returned 1 exit status

我的 Windows 笔记本电脑也是如此。我在 Visual Studio 上运行了完全相同的代码,它运行得非常好。我尝试用谷歌搜索错误,但tbh,我没有从中得到任何东西......

我使用 C/C++ 智能感知和编译运行插件运行 VSCode。

【问题讨论】:

  • testfile 源文件在项目中?在实际的 makefile(或使用的其他构建系统)中?你如何建造?你的构建设置是什么?您的 makefile(或其他构建系统配置文件)做什么?
  • 我还没有makefile,我使用插件编译代码。我还不知道如何制作makefile,但我想我应该研究一下。在我的 Mac 上,我尝试在终端上使用 g++,我尝试包含所有 3 个文件,但我总是遇到输出失败..

标签: c++ gcc visual-studio-code g++


【解决方案1】:

这很棘手...我不确定,但链接器可能会忽略 test.cpptestfile::output() 的实现,因为标头 test.hpp 包含构造函数 testfile::testfile(int in) 的实现。

我实际上无法重现问题,试试这个:

test.hpp

#pragma once
using namespace std;

class testfile
{
private:
  int i;
public:
  testfile(int in);
  void output();
};

test.cpp

#include "test.hpp"
#include <iostream>
using namespace std;

testfile::testfile(int in) : i(in) {}

void testfile::output()
{
  cout << i << endl;
}

我认为最好将所有实现都放在上面的 *.cpp 文件中。


编辑:

我使用 g++ 编译这些文件(对不起,我没有 VScode 环境)。

命令行(正确): g++ main.cpp test.cpp -o out

输出:

D:\workspace\test2\test2>out
main.cpp
5

命令行(不正确,缺少 test.cpp): g++ main.cpp -o 输出

输出:

${User}\AppData\Local\Temp\ccYKJ92L.o:main.cpp:(.text+0x1a): undefined reference to `testfile::testfile(int)'
${User}\AppData\Local\Temp\ccYKJ92L.o:main.cpp:(.text+0x48): undefined reference to `testfile::output()'
collect2.exe: error: ld returned 1 exit status

命令行(不正确,缺少 main.cpp): g++ test.cpp -o 输出

输出:

C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

EDIT2:

虽然我使用的是 windows,但我安装了 VSCode,但我想我知道为什么会出现这些类型的错误。

您可以使用 F6 命令构建带有 C/C++ 编译运行的源代码,但 F6 命令仅适用于当前选择并显示的文件在编辑器上。选择 main.cpp 则链接器找不到class testfile 的方法,否则选择 test.cpp 则链接器找不到项目的入口点(main)。

因此,如果您想正确构建,则必须制作某种构建脚本(makefile、json 等)。

如果你输入Ctrl+Shift+P,你可以输入Tasks: Configure task标签。单击它们并配置您的设置(主机操作系统,编译器,...)它将为您提供具有最小形式的默认 tasks.json 文件。

我使用这个 json 文件(Windows, mingw(gcc for windows))

tasks.json

{
    "version": "2.0.0",
    "command": "g++",
    // compiles and links with debugger information
    "args": ["-g", "-o", "out.exe", "main.cpp", "test.cpp"],
}

Ctrl+Shift+B(使用上面的json构建),然后输出:

running command> g++ -g -o out.exe main.cpp test.cpp

成功构建out.exe文件。

如果要调试或运行,请使用 F5Ctrl+F5 或顶部菜单栏上的“调试”选项卡,然后其输出在调试控制台上:

main.cpp
5

调试或运行步骤指的是launch.json文件,就像构建步骤指的是tasks.json文件。

供参考,launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows)build test",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/out.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }
    ]
}

Concolusionally 这不是来源的问题。 欲了解更多信息,请参阅这篇文章:

How do I set up Visual Studio Code to compile C++ code?

【讨论】:

  • 嗨,我尝试了你的方法,但我遇到了错误:“未定义对 `WinMain' collect2.exe 的引用:错误:ld 返回 1 个退出状态”。我把它展示给了一位教授,他告诉我将 testfile.cpp 包含到我的 main.cpp 中并且它起作用了。但是由于它在没有将 testfile.cpp 包含在 main 中的情况下为您工作,我可以手动将它与 makefile 链接吗?
  • 您说您尝试使用 g++ 进行编译并且遇到相同的错误,但我高度怀疑您所说的错误似乎错过了 *.cpp 文件之一。你能分享你的 g++ 命令行和错误吗?
  • 嗨,正如我所说,我使用 VSCode 中的插件编译了我的所有代码,但是在我在我的 mac 终端上尝试了你的命令后,它一切都很好,没有进一步的问题。感谢那!我从 VSCode 得到的错误如下:C:\Users\LUCACH~1\AppData\Local\Temp\ccXvErUU.o:main.cpp:(.text+0x1a): undefined reference to testfile::testfile(int)C:\Users\LUCACH~1\AppData\Local\Temp\ccXvErUU.o:main.cpp:(.text+0x26): undefined reference to testfile::output() collect2.exe: error: ld returned 1 exit status 同时在 mac 上一切正常。
  • 感谢命令行指针!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 2021-11-20
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2018-03-28
相关资源
最近更新 更多