0

I am reading the pimpl code from github, and tried to compile in my macOS laptop as follows:

file: foo.cpp

#include "foo.h"
#include <memory>

class foo::impl
{
  public:
    void do_internal_work()
    {
      internal_data = 5;
    }
  private:
    int internal_data = 0;
};
foo::foo()
  : pimpl{std::make_unique<impl>()}
{
  pimpl->do_internal_work();
}
foo::~foo() = default;
foo::foo(foo&&) = default;
foo& foo::operator=(foo&&) = default;

file: foo.h

#include <memory>
class foo
{
  public:
    foo();
    ~foo();
    foo(foo&&);
    foo& operator=(foo&&);
  private:
    class impl;
    std::unique_ptr<impl> pimpl;
};

file man.cpp

#include "foo.h"

#include <iostream>

int main() {
    foo x;
} 

I tried to compile by using clang++ -std=c++14 main.cc -o main, but there's an error:

Undefined symbols for architecture x86_64: "foo::foo()", referenced from: _main in main-b39a70.o "foo::~foo()", referenced from: _main in main-b39a70.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
pvd
  • 1,303
  • 2
  • 16
  • 31

1 Answers1

1

You're haven't added any of the code from foo.cpp to your compiler. Compiling with clang++ -std=c+14 main.cpp foo.cpp -o main should fix your problem.

Clearer
  • 2,166
  • 23
  • 38