Clang unable to link files in VS Code

≡放荡痞女 提交于 2020-06-17 14:19:29

问题


I recently started learning C++, and I'm currently trying to test out the header file functionality. I wrote three simple files:

headertest.h:

#pragma once

class Cube
{
public:
  double getVolume();
  double getSurfaceArea();
  void setLength(double length);

private:
  double length_;
};

headertest.cpp:

#include "headertest.h"

double Cube::getVolume()
{
  return length_ * length_ * length_;
}

double Cube::getSurfaceArea()
{
  return 6 * length_ * length_;
}

void Cube::setLength(double length)
{
  length_ = length;
}

and main.cpp:

#include "headertest.h"
#include <iostream>

int main()
{
  Cube c;
  c.setLength(3.48);
  double volume = c.getVolume();
  std::cout << volume;
}

However, when I try to build the files with Clang, I get the following error:

> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/jonathan/Desktop/myc++/cubetest/headertest.cpp -o /Users/jonathan/Desktop/myc++/cubetest/headertest <

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

Does anyone have any idea of why this is happening? I've tried running g++ main.cpp headertest.cpp -o main before building, but the error isn't getting solved. Thank you!

来源:https://stackoverflow.com/questions/62161584/clang-unable-to-link-files-in-vs-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!