Builds in xcode 4.6 but doesn't work with command line

When I run the following piece of code from Xcode4.6 it compiles and works fine. But when I try to compile it using the command line tool (clang ++), it cannot do it.

#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 the compilation 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


source to share


4 answers


You can see for yourself that the command line Xcode is being used.



  • Create your project in Xcode.
  • Switch to log view. The icon for it looks like a speech bubble with multiple lines in it.
  • Click on the latest version.
  • A list of basic build steps will appear in the main editing area. Right-click "Compile main.cpp" and select "Copy Transcription for Results Shown".
  • Paste this into your favorite text editor to see the exact command line that Xcode used to create your project.
+1


source


Try using clang's own standard library:

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

      



The default is the GNU Standard Library ( libstdc++

), but the included Apple version is quite old and does not support C ++ 11.

+2


source


Thanks to everyone who suggested solutions to me that kept me from going.

Finally, this is what worked for me.

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

to compile my code.

Everything went perfectly. thank!!

0


source


Make sure you are calling clang++

, not clang

both the compiler and the linker.

clang++

(as a compiler) needs compiler flags -std=c++11

and -stdlib=libc++

, and clang++

(as a linker) needs a flag -stdlib=libc++

.

0


source







All Articles