2

I have a makefile for macOS and Linux, which contains the following command:

cc -std=c++14 foo.cpp bar.cpp

And it compiles fine. foo.cpp and bar.cpp are, as the name suggests C++ files and it contains C++11 syntax. The compilation works fine.

Now if I include <fstream> I get hundred of linker errors. I am wondering, why that is?

Undefined symbols for architecture x86_64:
"std::__1::locale::has_facet(std::__1::locale::id&) const", referenced from:
    bool std::__1::has_facet<std::__1::codecvt<char, char, __mbstate_t> >(std::__1::locale const&) in DiceInvaders-6f5dd4.o
"std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
...

Afaik, cc links to the c compiler, and I would assume due to it's auto detection it compiles it with the C++ compiler. But why does it fail with an additional C++ include?

Is there any counterpart of cc for c++ on a system? If I use g++, I would assume that command is available, and what if the user actually wanted to compile it with his compiler of preference (as in cc)?

Edit: Is $(CXX) a good replacement for cc?

Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86
  • The command for Clang is clang++. – stark Oct 20 '19 at 16:33
  • `$(CXX)` looks like a variable in a build script, not the name of a compiler. Somewhere you'd likely find a `$(CXX)=cc` to set the compiler to cc and then the rest of the script merrily uses `$(CXX)` and gets whatever `CXX` contains. This way you only have to change the compiler once despite how many different rules use the compiler. – user4581301 Oct 20 '19 at 16:46
  • I'm a bit confused as to why the currently provided answer doesn't help. What happens if you just replace `cc` with `c++`? e.g. the compile/link command should be `c++ -std=c++14 foo.cpp bar.cpp` . – G.M. Oct 20 '19 at 17:23
  • @G.M. Because c++ is not on all systems – Daniel Stephens Oct 20 '19 at 18:26
  • Okay, so if you need something that's guaranteed to work on all platforms (or fail if it won't) then you should probably take the advice of @KamilCuk and use something like `cmake`. Having said that, on `macos` and any `linux` box I've used `cc` and `c++` refer to the default `C` and `C++` compilers respectively. – G.M. Oct 20 '19 at 19:03
  • I have a Linux here which has gcc and clang installed but not c++. I think I will use $(CXX) instead which seems to work on all platforms I tried – Daniel Stephens Oct 21 '19 at 03:38

1 Answers1

3

Most probably cc on your system is a symlink to gcc executable. Assuming that is true:

The difference between gcc and g++, quoting the man page, is:

g++ is a program that calls GCC and automatically specifies linking against the C++ library.

So when you invoke gcc it does not link against c++ library. You can link standard c++ library manually:

gcc -lstdc++ 1.cpp

Is there any counterpart of cc for c++ on a system?

The cc command is just a convention that most system follow. It's not standardized, at least I haven't heard where, the utility c99 is standarized by posix. On my linux system with archlinux distribution with the gcc package there is also installed symlink /usr/bin/c++ to g++.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • And if you don't want to link against the C++98 library (`-lstdc++`), you can alternatively link against the C++11 library (`-lc++`). – Eljay Oct 20 '19 at 16:37
  • `$ cc -lc++ 1.cpp /usr/bin/ld: cannot find -lc++`. It doesn't work on my archlinux. I only have `/usr/lib/libstdc++.so`. – KamilCuk Oct 20 '19 at 16:38
  • Unfortunately that doesn't solve the problem. Also -stdlib=libc++ doesn't help – Daniel Stephens Oct 20 '19 at 16:40
  • `solve the problem` - what is the problem? You want to detect the c++ compiler, learn from the best. Ex [cmake](https://github.com/Kitware/CMake/blob/master/Modules/CMakeDetermineCXXCompiler.cmake#L61) tries to find `c++` `g++` `aCC` `cl` `bcc` `xlC` and `clang++` – KamilCuk Oct 20 '19 at 16:43
  • 1
    @Eljay I don't think `-lc++` has anything to do with C++11 support? It's probably Clang's implementation of the standard library (libc++). – HolyBlackCat Oct 20 '19 at 16:44
  • @KamilCuk Still the linker errors as described in the question – Daniel Stephens Oct 20 '19 at 16:45
  • So to which compiler does _your_ `cc` link to? – KamilCuk Oct 20 '19 at 16:45
  • @HolyBlackCat • it'd germane to the "... macOS..." part of the question presuming the OP hasn't installed some other compiler. I've got 4 C++ compilers installed (plus my own homebrew janky C++ compiler), so it's hard to tell. – Eljay Oct 20 '19 at 16:47
  • This is all going to be very platform related. And because this is platform related, people started to use scripts to automate configuring scripts that are used to build projects. Exactly what automake and cmake does - it finds out which compiler to use in specific environment. so `-lstdc++` may work on my system, while it may not work on others. – KamilCuk Oct 20 '19 at 16:52