116

I am developing command line interface executables for both osx and linux using c/c++. The project will link against opencv. Should I use libc++ or libstdc++?

MobileDev
  • 3,750
  • 4
  • 32
  • 35
  • 1
    I don't know, but you may find this of interest: http://clang-developers.42468.n3.nabble.com/Problem-with-libc-libstdc-interoperability-on-OS-X-td4030121.html – DarenW Feb 20 '13 at 05:15
  • 3
    [This answer](http://stackoverflow.com/questions/12542971/using-libstdc-compiled-libraries-with-clang-stdlib-libc) may be helpful. – Yantao Xie Feb 20 '13 at 09:18
  • 2
    if you link against opencv, then use libstdc++. here's why http://stackoverflow.com/questions/13037659/opencv-build-issue-cant-find-ext-atomicity-h – MobileDev Mar 07 '13 at 05:14

2 Answers2

117

I would use the native library for each OS i.e. libstdc++ on GNU/Linux and libc++ on Mac OS X.

libc++ is not 100% complete on GNU/Linux, and there's no real advantage to using it when libstdc++ is more complete. Also, if you want to link to any other libraries written in C++ they will almost certainly have been built with libstdc++ so you'll need to link with that too to use them.

More info here about the completeness of libc++ on various platforms.

plasmacel
  • 8,183
  • 7
  • 53
  • 101
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • 3
    Could you elaborate / provide links about the completeness status of libc++ on Linux? I don't quite understand why this would be platform-specific as libc++ is just a bunch of Standard Library headers. Or do you mean that one needs to build Clang wrt to LLVM runtime libraries that are not well supported on Linux? – TemplateRex Nov 28 '13 at 14:17
  • 1
    @TemplateRex, I don't know the current status, you can look on http://libcxx.llvm.org/. I don't follow libc++ so you're asking the wrong person, but are you suggesting that "a bunch of Standard Library headers" will never have any platform-specific code? – Jonathan Wakely Nov 29 '13 at 12:03
  • Well since you can install Linux on pretty much the same Apple hardware that is running Mac OS X, I wonder where the platform dependency in C++ headers would come from? Maybe some wrappers around builtin CPU intrinsics or IO and exception handling stuff are system dependent, but my understanding was that such stuff is handled in librcxxrt type of binary layers. Aren't the Standard Library headers supposed to be more or less pluggable? – TemplateRex Nov 29 '13 at 12:29
  • 5
    I'm not talking about hardware. Again, I have no idea about libc++, but most C++ standard libraries are implemented over the OS's C library, and e.g. mapping from `std::ctype_base::mask` values to `` constants is entirely platform-dependent. (CPU intrinsics are provided by the compiler, exception handling is done by a low-level ABI layer, but IO is typically done entirely in the C++ and C libraries, not low-level stuff). – Jonathan Wakely Nov 29 '13 at 12:32
  • Thanks Jonathan, great info. I didn't mean to bug you about libc++ in particular as I know you are in the other camp, but I just wondered where platform dependence pops up in a Standard Library. Thanks again for taking the time to explain that. – TemplateRex Nov 29 '13 at 12:54
  • @JonathanWakely There is one strong argument for using _libc++_. That is statically linking, which might not be an option given _libstdc++_'s license. – abergmeier May 08 '16 at 08:02
  • 3
    @abergmeier, that's a bogus argument, because (when used with GCC or other eligible compilation processes) libstdc++ doesn't impose any restrictions on code using it, whether linked dynamically or statically. It's not the LGPL. Please don't spread FUD. https://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.license.what – Jonathan Wakely May 08 '16 at 16:32
  • @JonathanWakely Ah interesting. – abergmeier May 08 '16 at 20:26
35

Major Linux distributions do not provide LLVM libc++, because:

  1. Unlike Apple and FreeBSD, the GPL+3 is not an issue, so no need to implement another stack here.
  2. Linux components have been developed around GNU libstd++ for ages. Some of them do not build on anything else.
  3. While libc++ is strong in new features, it has some problems with legacy code.

If eventually libc++ became part of distributions, it will be as an optional component. linking against it will probably require extra options.

Like Jonathan said, you should use whatever tool is included by default. Clang is safe in Linux to use since is configured as a GCC replacement, so in that aspect you don't have to worry about 2 compilers. Also since you are targeting two platforms, you should take a look to cmake.

Mario Vazquez
  • 351
  • 3
  • 2
  • 5
    Clang is not a GCC replacement... Just another compiler. – Isaac Pascual Nov 15 '16 at 10:30
  • 11
    @IsaacPascual What Mario meant is clang officially has in its design goals to be a drop-in replacement for the prominent compiler on the platform you run it on (e.g. gcc when run on linux). Same for intel's compiler afaik. It's their way to get a broader adoption. – Johan Boulé Aug 06 '18 at 01:35