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

问题描述 投票:0回答:1

通常我使用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:错误:ld返回1退出状态

我的Windows笔记本电脑也是如此。我在Visual Studio上运行了完全相同的代码,并且运行良好。我尝试使用Google搜索错误,但是tbh,我什么都没得到...

我使用C / C ++ intellisense和编译运行插件运行VSCode。

通常我使用VS19进行C ++编程,但是我想尝试一下它是否可以在Macbook上的VSCode上工作,所以我写了我真正的简单程序主#include #include“ test.hpp” using namespace ...

c++ gcc visual-studio-code g++
1个回答
0
投票

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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.