Pergunta

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.
Foi útil?

Solução 2

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.

Outras dicas

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.

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.

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!!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top