0

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!

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • The bug is probably in your `tasks.json` I see that in your output at the bottom the build task is not compiling `main.cpp` – drescherjm Jun 02 '20 at 21:23
  • @drescherjm I used the tasks.json file given in this link: https://code.visualstudio.com/docs/cpp/config-clang-mac. Should something be changed? Also, if it helps, my .vscode folder isn't in the same directory as the three files above. The folder containing the three files and the .vscode folder are in the same directory, though. tasks.json has been working for all other standalone .cpp files, though. – Jonathan Lee Jun 02 '20 at 21:40
  • I think that is the problem. It appears to build only the currently selected .cpp file. Someone more familiar with `tasks.json` may have to help fix this. – drescherjm Jun 02 '20 at 21:44
  • This may help: [https://stackoverflow.com/a/59236875/487892](https://stackoverflow.com/a/59236875/487892) – drescherjm Jun 02 '20 at 21:45
  • Related if not a duplicate: [https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files](https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files) – drescherjm Jun 02 '20 at 21:46
  • @drescherjm that didn't seem to work, sadly. I think that the problem might be that the relative location between vscode and my working directory is being incorrectly handled in tasks.json, but I still have no clue how to fix it – Jonathan Lee Jun 02 '20 at 22:28
  • Visual studio code tasks are fine for building dongle files but if you need more than one file it'd be much better to use a proper build system like cmake then call that from your task (or even better use the cmake plugin) – Alan Birtles Jun 02 '20 at 22:32
  • Why ask the same question 4 hours later? – rioV8 Jun 03 '20 at 03:19

0 Answers0