I am on macOS High Sierra 10.13.6
I installed thrift as follows:
$ brew install thrift
It is the following version:
$ thrift --version
Thrift version 0.11.0
I installed boost as follows:
$ brew install boost
I have the following model.thrift file:
struct Person {
1: required i32 age;
}
I run the Thrift compiler to generate the cpp code as follows:
$ thrift -gen cpp model.thrift
I have to following cpp_encode.cpp file that includes the generate code:
#include "gen-cpp/model_types.h"
int main(int argc, char const *argv[])
{
return 0;
}
I attempt to compile the file as follows:
$ g++ -Wall -I /usr/local/include -L /usr/local/lib -o cpp_encode cpp_encode.cpp
The compiler version is as follows:
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I get the following compile error:
$ g++ -Wall -I /usr/local/include -L /usr/local/lib -o cpp_encode cpp_encode.cpp
In file included from cpp_encode.cpp:1:
In file included from ./gen-cpp/model_types.h:14:
In file included from /usr/local/include/thrift/TBase.h:24:
In file included from /usr/local/include/thrift/protocol/TProtocol.h:28:
In file included from /usr/local/include/thrift/transport/TTransport.h:24:
/usr/local/include/thrift/stdcxx.h:32:10: fatal error: 'boost/tr1/functional.hpp' file not found
#include <boost/tr1/functional.hpp>
The directory /usr/local/boost directory exists:
$ ls -1 /usr/local/include/boost
accumulators
algorithm
align
align.hpp
aligned_storage.hpp
any.hpp
archive
[...]
But the directory /usr/local/include/boost/tr1 does not exist:
$ ls -1 /usr/local/include/boost/tr1
ls: /usr/local/include/boost/tr1: No such file or directory
Should I install /usr/local/include/boost/tr1? If so, how?
Should I use Thrift differently?
There are a couple of issues here:
On macOS you must use clang++ instead of g++. Also, you must 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. See How to compile C++ with C++11 support in Mac Terminal
You don't need -I /usr/local/include or -L /usr/local/lib (but adding them doesn't cause harm)
If you want the program to not just compile but also link, there are some additional issues:
You also need to compile and link with the generated code gen-cpp/model_types.cpp
You need add the implementation of Coordinate::operator to the program. Thrift does generate the declaration for the Coordinate::operator< but does not generate the implementation of Coordinate::operator<. The reason for this is that Thrift does not understand the semantics of the struct and hence cannot guess the correct implementation of the comparison operator. This is discussed at http://mail-archives.apache.org/mod_mbox/thrift-user/201007.mbox/%3C4C4E08BD.8030407@facebook.com%3E
.
bool Coordinate::operator<(const Coordinate& other) const
{
if (x < other.x) {
return true;
} else if (x > other.x) {
return false;
} else if (y < other.y) {
return true;
} else {
return false;
}
}
Given all of the above, the following works:
clang++ -std=c++11 -stdlib=libc++ -Wall -o cpp_encode cpp_encode.cpp gen-cpp/model_types.cpp
Note 1: This simple program does not require it, but once you write more complete Thrift code, you will need to link with the thrift library (-lthrift) and with the boost library (-lboost).
Note 2: I am granting the bounty to Corristo; even though his reply did not quite give the answer, his suggested test program got me started on the path that eventually led to the solution.