# Clang -std=c++11 -stdlib=libc++ Should enable C++ 11



## neilms (Jan 14, 2013)

I am trying to compile a program that has C++ 11 features. I should say that I first compiled it successfully on a Linux machine running Debian, using g++. I thought that getting the program to work on FreeBSD 9.1 would only require a few changes to my Makefile.

After changing my Makefile, clang is passed the options in the title header above. Using these options should enable 100% of the C++ 11 features. My program fails to compile with one fatal error:


```
clang -std=c++11 -stdlib=libc++ ....
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
1 error generated.
```

At first, I thought well maybe the new standard libc++ has changed c++ and iostream is no longer used. So I looked for documentation on libc++ and could not find any. 

On my system I found that libc++ is in /usr/src/contrib/libc++. I then included the path in my makefile. My program compiles, but I get this error:


```
/usr/bin/ld: cannot find -lc++
clang: error: linker command failed with exit code 1
```

How can I fix this?


----------



## nslay (Jan 14, 2013)

Try using clang++ instead. I think clang is for C.


----------



## neilms (Jan 14, 2013)

clang++ is only a symbolic link to clang. In any event I tried that and the error is still the same. I have added the path to llvm-link to my shell $PATH, but it seems that the gnu linker ld automatically kicks in from /usr/bin.


----------



## Anonymous (Jan 15, 2013)

Try something like


```
clang++ -std=c++11 the_source_file.cpp -o the_linked_binary
```

Without the option -stdlib=libc++ --- let clang++ find its libc++ by itself.

As a side note, at my system, I cannot compile *.cpp files with clang, I need to call clang++.


----------



## SNK (Jan 15, 2013)

If those libs are only in /usr/src/ then they were not included in the system. AFAIK you need to add WITH_LIBCPLUSPLUS=true to /etc/make.conf (or /etc/src.conf). And use clang++.

Not sure to what extent everything got merged to FreeBSD 9.1.


----------



## nslay (Jan 15, 2013)

neilms said:
			
		

> clang++ is only a symbolic link to clang. In any event I tried that and the error is still the same. I have added the path to llvm-link to my shell $PATH, but it seems that the gnu linker ld automatically kicks in from /usr/bin.



While they two may be identical, the name of the executable determines the behavior. Use clang++.


----------



## vertexSymphony (Jan 16, 2013)

Not really *full* support but yeah, the support is quite good ... the clang site seems down atm so I couldn't check (I haven't seen the 3.2 goodies yet)
There you have a grasp on the actual support â†’ http://web.archive.org/web/20120922231808/http://clang.llvm.org/cxx_status.html

And for the "sake of comparison", here's the gcc status on this â†’ http://gcc.gnu.org/projects/cxx0x.html

If you make your code c++11-enabled, please, check those "charts" to see in which version which feature is enabled and do proper preprocessor with an #error or a #warning 
 so if the code that doesn't compile, at least it spits an readable error in case a feature is missing (because yes, if you use a missing feature you'll get all kinds of nasty errors but nothing that really makes sense), and the user/developer compiling the code can try another compiler/version

With clang I use __has_feature() â†’ http://web.archive.org/web/20120922231829/http://clang.llvm.org/docs/LanguageExtensions.html#cxx11
And with GCC I'm not aware of something similar, so I read the "chart" and pick the lowest version to get a working code (If you get all excited, you could a more in-depth analysis of the version and error showing which features the actual compiler doesn't have) : __GNUC__ for major and __GNUC_MINOR__ for minor

Regards.


----------



## euler (Feb 12, 2013)

You have to build, then install libcxxrt and libc++ in order from /usr/src/lib.

See this: http://lists.freebsd.org/pipermail/freebsd-stable/2012-May/067645.html


----------



## jozze (Jun 4, 2013)

neilms said:
			
		

> clang++ is only a symbolic link to clang



Wow, it's true: clang, clang++ and clang-cpp have the same inode number.

In any case, you can try compiling maybe with lang/gcc47. It's the same compiler that Debian uses by default (at least I think it's true for Debian-TESTING). See this document for more info on how to do it properly.


----------



## PageDir (Jun 5, 2013)

jozze said:
			
		

> Wow, it's true: clang, clang++ and clang-cpp have the same inode number.
> 
> In any case, you can try compiling maybe with lang/gcc47. It's the same compiler that Debian uses by default (at least I think it's true for Debian-TESTING).




```
int main(int argc, char** argv)
{
    if (string(argv[0]) == "./a.out") {...}
    else if (string(argv[0]) == "./foo") {...}
    return 1;
}
```


```
ln a.out foo
./a.out
./foo
```


----------

