lundi 2 avril 2018

PIMPL not compile on macOS

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)

Aucun commentaire:

Enregistrer un commentaire