shigeya
shigeya

Reputation: 4922

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

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:

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

with c++11 as compile option:

% 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.

I'm using clang++ on MacOS X 10.8.

Update 1

I have tried a sample at cppreference.com site but it won't compile too. Giving some option solve the problem?

Update 2

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

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:

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)

Or functional just does not work?

Upvotes: 4

Views: 7113

Answers (2)

Brett Hale
Brett Hale

Reputation: 22328

For C++11, it's best to always invoke clang as: 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.

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

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

Upvotes: 5

user529758
user529758

Reputation:

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.

Upvotes: 6

Related Questions