Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aquamite

macrumors 6502
Original poster
Oct 2, 2014
384
176
Spain
I have no experience with using other libraries in C++ other than the STL. I have been using clang++ for compiling small C++ codes and projects using Terminal commands with makefiles and rarely I've had to use XCode. However it had to come the day I had to move beyond that soft code beach and step into the big jungle of option crowded IDE's and C++ libraries.

I've downloaded from here: http://sourceforge.net/projects/boost/files/boost/1.60.0/ the latest version of the boost library. I've unziped it in a folder. Then I've opened a Terminal in the unziped folder called "boost" and naively without having read the installation instructions deeply thinking that it would be easy to get it to work I've executed "./bootstrap.sh" and then "./b2" and after a (veeeeery long) while it seems like it finished the installation.

And after that, when I come to compile the code where I use some .hpp from the boost library with clang++ using the terminal I get the following error:

Code:
fatal error: 'boost/asio.hpp' file not found
#include <boost/asio.hpp>

I don't know if that error means that the installation of boost hasn't been successful or what. I can't even find a way to confirm that the installation of the library performed well. I've googled a lot trying to find a solution and some say to try to compile the code using XCode changing some header parameters that I don't know about. I do really get lost inside big IDEs sea of options and like to keep things clear.

Can anybody help me with this? It's the first time I'm using external libraries with C++.

Thanks a lot.
 

chown33

Moderator
Staff member
Aug 9, 2009
11,003
8,899
A sea of green
Post the exact command line that invokes clang++.

Where did the installer install the header files? You'll need to know that.

At the command-line, you must add directories where includes are looked for. Look at the man-page for clang++ and read about the options for how to specify include-file dirs.
http://clang.llvm.org/docs/CommandGuide/index.html

The option is -Idir, but read the man-page for details.

You'll probably need a similar command-line option to tell clang where to find the library for linking. You'll need to know that, too.
 
  • Like
Reactions: Aquamite

teagls

macrumors regular
May 16, 2013
202
101
Post the exact command line that invokes clang++.

Where did the installer install the header files? You'll need to know that.

At the command-line, you must add directories where includes are looked for. Look at the man-page for clang++ and read about the options for how to specify include-file dirs.
http://clang.llvm.org/docs/CommandGuide/index.html

The option is -Idir, but read the man-page for details.

You'll probably need a similar command-line option to tell clang where to find the library for linking. You'll need to know that, too.

First of all you should install boost using homebrew to make sure it is done correctly. Then you have to add the search path for the boost headers from something like /usr/local , and link the library in Xcode. To put it in simple terms. Xcode has no idea you have it installed where it is at and where the actually compiled boost library is. You have to point it to the library and the header files so you can make API calls to the library.

http://brew.sh
 

Aquamite

macrumors 6502
Original poster
Oct 2, 2014
384
176
Spain
Xcode has no idea where the actually compiled boost library is.

Me neither... :(

Post the exact command line that invokes clang++.

Code:
clang++ -o *.cpp

Where did the installer install the header files? You'll need to know that.

I don't know that I've searched for "boost" with finder in /usr and it didn't find anything.

At the command-line, you must add directories where includes are looked for. Look at the man-page for clang++ and read about the options for how to specify include-file dirs.
http://clang.llvm.org/docs/CommandGuide/index.html

The option is -Idir, but read the man-page for details.

You'll probably need a similar command-line option to tell clang where to find the library for linking. You'll need to know that, too.

The man-page only shows this:
Code:
-idirafter <value>      Add directory to AFTER include search path
And also:
Code:
       -Idirectory
           Add the specified directory to the search path for include files.

First of all you should install boost using homebrew to make sure it is done correctly. Then you have to add the search path for the boost headers from something like /usr/local , and link the library in Xcode. To put it in simple terms. Xcode has no idea you have it installed where it is at and where the actually compiled boost library is. You have to point it to the library and the header files so you can make API calls to the library.

http://brew.sh

I'd rather do it without third party software that I don't fully understand.
 

teagls

macrumors regular
May 16, 2013
202
101
I'd rather do it without third party software that I don't fully understand.

Homebrew is a package manager for mac. "It's all git and ruby underneath". All it does is download the appropriate version from git for your system. Compiles and builds it and then puts it into a standard location and creates a symlink so it can be easily accessed from other applications/tools like Xcode. To say you'd rather do it without third party software is naive. You should really read more in detail what it is and what a package manger is. It would probably also help you understand how to include external libraries and headers in Xcode.
 
  • Like
Reactions: Aquamite

r03dz

macrumors member
Aug 5, 2014
69
69
Homebrew is glorious. It's open source, all pull requests are reviewed before they are accepted. I use it everyday. brew install boost
[doublepost=1453531345][/doublepost]I am just curious here. What particular feature are you going to be using from Boost?
 

aiusepsi

macrumors newbie
Jun 5, 2014
2
1
I'm going to add to the chorus saying to use Homebrew. It's totally great, pretty much every library you could ever want to install is on there, it makes your life much, much easier, and pretty much everyone uses it. And while it is third-party, it's a) open source, b) the original creator now works on the Swift dev tools at Apple. So there's that.

As far as using Boost goes, you need to tell the compiler where to find the header files. Boost.Asio is header-only, but other Boost libraries aren't header only, and in those cases you could also need to tell the linker where to find the compiled libraries and which libraries to link. It may be that Asio requires Boost.System to be linked; I'd have to check.

Adding an extra include path is easy: when you invoke clang++, add the option -I/usr/local/include as /usr/local/include is where headers installed by Homebrew go. If you installed Boost some other way you'll have to figure out where the Boost build scripts put this stuff by default. This is one of the good things about Homebrew, you learn once that everything you need is in /usr/local, and then everything just works.

To link libraries, you need to give the linker (which in this case, is also clang++) the option -L/usr/local/lib (path will differ if not using Homebrew) and then use -l to bring in any libraries you want, for example -lboost_system to link
libboost_system.a (the compiler adds the "lib" and the extension for you).
[doublepost=1453848494][/doublepost]This compiles for me fine with Homebrew-installed Boost:
Code:
 // boost_test.cpp
#include <boost/asio.hpp>
int main()
{
}

Code:
clang++ -I/usr/local/include -L/usr/local/lib -lboost_system boost_test.cpp
 
Last edited:
  • Like
Reactions: Aquamite
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.