C++ 为什么这个C++;程序在MacOS上编译,但不在Ubuntu上编译?
我在Ubuntu和MacOS机器上使用的是Clang版本10:C++ 为什么这个C++;程序在MacOS上编译,但不在Ubuntu上编译?,c++,macos,ubuntu,default-constructor,C++,Macos,Ubuntu,Default Constructor,我在Ubuntu和MacOS机器上使用的是Clang版本10: ubuntu $ clang++ --version clang version 10.0.0-++20200227124856+593a0dda7a6-1~exp1~20200227115450.103 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin 以下简单程序不在Ubuntu(16.04)上编译,但在MacOS 10.14上编译:
ubuntu $ clang++ --version
clang version 10.0.0-++20200227124856+593a0dda7a6-1~exp1~20200227115450.103
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
以下简单程序不在Ubuntu(16.04)上编译,但在MacOS 10.14上编译:
// test.cpp
#include <atomic>
struct foo {
bool bar;
int baz;
foo(bool bar, int baz) : bar(bar), baz(baz) {
}
};
int main(int argc, char* argv[]) {
std::atomic<foo> test_struct({false, 0});
test_struct.load();
return 0;
}
Ubuntu上的错误是
In file included from test.cpp:1:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/atomic:234:13: error: no matching constructor for initialization of 'foo'
_Tp tmp;
^
test.cpp:13:11: note: in instantiation of member function 'std::atomic<foo>::load' requested here
test_struct.load();
^
test.cpp:2:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
struct foo {
^
test.cpp:2:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
test.cpp:6:3: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
foo(bool bar, bool baz) :
^
1 error generated.
test.cpp:1中包含的文件中的:
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../include/c++/5.4.0/atomic:234:13:错误:“foo”的初始化没有匹配的构造函数
_Tp-tmp;
^
test.cpp:13:11:注意:在这里请求的成员函数“std::atomic::load”的实例化中
测试结构加载();
^
test.cpp:2:8:注意:候选构造函数(隐式副本构造函数)不可行:需要1个参数,但提供了0
结构foo{
^
test.cpp:2:8:注意:候选构造函数(隐式移动构造函数)不可行:需要1个参数,但提供了0
test.cpp:6:3:注意:候选构造函数不可行:需要2个参数,但提供了0
foo(boolbar,boolbaz):
^
生成1个错误。
我是否遇到了一些未定义的行为?或者我需要采取什么措施使这两种情况完全相同
< > > > >编辑< /强>我可以通过向类型添加默认构造函数来编译此代码。但是,我不能解析这个要求,我很困惑为什么默认构造函数出现在MaOS上而不是Ubuntu上。< /P> < P>答案是在Linux系统上,<代码> CLAN< /COD>链接到GNU C++标准库INFULL心理状态
libstc++
,在本例中,它太旧(5.4版或类似版本),不能与所选的语言标准配合使用(c++14
)
有两种解决方案:
libstdc++
,它可能在Ubuntu版本的APT软件包存储库中,也可能不在其中(在我的情况下,没有更新的版本)clang
使用LLVM实现libc++
。确保安装了最新版本(包libc++-dev
),并在编译过程中传递-stdlib=libc++
我得到了<代码>错误:“继续”在这个范围内没有声明<代码>。在Ubuntu中,您似乎有一个旧版本的库。参见U.buntuCLANK链接上的V..O.FISHORE默认为“代码”> LBSTDC++< /COD>这是GNU对C++标准库的实现。您必须采取额外的步骤来链接<代码> LBC++<代码>是LLVM的C++标准库。是的,如果你想链接到LIbSTDC++ +,你需要升级它。你可以回答你自己的问题,所以它不保持打开。@ OrFISH不担心它。鼓励你回答你自己的问题,如果你找到答案。
clang++ -std=c++14 test.cpp -o test
In file included from test.cpp:1:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/atomic:234:13: error: no matching constructor for initialization of 'foo'
_Tp tmp;
^
test.cpp:13:11: note: in instantiation of member function 'std::atomic<foo>::load' requested here
test_struct.load();
^
test.cpp:2:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
struct foo {
^
test.cpp:2:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
test.cpp:6:3: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
foo(bool bar, bool baz) :
^
1 error generated.