Michael Soulier
Michael Soulier

Reputation: 821

How to compile C++ on Mac OS using TR1

I have a preexisting product that builds on Linux, and I'm trying to port it to Mac OS.

msoulier@merlin:~$ xcode-select -v
xcode-select version 2343.
msoulier@merlin:~$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

The problem is that it uses the tr1/tuple library, and for some reason, that is not in the default include path.

msoulier@merlin:~$ ls /usr/include/c++/4.2.1/tr1/tuple
/usr/include/c++/4.2.1/tr1/tuple

So it's there, which should be in the include path based on the --with-gxx-include-dir option above,

And yet

msoulier@merlin:~$ cat hello.cpp
#include <iostream>
#include <tr1/tuple>

using namespace std;

int main(void) {
    cout << "Hello, World!" << endl;
    return 0;
}
msoulier@merlin:~$ g++ -o hello hello.cpp
hello.cpp:2:10: fatal error: 'tr1/tuple' file not found
#include <tr1/tuple>
        ^
1 error generated.

Why doesn't this just work?

Thanks.

Upvotes: 2

Views: 3609

Answers (1)

Brendan Shanks
Brendan Shanks

Reputation: 3236

Short answer: call clang++ with -stdlib=libstdc++, and the tr1 headers will be there.

Long answer: The reason for your error and the 2 sets of C++ includes is that macOS/Xcode has two different C++ standard libraries you can build against: an old GNU libstdc++, and the new and modern LLVM libc++.

As of macOS 10.12 Sierra, the default is now libc++ and libstdc++ is deprecated. libstdc++ is quite old, v4.2.1, and predates C++11 (hence the tr1 headers). If you're going to be maintaining this code long-term, it'd be worth the time to make it C++11 compliant (i.e. #include <tuple>)

Update: Xcode 10 no longer allows building against libstdc++. Either update your codebase to use standard C++11 headers, or use Xcode 9 if that's really not an option.

Upvotes: 7

Related Questions