简体   繁体   English

在 VSCode 中编译 C++ 给了我一个未定义的引用

[英]Compiling C++ in VSCode gives me an undefined reference

Normally I use VS19 for C++ programming, but I wanted to try if it works on VSCode on my Macbook, so I wrote a real simple program:通常我使用 VS19 进行 C++ 编程,但我想尝试一下它是否适用于我的 Macbook 上的 VSCode,所以我写了一个真正简单的程序:

main主要的

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

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

class header (.hpp)类标题 (.hpp)

#pragma once
using namespace std;

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

class file (.cpp)类文件 (.cpp)

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

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

I know I could write the little output in the header, but I want to try if it works if the code is split up into many different files.我知道我可以在标头中编写小输出,但是我想尝试将代码拆分为许多不同的文件是否有效。 I get the following error:我收到以下错误:

(PATH)..\\Temp\\ccITg6NM.o:main.cpp:(.text+0x48): undefined reference to `testfile::output()' collect2.exe: error: ld returned 1 exit status (PATH)..\\Temp\\ccITg6NM.o:main.cpp:(.text+0x48):未定义对`testfile::output()' collect2.exe 的引用:错误:ld 返回 1 个退出状态

The same goes for my windows laptop.我的 Windows 笔记本电脑也是如此。 I ran the exact same code on Visual Studio and it worked perfectly fine.我在 Visual Studio 上运行了完全相同的代码,它运行得非常好。 I tried googling the error but tbh, I didn't got anything out of it...我尝试在谷歌上搜索错误,但是 tbh,我没有得到任何结果......

I run VSCode with C/C++ intellisense and the compile & run plugin.我使用 C/C++ 智能感知和编译和运行插件运行 VSCode。

It's pretty tricky... I'm not sure but the linker maybe ignore the implementation of testfile::output() in test.cpp because the header test.hpp include implementation of constructor testfile::testfile(int in) .这很棘手......我不确定但链接器可能会忽略test.cpptestfile::output()的实现,因为头文件test.hpp包括构造函数testfile::testfile(int in)

I actually cannot reproduce the problem, try this :我实际上无法重现这个问题,试试这个:

test.hpp测试文件

#pragma once
using namespace std;

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

test.cpp测试.cpp

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

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

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

I think it is better that all implementation are in *.cpp file like above.我认为所有实现都在 *.cpp 文件中更好。


EDIT :编辑 :

I use g++ for compiling those files(I'm sorry I don't have VScode environment).我使用 g++ 来编译这些文件(对不起,我没有 VScode 环境)。

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

output :输出 :

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

command line(incorrect, test.cpp is missing) : g++ main.cpp -o out命令行(不正确,缺少 test.cpp): g++ main.cpp -o out

output :输出 :

${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

command line(incorrect, main.cpp is missing) : g++ test.cpp -o out命令行(不正确,缺少 main.cpp): g++ test.cpp -o out

output :输出 :

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 :编辑2:

I installed VSCode although I use windows, I think I figured out why these type of error occur.虽然我使用 Windows,但我安装了 VSCode,我想我知道为什么会发生这些类型的错误。

You might use F6 command for build the sources w/ C/C++ Compile Run, but the F6 command is applied only for a file which is currently selected and showed on editor.您可以使用F6命令来构建带有 C/C++ 编译运行的源代码,但F6命令仅适用于当前选择并显示在编辑器上的文件。 You select main.cpp then linker cannot find method of class testfile , otherwise select test.cpp then linker cannot find entry point(main) of project.您选择 main.cpp 然后链接器找不到class testfile方法,否则选择 test.cpp 然后链接器找不到项目的入口点(main)。

So if you want to build correctly, you must make kind of build script(makefile, json, something).因此,如果您想正确构建,则必须制作某种构建脚本(makefile、json 等)。

If you type Ctrl + Shift + P , you can fild Tasks: Configure task tab.如果您输入Ctrl + Shift + P ,您可以找到Tasks: Configure task选项卡。 Click them and configure your setting(host OS, compiler, ...) It would gives you default tasks.json file with minimal form.单击它们并配置您的设置(主机操作系统,编译器,...)它会以最小的形式为您提供默认的tasks.json文件。

I use this json file(Windows, mingw(gcc for windows))我使用这个 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 (build using above json), then its output: Ctrl + Shift + B (使用上面的 json 构建),然后输出:

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

It builds out.exe file successfully.它成功构建了out.exe文件。

If you want to debug or run, use F5 or Ctrl + F5 or Debug tab on the top menu bar, then its output on debug console:如果要调试或运行,请使用F5Ctrl + F5或顶部菜单栏上的 Debug 选项卡,然后在调试控制台上输出:

main.cpp
5

Debug or run step refers to launch.json file like build step refers to tasks.json file.调试或运行步骤指的是launch.json 文件,就像构建步骤指的是tasks.json 文件。

for reference, launch.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 it's not problem of source. Concolusionally 这不是来源的问题。 For further infomation refer to this post :有关更多信息,请参阅此帖子:

How do I set up Visual Studio Code to compile C++ code? 如何设置 Visual Studio Code 来编译 C++ 代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM