4

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?

jww
  • 97,681
  • 90
  • 411
  • 885
shigeya
  • 4,862
  • 3
  • 32
  • 33

2 Answers2

6

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.

  • I'm just compiling the single source code without linking (see above `-c` option above) so it does not matter wrt linking. But you may right, on the selection of standard library affect header file selection. – shigeya Jul 25 '13 at 04:49
  • 1
    @shigeya I can differentiate between linkage and compilation, I assure you. That's why I said "this is not about the definition". It's about the declaration. (Well, strictly speaking, the definition may also be in the header because `std::function` is a class template, but you get my point.) –  Jul 25 '13 at 04:52
  • And giving `-stdlib=libc++` to the sample from cppreference.com don't help. It even can't find `functional` header file. odd. may be OSX specific issue. I will try on other platform later. – shigeya Jul 25 '13 at 05:00
  • 1
    @shigeya This is an OS X-specific solution; C++11 is supported, as far as I can tell, by GNU glibc++ and g++ without any further hacking. Note that you may have to install the latest version of the development tools - check for an update in Xcode, and if available, install the most recent "Command Line Tools" package. –  Jul 25 '13 at 05:02
  • 2
    Am I the only one who finds it ironic that Apple, the poster child company for ease-of-use, has a compilation setup that requires users to tell the compiler to use the correct C++ standard library? – Casey Jul 25 '13 at 05:43
  • 1
    Thanks. As I wrote in the updated question, `-stdlib=libc++` doesn't help in my case. Command line tools are updated. And I agree it's ironic. – shigeya Jul 25 '13 at 12:36
  • 1
    @shigeya If I understand correctly, you still couldn't get this working. Now try passing `-isysroot ` to the compiler, where DEVROOT is the development sysroot directory, located somewhere in /Applications/Xcode.app, and it's called `MacOSX10.8.sdk`. –  Jul 25 '13 at 12:58
  • @H2CO3 Still having problem. `-sysroot` doesn't help. can't find functional as same as the output on Update 2 in the above question. (Installing preview version of Xcode might help?) – shigeya Jul 25 '13 at 22:55
  • Actually, Apple did a good job. Thank you very much answering quickly. – shigeya Jul 26 '13 at 12:38
  • Perfect, thanks. And -1 to Apple and their ancient, non-working stuff in 2013. – jww Nov 12 '13 at 04:06
5

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.

Community
  • 1
  • 1
Brett Hale
  • 21,653
  • 2
  • 61
  • 90