Boost libraries on OS X

Discussion in 'Mac Programming' started by Soulstorm, Mar 23, 2007.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I thought I should try the boost libraries. So, I downloaded the source but I have a problem understanding what to do.

    I have also downloaded Boost.Jam, and installed it. I ran bjam with the current directory set to the directory of the Boost source. I ran it with the command:

    bjam "-sTOOLS=gcc" install

    But now, where is this supposed to be installed? And how do I use the libraries with XCode?
     
  2. macrumors member

    Joined:
    Feb 18, 2007
    Location:
    Eindhoven
    #2
    Boost install in /usr/local by default. Include the necessary headerfiles. Add the library to your project and compile!

    Wittegijt.
     
  3. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    Seems you have managed to do it. Can you please tell me how to include the necessary headerfiles to be installed?

    Actually, it would be better to briefly tell me how you installed it in the first place. The instructions provided in the boost readme are generic, and I seem to be doing something wrong...
     
  4. macrumors member

    Joined:
    Feb 18, 2007
    Location:
    Eindhoven
    #4
    I've installed Boost some time ago, so I don't remember exactly what I did:)
    I just followed the instructions. Do you have the libaries installed?

    #include <boost/thread/thread.hpp> (or whatever you want to use)
    add /usr/local/include/boost-1_33_1 to your include search path (-I option)
    add /usr/local/lib/ to your library rearch path (-L option)

    Wittegijt.
     
  5. macrumors member

    Joined:
    Feb 18, 2007
    Location:
    Eindhoven
    #5
    Looking at the documentation, I probably would have used:
    bjam "-sTOOLS=darwin" install
    instead of gcc. Have tried that?

    Witegijt.
     
  6. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #6
    I have tried that, but I don't think it worked. Where are the files supposed to be?


    I only see this in my usr/local/ folder:

    [​IMG]

    I am also attaching my terminal output. Something is missing, it seems.

    I should note though, that in my boost directory, a folder was created. It's named "bin", and it has inside a folder named "boost". Inside it there are many folders containing many built libraries, dylibs for gcc and darwin. What's this? I have the feeling that everything is compiled but cannot be placed correctly.
     

    Attached Files:

  7. macrumors newbie

    Joined:
    Jul 18, 2002
    Location:
    Spain
    #7
    You can install boost manually

    Hi,

    If you managed to compile the libraries (if I remember correctly they will not be universal by default) you can manually install them. I copied everything in the bin folder to /usr/local/lib. Note that I did NOT copy the directory structure, I only copied the .dilyb and .a files.

    As for the headers, there should be a boost directory with headers (.h or .hpp) files, and folders for each boost library. As you can specify where XCode (or, more correctly, gcc) will look for them it is not necessary to "install" them. However I chose to copy them to /usr/local/share/boost_1.33.1.

    I hope this helps.

    -- EDIT --
    Be sure to verify the libraries' install names (otool -L xxx.dylib, the first line is the library install name). This is important because it is the name that the loader (ld) will look for when an executable asks for the library at run time. I chose to strip any path info from the install name (consult the otool man pages) and to add /usr/local/lib to the search path.

    I did this a while ago, I hope I'm being clear and that I remember correctly.
     
  8. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #8
    Just a heads up... I finally managed to do this a long time ago. I had written a tutorial on how to do this, here. I hope this proves helpful to anyone that has the same problem as me.
     
  9. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    Another easier method is to just use Mac Ports :).
     
  10. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #10
    Another easier method is to frameworktize the resulting dylibs. I got python script from the Adium SVN repository that converts dynamic libs into a framework. I have to build up a convoluted open source UPnP library (using Xcode) into a framework and that script is very handy.
     
  11. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #11
    I've never attempted that. There are lots of useful libraries out there though that would make fantastic frameworks. May need to look into that.
     
  12. macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #12
    Strange, I don't remember installing boost from source being any more involved than the configure, make, make install sequence.
     
  13. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #13
    I do, unfortunately. I wrote that guide with a help of a member from another forum, which had the exact same problem.
     
  14. macrumors 6502a

    #14
    IIRC, most of boost is implemented as header / template files and requires no linking to libraries. It's only those parts that require OS specific implementations that need linking, so you can actually use a large part of it without going through a build / install process.
     
  15. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #15
    I know. But for example, when I tried using boost::regex, I came into many errors, because boost::regex requires some other files in order to run. Hoever, the includes where in the type
    Code:
    #include <something_else.h> 
    and not like
    Code:
    #include "something_else.h"
    as they should be. That caused me many errors. How am I going to get past that?
     
  16. macrumors newbie

    Joined:
    Mar 15, 2009
    #16
    Mac Ports install of Boost seems not to work

    Hi all,
    I just completed a Mac Ports install of Boost on my Mac (Leopard).

    gcc -v
    Using built-in specs.
    Target: i686-apple-darwin9
    Configured with: /var/tmp/gcc/gcc-5490~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --host=i686-apple-darwin9 --target=i686-apple-darwin9
    Thread model: posix
    gcc version 4.0.1 (Apple Inc. build 5490)

    All I did was:
    > sudo port install boost

    It seemed successful. Took a long time, maybe 45 mins. But now when I do a simple program (first.cpp):

    #include <iostream>
    #include<boost/any.hpp>
    int main() {
    boost::any a(5);
    a = 7.67;
    std:cout<<boost::any_cast<double>(a)<<std::endl;
    }

    and then try to compile:
    g++ -o first first.cpp

    I get this error:
    first.cpp:2:24: error: boost/any.hpp: No such file or directory
    first.cpp: In function ‘int main()’:
    first.cpp:4: error: ‘boost’ has not been declared
    first.cpp:4: error: ‘any’ was not declared in this scope
    first.cpp:4: error: expected `;' before ‘a’
    first.cpp:5: error: ‘a’ was not declared in this scope
    first.cpp:6: error: ‘cout’ was not declared in this scope
    first.cpp:6: error: ‘boost’ has not been declared
    first.cpp:6: error: ‘any_cast’ was not declared in this scope
    first.cpp:6: error: expected primary-expression before ‘double’
    first.cpp:6: error: expected `;' before ‘double’

    Clearly, the compiler does not know where to find the new install of Boost.

    I have seen several links with glib comments like "just link to this..." or "include that..", but I would deeply appreciate if someone would show me EXACTLY how that sample program should look in order to get it to properly include boost and compile.

    When I do "ports info boost", I get back:
    boost @1.37.0 (devel)
    Variants: darwin, darwin_9, debug, docs, graphml, icu, openmpi, python24, python25, python26, st

    Boost provides free portable peer-reviewed C++ libraries. The emphasis is on portable libraries which work well with the C++
    Standard Library.
    Homepage: http://www.boost.org

    Build Dependencies: boost-jam, gmake
    Library Dependencies: zlib, bzip2
    Platforms: darwin
    Maintainers: sancho*****gmail

    I'm convinced that I am simply new and naive and am missing something so simple that an idiot should know it. If someone has successfully installed Boost using Mac Ports, please explain to me precisely why this did not work. I am really stumped. I saw the other "tutorials" but my results do not match.

    The most frustrating part is not understanding how to confirm the install, other than to successfully use Boost in a program. Most tutorials say "here's what you SHOULD see" and don't delve into "here's how to figure out what is wrong if you don't".

    I esp need to use boost/regex.hpp and can't simply #include<boost/regex.hpp>. It's not that easy!

    Best regards, thanks in advance,
    John
     
  17. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #17
    You need to add the header and library search path options in the project settings. Set them to the appropriate folders for the boost library and header files.
     
  18. macrumors newbie

    Joined:
    Mar 15, 2009
    #18
    Thank you for your fast reply. I supposed as much; perhaps there is a g++ config file somewhere? If there is, I don't see it, and I am not finding it by searching the web. I'm sorry to be so terribly dense, but I do not understand what you mean.

    I do not have a "project". I am not programming through a GUI of any kind, and so there is no "settings" location(s). I am simply writing that sample first.cpp file in a directory under /Users/username/Documents/C_stuff using a simple text editor.

    What I apparently don't know how to do is how to set g++ to know where to find the #include<boost> files. I should add that
    g++ -I /path/to/boost -o outfilename file.cpp shows similar errors, but it might be because I don't understand which directory path /path/to/boost is supposed to be.

    There's nothing in the world so humbling as programming.

    Thanks for your patience and help.
    John
     
  19. macrumors newbie

    Joined:
    Mar 15, 2009
    #19
    Solution to the #include problem for boost install on Mac Leopard

    OK, I figured out how to solve part of my problem.

    Using command line g++, there apparently IS no way to set a "path" except to actually explicitly declare it on the command line when compiling, using the "-I" switch.

    In my case, I installed Boost using MacPorts, and it ended up in
    /opt/local/var/macports/software/boost/1.37.0_0+darwin_9/opt/local/include/boost

    However, putting #include<that/entire/11-step/path> into my program did not work, and using
    g++ -I that/whole/path/to/boost gave a "File not found" error.

    Here's the trick: back up one directory on the -I path statement, like so:
    g++ -I /opt/local/var/macports/software/boost/1.37.0_0+darwin_9/opt/local/include/

    (note that it looks the same, except I didn't type "boost" on the end)

    My first.cpp test file (above) then compiled and ran without complaint. It works to name the directory CONTAINING the boost directory, but not the boost directory itself.

    Naming an -I "starting path" for g++ means it will start looking recursively down from that starting path. Since the various boost headers all name their dependencies by saying "boost/whatever", that means if I name "boost" as the starting path, g++ is therefore directed to look in "boost/boost/whatever" and that generates the "File not found" error.

    I then did this in my home directory:

    ln -s /opt/local/var/macports/software/boost/1.37.0_0+darwin_9/opt/local/include/ boost

    This made a symbolic link to that long directory path named simply "boost". That way, I was able to then do this:
    g++ -I boost first.cpp -o first
    and it compiled just fine. I moved the source file down into a sub-directory, and typed
    g++ -I ../boost first.cpp -o first
    and that compiled too, because g++ was able to follow the path to the symbolic link, which it could then follow to the actual boost directory.

    That must seem like the stupidest most obvious thing in the world to most of you fellows. Hope that helps someone else like me sometime.

    Thanks!
    John
     
  20. macrumors newbie

    Joined:
    Jun 20, 2010
    #20
    John !!! Thanks a lot for posting the solution !! It really helped me save a couple of hours !! :)
     
  21. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #21
    That crazy long MacPorts directory can simply be reduced to "/opt/local/include". You're pointing at the uninstalled copy.



    Here's what I do:

    Create a folder in your home directory called "Boost." Into that directory, expand the attached zip file. Also, expand your boost tarball. Edit build_boost.sh to make sure that the boost version specified matches the version you want to build.

    Run the script. It will build for OS X, iPhone, and iPhone sim. You can enable or disable portions of the script as you please. That is, just comment out the iPhone parts if you don't want to use them. Actually, building for iPhone is very slowly getting better.

    Anyway, my favorite part is that when the script is done, it creates a global "BOOST" environment variable that will work in any shell for your user (log out and back in again), and it creates an additional copy that works in Xcode (quit and relaunch). Then, in Xcode, this is what my build settings look like:



    OTHER_LDFLAGS = $(BOOST)/lib/libboost_system-xgcc42-mt-$(BOOST_VERSION)-macosx.a
    HEADER_SEARCH_PATHS = $(inherited) $(BOOST)/include/boost-$(BOOST_VERSION)/
    LIBRARY_SEARCH_PATHS = $(inherited) $(BOOST)/lib
    BOOST_VERSION = 1_41



    When you build this way (using the user-config.jam files), you have far more control over precisely what gets built and how, including what versions of OS X it will run on. As provided, it should build against the 10.5 SDK but still be able to run on 10.4.

    I think.

    YMMV.
     

    Attached Files:

  22. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #22
    I'm not following this.

    Lets say I have the following directory structure

    Code:
    MyProject/
              `-- ThirdParty/
    		`-- Boost/
    		     |-- Build_Boost/
    		     |   |-- build_boost.sh
    		     |   |-- user-config-darwin.jam
    		     |   |-- user-config-iphone.jam
    		     |   `-- user-config-iphonesim.jam
    		     `-- boost_1_43_0/
    		          `-- ...
    
    How do you suggest I go about executing your 'build_boost.sh' so that it finds everything?
     
  23. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #23
    The boost source directory should be in the same directory as the script. This creates a few extra directories. One is named "build." This is where the intermediates go (stuff you have no reason to keep). The other is called "installed." The libraries and the include files are all copied here. Assuming everything works, at this point, you no longer need the original boost source directory.

    Like so:

    Code:
    MyProject/
              `-- ThirdParty/
    		`-- Boost/
    		     |-- Build_Boost/
    		     |   |-- build_boost.sh
    		     |   |-- user-config-darwin.jam
    		     |   |-- user-config-iphone.jam
    		     |   |-- user-config-iphonesim.jam
    		     |   |-- boost_1_43_0/
                         |   |-- build/
                         |   |-- installed/
    		     |   |   |-- include/
                         |   |   |-- lib/
                         `-- ...
    
    Technically, this is independent of your single project and could potentially be used with multiple projects, so it would make more sense outside of the "MyProject" directory, but ultimately, that's just a detail.



    What the script does is cd into the boost source code directory. Then it runs bjam with each of the three jam files. Each of these bjam commands culminates in installing the libraries and headers in to the "installed" directory. After all three have finished, user-wide global environment variables are set specifying for you that this is where your Boost installation is located. So far as I'm aware, this is not a Boost standard. It's just what I picked that made sense to me. There are two of these variables. One is visible in any shell. The other is visible to Xcode. Both are named "BOOST" and point to the same place for consistency, but they are technically distinct.
     
  24. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #24
    I had all that figured out before the question. The problem is that your directions:

    result in the directory structure I show above - which of course doesn't work.


    Finding accurate instructions for getting boost compiled and functional for iPhone development is hard to come by. Yours is the most direct and useful I've seen but could be even more useful with a slight correction.

    Thank you for taking both the time and trouble to post your script and config files.

    EDIT: Oh, and the reason for my placement of boost is that it follows the conventions of a former employer whose edutainment product I'm try to update and get working on the iPad. That is any dependent third party libraries must be placed within the working structure of the project for inclusion in the source control system.
     
  25. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #25
    Does it actually work for iPhone? I've not had a chance to try it myself, but this is as far as I've been able to figure out about building it--with varying results. There are no instructions out there that I've found. I had to read the actual jam files, and they change dramatically between boost releases.

    I've also done the same thing for SCM.

    About the directions, I reckon I forgot that the zip expands into a directory. Oops.
     

Share This Page