3

When I run following code snippet from Xcode4.6 it compiles and runs fine. But when I try to compile it using command line tool (clang++) it fails to do so.

#include <iostream>
#include <memory>

int main(int argc, const char * argv[])
{

    std::unique_ptr<int> foo(new int(0));

    // insert code here...
    std::cout << "Hello, this is cool giri World!\n";
    return 0;
}

Here is compile log:

$ clang --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix

$ clang++ main.cpp -stdlib=libc++ -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ -I /usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include/ 
main.cpp:7:10: error: no member named 'unique_ptr' in namespace 'std'
    std::unique_ptr foo(new int(0));
    ~~~~~^
main.cpp:7:24: error: expected '(' for function-style cast or type construction
    std::unique_ptr foo(new int(0));
                    ~~~^
main.cpp:7:26: error: use of undeclared identifier 'foo'
    std::unique_ptr foo(new int(0));
                         ^
3 errors generated.
3
  • Yeah I tried that as well. same result. Feb 12, 2013 at 9:37
  • clang++ -std=c++11 cannot find the definition for std::unique_ptr, even when #include <memory>. strange compiler this. use gcc.
    – Walter
    Feb 12, 2013 at 9:47
  • Add -stdlib=libc++ to your compilation cmdline and link cmdline. Odd, I know, but do it.
    – WhozCraig
    Mar 30, 2013 at 7:47

4 Answers 4

2

Try using clang's own standard library:

clang -std=c++11 -stdlib=libc++ main.cpp

The default is GNU's standard library (libstdc++), but the version Apple included is quite old and doesn't have C++11 support.

2
  • I wrote about this very problem here: marshall.calepin.co/… Feb 12, 2013 at 17:35
  • thanks! this is what ideally should work. But unfortunately its not working for me and hence I am trying out too many things. Feb 13, 2013 at 7:00
1

You can look for yourself to see what command line Xcode used.

  1. Build your project in Xcode.
  2. Switch to log view. The icon for it looks like a speech bubble with a couple of lines in it.
  3. Click on the latest build.
  4. A list of build steps will show up in the main editing area. Right-click on "Compile main.cpp" and select "Copy Transcript for Shown Results".
  5. Paste this into your favorite text editor to see the exact command line that Xcode used to build your project.
2
  • thanks ! this is useful. I was interested in seeing how xcode does compilation and linking. But output is quite overwhelming . Its huge command which begins with somehthing like Feb 13, 2013 at 6:48
  • thanks ! this is useful. I was interested in seeing how xcode does compilation and linking. But output is quite overwhelming . Its huge command which begins with something like "/Applications/xcode4.6/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=0 -std=gnu++11 -stdlib=libc++ ..." and proceeds to includes lot of things from project folder. If I extract out required things for me and use it, it produces .o file but link command doesn't accept it as valid file. I will keep trying. thanks again. Feb 13, 2013 at 6:59
0

Make sure you are invoking clang++, not clang, for both the compiler and linker.

clang++ (as compiler) needs the -std=c++11 and -stdlib=libc++ compiler flags, and clang++ (as linker) needs the -stdlib=libc++ linker flag.

0

thanks Everyone for suggesting me solutions which kept me going.

Finally this is what worked for me.

I uninstalled command line tools using shell script mentioned in http://www.cocoanetics.com/2012/07/you-dont-need-the-xcode-command-line-tools/
and then used $xcode-select -switch /Applications/Xcode.app/Contents/Developer/ to set xcode version . and finally used $xcrun clang++ main1.cpp -stdlib=libc++

to compile my code.

This worked fine. thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.