Tag Archives: Folly

Setting up Facebook Folly on Ubuntu

This is how I installed the Facebook Folly C++ libraries on Ubuntu.

Step 1: The source

I downloaded master.zip from https://github.com/facebook/folly/archive/master.zip and unzipped it, creating the directory /home/martin/folly-master.

Step 2: Install prerequisites

I just installed everything mentioned in the README.md. I had most of these already:

sudo apt-get install \
    g++ \
    automake \
    autoconf \
    autoconf-archive \
    libtool \
    libboost-all-dev \
    libevent-dev \
    libdouble-conversion-dev \
    libgoogle-glog-dev \
    libgflags-dev \
    liblz4-dev \
    liblzma-dev \
    libsnappy-dev \
    make \
    zlib1g-dev \
    binutils-dev \
    libjemalloc-dev \
    libunwind8-dev \
    libelf-dev \

Step 3: Build

autoreconf -ivf
sudo make install

I couldn’t get make check to work because Google Test has changed, so I skipped that step.

Step 4: Install header files

sudo mkdir /usr/include/folly
sudo mkdir /usr/include/folly/detail
sudo cp /home/martin/folly-master/folly/*.h /usr/include/folly
sudo cp /home/martin/folly-master/folly/detail/*.h /usr/include/folly/detail
sudo cp -r /home/martin/folly-master/folly/compatibility /usr/include/folly

I’m not sure if copying the header files from detail was necessary, but it doesn’t do any harm.

Step 5: Install the libraries

sudo cp /home/martin/folly-master/folly/.libs/libfolly.so.57.0.0 /usr/local/lib
cd /usr/local/lib
sudo ln -s libfolly.so.57.0.0 libfolly.so
sudo ln -s libfolly.so.57.0.0 libfolly.so.57
sudo ldconfig -v

Step 6: Try it

I wrote this program in hello.cpp:

#include <iostream>
#include <folly/FBString.h>

int main()
    folly::fbstring str("Hello, Folly World!");
    std::cout << str << "\n";

I compiled it like this:

g++ hello.cpp -o hello -lfolly

Finally, I ran it:

Hello, Folly World!


How to do string formatting in C++


  1. Use vsnprintf() (C++11 and POSIX)
  2. Use Boost.Format
  3. Use folly::format()

Method 1: Use vsnprintf() (C++11 and POSIX)

#include <iostream>
#include <vector>
#include <string>
#include <cstdarg>
#include <cstring>

std::string format(const std::string& format, ...)
    va_list args;
    va_start (args, format);
    size_t len = std::vsnprintf(NULL, 0, format.c_str(), args);
    va_end (args);
    std::vector<char> vec(len + 1);
    va_start (args, format);
    std::vsnprintf(&vec[0], len + 1, format.c_str(), args);
    va_end (args);
    return &vec[0];

int main()
    char str[] = "%s => %d";
    std::cout << string_format(str, "apples", 7) << "\n";

Method 2: Use Boost.Format

#include <iostream>
#include <boost/format.hpp>

int main()
    char str[] = "%s => %d";
    std::cout <<  boost::str(boost::format(str) % "apples" % 7) << "\n";

Reference: Boost Format library

Method 3: Use folly::format()

#include <iostream>
#include <folly/Format.h>

int main()
    char str[] = "{} => {}";
    std::cout <<  folly::format(str, "apples", 7) << "\n";

Reference: folly/Format.h


How to concatenate a string and an int in C++


  • Use std::stringstream
  • Use boost::lexical_cast()
  • Use boost::format()
  • Use std::to_string() (C++11)

Method 1. Use std::stringstream

#include <sstream>

std::stringstream ss;
ss << name << age;
result = ss.str();

Method 2. Use boost::lexical_cast()

#include <boost/lexical_cast.hpp>

result = name + boost::lexical_cast<std::string>(age);

Reference: Boost.Lexical_Cast

Method 3. Use boost::format()

#include <boost/format.hpp>

result =  boost::str(boost::format("%s%d") % name % age);

Reference: Boost Format library

Method 4. Use std::to_string() (C++11)

result = name + std::to_string(age);

Method 5. Use folly::toAppend()

#include <folly/Conv.h>

result =  name;
folly::toAppend(age, &result);

Reference: folly/Conv.h


How to convert an int to a std::string in C++


  1. Use std::stringstream
  2. Use boost::lexical_cast
  3. Use folly::to()
  4. Use std::to_string (C++11)

Method 1: Use std::stringstream

#include <sstream>

template <typename T>
std::string to_string(const T& thing)
    std::stringstream ss;
    ss << thing;
    return ss.str();

template <>
std::string to_string<std::string>(const std::string& s)
    return s;

std::cout << ::to_string(n) << "\n";

Method 2: Use boost::lexical_cast

#include <boost/lexical_cast.hpp>
std::cout << boost::lexical_cast<std::string>(n) << "\n";

Reference: Boost.Lexical_Cast

Method 3: Use folly::to()

#include <folly/Conv.h>
std::cout << folly::to<std::string>(n) << "\n";

Reference: folly/Conv.h

Method 4: Use std::to_string (C++11)

std::cout << std::to_string(n) << "\n";