PIMPL 无法在 macOS 上编译
PIMPL not compile on macOS
我正在阅读 pimpl code from github,并尝试在我的 macOS 笔记本电脑中编译如下:
文件: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;
文件:foo.h
#include <memory>
class foo
{
public:
foo();
~foo();
foo(foo&&);
foo& operator=(foo&&);
private:
class impl;
std::unique_ptr<impl> pimpl;
};
文件man.cpp
#include "foo.h"
#include <iostream>
int main() {
foo x;
}
我尝试使用clang++ -std=c++14 main.cc -o main
编译,但出现错误:
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)
您还没有将 foo.cpp
中的任何代码添加到您的编译器中。使用 clang++ -std=c+14 main.cpp foo.cpp -o main
编译应该可以解决您的问题。
我正在阅读 pimpl code from github,并尝试在我的 macOS 笔记本电脑中编译如下:
文件: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;
文件:foo.h
#include <memory>
class foo
{
public:
foo();
~foo();
foo(foo&&);
foo& operator=(foo&&);
private:
class impl;
std::unique_ptr<impl> pimpl;
};
文件man.cpp
#include "foo.h"
#include <iostream>
int main() {
foo x;
}
我尝试使用clang++ -std=c++14 main.cc -o main
编译,但出现错误:
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)
您还没有将 foo.cpp
中的任何代码添加到您的编译器中。使用 clang++ -std=c+14 main.cpp foo.cpp -o main
编译应该可以解决您的问题。