Where is the definition of std :: function in clang ++ (3.3 / Xcode)

Problem solved => see update at the end

I'm trying to use std::function , but it seems like just turning on <functional> does not give a definition. I tried to compile the following code:

 #include <functional> std::function<int(int)> f = nullptr; 

with C ++ 11 parameter:

 % 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, so I don't know important knowledge. I think so.

I am using clang ++ on MacOS X 10.8.

Update 1

I tried the sample on cppreference.com , but it will not compile either. Providing some solutions to the problem?

Update 2

Tried the cppreference.com sample above with clang++ -c -std=c++11 -stdlib=libc++11 x.cc , and the compiler still says:

 x.cc:1:10: fatal error: 'functional' file not found #include <functional> ^ 1 error generated. 

Where is it functional? I think I should give -stdlib=libc++11 or something else, but this also does not work:

 clang: error: invalid library name in argument '-stdlib=libc++11' 

How can I find the argument list for -stdlib ? (note: in the man page, only the available libc++ and libstdc++ options are available, both of them do not work)

Or functional just doesn't work?

+4
source share
2 answers

For C ++ 11, it is better to always call clang as: clang++ -std=c++11 -stdlib=libc++

I use this most of the time, so I set the $CXX environment variable to this value. Thus, I get a choice of dialect and library both in compilation and in layout. -std=c++11 not enough, because clang will still use the (old) system gcc headers in /usr/include/c++/4.2.1 .

-stdlib=libc++ will use clang headers in /usr/lib/c++/v1 , such as <functional> .

There's a similar question with the answer of Howard Hinnant, who (IIRC) is an Apple engineer.

+5
source

This is not about defining a function. You do not have a linker error. You have a compiler error. The problem seems to be that the standard BSD / GNU / Darwin library installed in real sysroot does not support C ++ 11. You should use the one that comes with Clang by specifying the -stdlib=libc++ flag of the compiler.

+6
source

Source: https://habr.com/ru/post/1493348/


All Articles