clang++ (3.3/Xcode) 中 std::function 的定义在哪里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17849154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is definition of std::function in clang++ (3.3/Xcode)
提问by shigeya
Problem Solved => see the update at the end
问题已解决 => 看到最后的更新
I'm trying to use std::function
but it looks like just include <functional>
does not provide the definition. I have tried to compile following code:
我正在尝试使用,std::function
但看起来只是包含<functional>
不提供定义。我试图编译以下代码:
#include <functional>
std::function<int(int)> f = nullptr;
with c++11 as compile option:
使用 c++11 作为编译选项:
% clang++ -c -std=c++11 t.cc
cause:
原因:
t.cc:3:6: error: no type named 'function' in namespace 'std'
std::function<int(int)> f = nullptr;
~~~~~^
t.cc:3:14: error: expected unqualified-id
std::function<int(int)> f = nullptr;
^
2 errors generated.
what am I missing? I know C++ well but new to clang++/C++11 thus I lack of important knowledge, I guess.
我错过了什么?我很了解 C++,但我对 clang++/C++11 是新手,因此我想我缺乏重要的知识。
I'm using clang++ on MacOS X 10.8.
我在 MacOS X 10.8 上使用 clang++。
Update 1
更新 1
I have tried a sample at cppreference.com sitebut it won't compile too. Giving some option solve the problem?
我已经在 cppreference.com 站点尝试了一个示例,但它也无法编译。给一些选项解决问题?
Update 2
更新 2
Tried above sample from cppreference.com with clang++ -c -std=c++11 -stdlib=libc++11 x.cc
, and compiler still says:
从 cppreference.com 尝试了上面的示例clang++ -c -std=c++11 -stdlib=libc++11 x.cc
,编译器仍然说:
x.cc:1:10: fatal error: 'functional' file not found
#include <functional>
^
1 error generated.
Where is functional? I guess I should give -stdlib=libc++11
or whatever but it does not work too:
功能在哪里?我想我应该给予-stdlib=libc++11
或其他什么,但它也不起作用:
clang: error: invalid library name in argument '-stdlib=libc++11'
How I can find list of argument for -stdlib
? (note: in man page, only available options are libc++
and libstdc++
both of them don't work)
我如何找到参数列表-stdlib
?(注意:在手册页中,只有可用选项libc++
并且libstdc++
它们都不起作用)
Or functional just does not work?
或者功能只是不起作用?
回答by
This is not about the definition of the function. You don't have a linker error. You have a compiler error. The problem is, presumably, that the BSD/GNU/Darwin standard library installed in the real sysroot doesn't support C++11. You have to use the one that comes with Clang by specifying the -stdlib=libc++
compiler flag.
这与函数的定义无关。您没有链接器错误。你有一个编译器错误。问题大概是安装在真正的 sysroot 中的 BSD/GNU/Darwin 标准库不支持 C++11。您必须通过指定-stdlib=libc++
编译器标志来使用 Clang 附带的那个。
回答by Brett Hale
For C++11, it's best to always invoke clang
as: clang++ -std=c++11 -stdlib=libc++
对于 C++11,最好总是这样调用clang
:clang++ -std=c++11 -stdlib=libc++
I use this most of the time, so I set the environment variable $CXX
to this value. That way, I'm getting the dialect and library option in both compilation and linking. -std=c++11
is insufficient, as clang will still use the (old) system gcc headers in /usr/include/c++/4.2.1
.
我大部分时间都使用这个,所以我将环境变量设置$CXX
为这个值。这样,我在编译和链接中都获得了方言和库选项。-std=c++11
是不够的,因为 clang 仍将在/usr/include/c++/4.2.1
.
-stdlib=libc++
will use the clang headers in /usr/lib/c++/v1
such as <functional>
.
-stdlib=libc++
将在/usr/lib/c++/v1
诸如<functional>
.
There's a similar questionwith an answer by Howard Hinnant, who is (IIRC) an Apple engineer.
苹果工程师 (IIRC) 的 Howard Hinnant 回答了类似的问题。