Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X/Clang won't use c++11 headers

system info: OS X 10.10.5, Clang = Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn), cmake = 2.8.12.2

Suppose I have some simple file, main.cpp:

#include <stdio.h>

#include <vector>

#include <algorithm>

int main(void)
{
    std::vector<int> v{1, 2, 3, 4};

    int sum = std::accumulate(v.begin(), v.end(), 0);

    printf("Sum = %d\n", sum);
    return 0;
 }

When I run "clang++ -stdlib=libc++ -std=c++11 main.cpp" I get error:

main.cpp:11:20: error: no member named 'accumulate' in namespace 'std' int sum = std::accumulate(v.begin(), v.end(), 0);

When I look, using an IDE (Qt Creator), I see that the included header is /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm

when I look through my file system, I see that /usr/include/c++/4.2.1 exists with c++11 compliant headers.


Next, I use cmake to control larger builds (the above is just an example setup).

So here's a token CMakeLists.txt file for the above example:

project(c11Test)
cmake_minimum_required(VERSION 2.8)

set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_CXX_FLAGS "-stdlib=libc++ -std=gnu++11")

add_executable(${PROJECT_NAME} main.cpp)

Which, when I build, creates this output (excerpt):

[100%] Building CXX object CMakeFiles/c11Test.dir/main.cpp.o /usr/bin/c++ -stdlib=libc++ -std=gnu++11 -o CMakeFiles/c11Test.dir/main.cpp.o -c /Users/username/c11Test/main.cpp /Users/username/c11Test/main.cpp:11:20: error: no member named 'accumulate' in namespace 'std' int sum = std::accumulate(v.begin(), v.end(), 0); ~~~~~^ 1 error generated.

(whole output here):

I'm aware of this post, which seems to imply that all I need to do is just include these compiler flags. But that doesn't seem to work.

Furthermore, I probably need to build this on a variety of OS X computers at different versions, so I really wonder if there's a general solution I'm overlooking here?

like image 461
shavera Avatar asked Sep 16 '15 17:09

shavera


1 Answers

Another friend provided the answer:

In my bigger program (not here posted, obv.) Something had included <numeric> in some kind of hidden way. When I went to compile on OSX, it was no longer included. I had thought <algorithm> was the necessary component, but clearly it wasn't. So just my error.

like image 175
shavera Avatar answered Nov 04 '22 00:11

shavera