1

I gave up on linking to Boost Program Options in Xcode, because I couldn't get it to work. I figured out how to get the headers to be recognized, but I'm not familiar enough with how to set up the paths to the libraries to link to in Xcode.

I used Boost and Eclipse in college last year, so I thought I'd be able to sort it out a little easier if I just used Eclipse for this project. My program looks a lot like the example they provide (first.cpp). If you feel the need to look at the source I have it loaded on github.

Don't worry, I have Boost installed in /usr/local/boost:

new-host-2:$ ls | grep program_options
libboost_program_options.a
libboost_program_options.dylib
new-host-2:$ pwd
/usr/local/boost_1_49_0/stage/lib

I have my Project settings configured for Boost in the following ways under Project > Properties > C/C++ General > Paths & Symbols: Includes: /usr/local/boost_1_49_0/ Libraries: boost_program_options Library Paths: /usr/local/boost_1_49_0/stage/lib

Based on the Eclipse console, you would think it linked properly, as I get this output last:

Building target: FizzBuzz
Invoking: Cross G++ Linker
g++ -L/usr/local/boost_1_49_0/stage/lib -o "FizzBuzz"  ./Fizzbuzz.o ./main.o -lboost_program_options
Finished building target: FizzBuzz


**** Build Finished ****

However, when I go to run my program, I get a dialog box that says: "Unable to Launch" The selection could not be launched, and there are no recent launches.

I tried running it from the command line, with no luck:

new-host-2:$ ./FizzBuzz 
dyld: Library not loaded: libboost_program_options.dylib
  Referenced from: /Users/per001/Documents/workspace/FizzBuzz/Debug/./FizzBuzz
  Reason: image not found
Trace/BPT trap: 5

How do I set this up to link properly in Eclipse? Or even better, Xcode?

paulrehkugler
  • 3,241
  • 24
  • 45
  • Do you have any particular reason compiling Boost.ProgramOptions as a dynamic library? If not then you could follow my [static compilation guide](http://stackoverflow.com/questions/4755712/how-to-build-boost-1-45-universal-binaries/4894399#4894399), and possibly save yourself a lot of trouble. – Mankarse May 08 '12 at 08:12

4 Answers4

3

I have managed to link and run your code from the specified github repository.

It was bit of a process as I have proceeded a bit differently.

First I have reinstalled on my mac boost as follows.

  1. I have installed the latest macport from here.

  2. Then I have installed the latest command line tools as I have xcode 4.3 and the command line tools are no longer shipped with it as default from here.

  3. After that I have installed boost as follows:

    sudo port install boost

    Here I have had some issues because Xcode is in a different place than previously so I had to execute the following commands to fix this:

    sudo xcode-select -switch /Applications/Xcode.app

    To verify that this has done the job I have executed the following:

    xcode-select -print-path

    Where I have the expected result: /Applications/Xcode.app

    The location of the files are as follows after this:

    macpro:local ervinbosenbacher$ locate libboost_program_options.a
    /opt/local/lib/libboost_program_options.a
    
    macpro:local ervinbosenbacher$ locate program_options
    /opt/local/include/boost/program_options
    /opt/local/include/boost/program_options/cmdline.hpp
    /opt/local/include/boost/program_options/config.hpp
    /opt/local/include/boost/program_options/detail
    ...
    
  4. Following this I have launched XCode and created a brand new project and included your code files in the project.

  5. Have modified the settings as follows:

    Build Settings->All->Search Paths->Always Search User Paths: Yes.
    
    Build Settings->All->Search Paths->Library Search Paths->Debug->Any Architecture|Any SDK:/opt/local/lib
    Build Settings->All->Search Paths->Library Search Paths->Release->Any Architecture|Any SDK:/opt/local/lib
    
    Build Settings->All->Search Paths->User Header Search Paths->Debug->Any Architecture|Any SDK:/opt/local/include
    Build Settings->All->Search Paths->User Header Search Paths->Release->Any Architecture|Any SDK:/opt/local/include
    
    Build Settings->All->Linking->Other Linking Flags->Debug->Any Architecture|Any SDK: -lboost_program_options
    Build Settings->All->Linking->Other Linking Flags->Release->Any Architecture|Any SDK: -lboost_program_options

Then I have successfully build and run the app from Xcode:

Allowed options:
        --help                produce help message
        --begnum arg          set beginning number
        --endnum arg          set ending number
        --fizznum arg         set fizznum (default: 3)
        --buzznum arg         set buzznum (default: 5)
        --fizzword arg        set fizzword (default "Fizz"
        --buzzword arg        set buzzword (default "Buzz"
ervinbosenbacher
  • 1,720
  • 13
  • 16
  • Wow - thanks for all the hard work! Let me try that tomorrow! – paulrehkugler May 07 '12 at 03:52
  • Np :) let me know how you getting on. – ervinbosenbacher May 07 '12 at 11:40
  • Hmm... Looks like I'm having issues `dyld: Library not loaded: libboost_program_options.dylib Referenced from: /Users/per001/Library/Developer/Xcode/DerivedData/FizzBuzz-dqvwpktqcdxeeqaupfttomlnjkiq/Build/Products/Debug/FizzBuzz Reason: image not found (lldb) ` – paulrehkugler May 08 '12 at 01:57
  • I'll try uninstalling and reinstalling boost using macports to see if that makes things work better. I just set the search paths and flags to point at what I already have installed. And I at least got further than I did before. – paulrehkugler May 08 '12 at 01:58
  • I was linking against the static libraries .a. Try that. Or try to locate the .dylib libraries and change your settings in xcode. Later today I will test that. – ervinbosenbacher May 08 '12 at 04:49
  • I was thinking, there might be something with your settings. Why do you want the mac dynamic libraries. Can you create a C++ project from scratch in xcode, just as a test, and put your code files in there? – ervinbosenbacher May 08 '12 at 06:34
  • I got the error from a new project, and the .dylib libraries are in the same place as the .a files `$ ls libboost_prog* libboost_program_options.a libboost_program_options.dylib` – paulrehkugler May 10 '12 at 01:58
  • I'll try to reproduce this but extremely annoying I must say. – ervinbosenbacher May 10 '12 at 08:01
0

Here's how I link Boost program options on Linux using plain g++ from a shell:

g++ -static main.cpp -o program \
-I/usr/local/include/boost/ \
/usr/local/lib/libboost_program_options-s.a

And in the cpp source code, I have this include:

#include <boost/program_options.hpp>

I'm not sure how to add paths to Eclipse in Xcode, but you may try plain g++ from a shell just to see if that compiles and links correctly and then go from there.

  • I tried that, but got an error: `new-host-2:class-based-solution per001$ g++ -static -o main -I /usr/local/boost_1_49_0/ /usr/local/boost_1_49_0/stage/lib/libboost_program_options.a ld: library not found for -lcrt0.o collect2: ld returned 1 exit status` – paulrehkugler Apr 26 '12 at 01:20
0

Adjust the path to the dyn lib using install_name_tool:
$ otool -L /path/to/your/binary (will list shared libs dep)
$ install_name_tool -change libboost_foo.dylib /path/to/boost/stage/lib/libboost_foo.dylib /path/to/your/binary (will change the location of the dyn lib)
$ otool -L /path/to/your/binary
$ /path/to/your/binary

  • The reason I provided this answer is so that you can run it; Your linker can't find the shared library and so you can use `install_name_tool` to fix that. Specifically this: `new-host-2:$ ./FizzBuzz dyld: Library not loaded: libboost_program_options.dylib Referenced from: /Users/per001/Documents/workspace/FizzBuzz/Debug/./FizzBuzz Reason: image not found Trace/BPT trap: 5` – Ishwor Gurung May 08 '12 at 05:01
0

Did you try

export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/boost_1_49_0/stage/lib

before lauching Eclipse within the same terminal and see if it fix the bug ?

Benoit.

Quanteek
  • 254
  • 2
  • 11