builds in xcode 4.6 but fails using command line - c++

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.

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.

You can look for yourself to see what command line Xcode used.
Build your project in Xcode.
Switch to log view. The icon for it looks like a speech bubble with a couple of lines in it.
Click on the latest build.
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".
Paste this into your favorite text editor to see the exact command line that Xcode used to build your project.

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

Related

In Visual Studio Code: "no template named 'initializer_list' in namespace 'std'"? [duplicate]

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).
Which option should I use to turn on C++11 support?
By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.
As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.
xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.
For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.
XCode uses clang and clang++ when compiling, not g++ (assuming you haven't customized things). Instead, try:
$ cat t.cpp
#include <iostream>
int main()
{
int* p = nullptr;
std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out
0x0
Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.
If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11 instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

C++ 20 modules in Xcode 14.0.1

I would like to experiment with newer C++ features from (at least) C++20 and I'm new to C++ development on macOS in particular.
How do I enable importing of library headers in the 'module way'?
import <iostream>;
using namespace std;
int main(int argc, const char * argv[]) {
cout << "Hello, World!\n";
return 0;
}
The build errors are
Use of undeclared identifier 'iostream'
Use of undeclared identifier 'std'
I was looking at this answer and tried installing llvm and I suppose that I could run the provided commands from the terminal, but I have not tried. Is there a way to make Xcode issue the commands provided in that answer (see below)? Where would I enter them in XCode? And what do they do? I'd like to avoid thinking about terminal commands and just have something that works.
/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -c -Xclang -emit-module-interface mathlib.cpp -o mathlib.pcm
/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -fmodules -c -fprebuilt-module-path=. main.cpp -o main.o
/opt/homebrew/opt/llvm/bin/clang++ -std=c++2a -fmodules -o main main.o *.pcm
I saw that when making a new C++ project, Xcode defaults to the Apple Clang compiler and GNU++20 [-std=gnu++20] C++ Language dialect. There is also another option C++20 [-std=c++20] (what's the difference?) which I also tried, resulting in the same errors.
FYI, when I do clang -v I get
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
And a general question about other build systems such as CMake, which I discovered during my research for C++ compilation on mac. After making a CMake project, is it possible to use the regular IDE tools such as code suggestions and debugging?

Clang MacOs over command line does not work

I am fairly new to both MacOs and C++ and have a problem which is similar to the one described here but also no solution I find in the Internet works.
'fatal error: 'wchar.h' file not found' error with the new macos 11.3 update
If I try to compile the most simple c++ program on my machine via command line it does not work.
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
Since it used to work I probably broke something but don't know what
The simple test command I use is the following
clang -v --target=arm64 helloworld.cpp
Which results in a iostream not found error
if I now include the xcode include directory via
clang -v --target=arm64 -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ helloworld.cpp
The iostream error disappears and I get a wchar.h not found error
I removed xcode completely and reinstalled it but this seems to not help also does it not make a difference if I use clang or clang++.
With Clion and cmake it works but I do not know why
xcode-select version 2392
/usr/bin/clang
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
locate iostream.h
`/Library/Frameworks/Mono.framework/Versions/6.12.0/include/glib-2.0/gio/gfileiostream.h
/Library/Frameworks/Mono.framework/Versions/6.12.0/include/glib-2.0/gio/giostream.h
/opt/homebrew/Cellar/boost/1.76.0/include/boost/asio/basic_socket_iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/iostreams/detail/iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/math/cstdfloat/cstdfloat_iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/nowide/iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/typeof/std/iostream.hpp
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/gfileiostream.h
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/giostream.h
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/gsimpleiostream.h
I had similar troubles building clang + llvm, then trying to use the newly built clang via
./build/bin/clang -std=c++20 <my_file>
gave me a
fatal error: 'iostream' file not found
What fixed it was if I compiled with the following instead:
./build/bin/clang -std=c++20 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk <my_file>
The fix largely comes from https://github.com/MaskRay/ccls/issues/191#issuecomment-556983460 and the discussion there

Apple Clang; using C++11 with libstdc++

I have an issue when compiling a simple Hello file with an empty function taking initializer_list argument when using both -stdlib=libstdc++ and -std=c++11
If I use only -std=c++11 (which means compiling with libc++)
then the file compiles and prints Hello!
If I comment function_test and I use both -std=c++11 and -stdlib=libstdc++
then the file compiles and prints Hello!
If I keep the function function_test and I use both -std=c++11 and -stdlib=libstdc++
then I get the following error:
$ g++ -stdlib=libstdc++ -std=c++11 -o test test.cpp
test.cpp:1:10: fatal error: 'initializer_list' file not found
#include <initializer_list>
^
1 error generated.
Here is my file
#include <initializer_list>
#include <iostream>
using namespace std;
void function_test(initializer_list<int> something){}
int main(int argc, char * argv[])
{
cout << "Hello!" << endl;
function_test({0});
return 0;
}
Here is my apple clang version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
--with-gxx-include-dir=/usr/include/c++/4.2.1
^^^^^^^
Notice the "4.2". Your libstdc++ is way to old for C++11. Upgrade it to some 5.x version for full C++11 support.
I found out that upgrading to a newer version of libstdc++ is just not possible with apple-llvm(clang). So using some features of C++11 with libstdc++ is not possible. The reason is this one:
Mainline libstdc++ has switched to GPL3, a license which the
developers of libc++ cannot use. libstdc++ 4.2 (the last GPL2 version)
could be independently extended to support C++11, but this would be a
fork of the codebase (which is often seen as worse for a project than
starting a new independent one). Another problem with libstdc++ is
that it is tightly integrated with G++ development, tending to be tied
fairly closely to the matching version of G++.
source:
http://libcxx.llvm.org/docs/
Thanks to all the answers/comments that helped me reach the answer.

Where is definition of std::function in clang++ (3.3/Xcode)

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?
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.
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.