Sunday, June 08, 2008

boost with cmake

First, make sure you are using cmake 2.6, as the boost module is much better and I've actually managed to use it on both Linux and Windows.

cmake_minimum_required( VERSION 2.6 FATAL_ERROR )


Next check for the boost installation, version a/o some libraries:

# search for Boost version 1.34
find_package( Boost 1.34 COMPONENTS regex )


Last piece to the puzzle: specify the include and lib path and link as needed:


link_directories ( ${Boost_LIBRARY_DIRS} )
include_directories ( ${Boost_INCLUDE_DIRS} )

add_executable (
helloworld
helloworld.cc
)

target_link_libraries (
helloworld
${Boost_LIBRARIES}
)


On Linux, nothing else is needed to compile your boost app. On Windows, I had to extend the path to include the boost root and lib paths. If you are using MSys, just add something like this to your .profile, for example:


BOOST_ROOT=/e/programs/boost_1_35_0
BOOST_LIBRARYDIR=$BOOST_ROOT/stage/lib

export PATH=$PATH:$BOOST_ROOT:$BOOST_LIBRARYDIR


If you need more info, just read the help:

$ cmake --help-module FindBoost

13 comments:

  1. Good job, it helps me a lot. Thanks
    French Guys love you !

    ReplyDelete
  2. Awsome post... this post taught me how to make a cmake project...
    and further...
    successfully changed the Qgis cmake project to use boost::threads.

    Thanks very much.

    ReplyDelete
  3. I hace one doubt... if u can help me out please...

    in

    link_directories ( ${Boost_LIBRARY_DIRS} )
    include_directories ( ${Boost_INCLUDE_DIRS} )

    add_executable (
    helloworld
    helloworld.cc
    )

    target_link_libraries (
    helloworld
    ${Boost_LIBRARIES}
    )



    how are the vars Boost_LIBRARY_DIRS,Boost_INCLUDE_DIRS and Boost_LIBRARIES selected??

    if i change Boost_LIBRARY_DIRS and Boost_INCLUDE_DIRS to Bst_LIBRARY_DIRS and Bst_INCLUDE_DIRS..and keet the Boost_LIBRARIES as such...stil all goes fine ..im able to configure ,make and link fine..isnt there any relation b/w these variables??i think there must be some link definitely...

    Thanks.

    ReplyDelete
  4. Each of these variables has its own purpose.

    The INCLUDE_DIRS tells the compiler where the headers are and will end up as an -I argument. For example, if you have some headers stored under /my/path/to/headers/foo/bar.h and set the INCLUDE_DIRS to -I /my/path/to/headers, your code can use something like "#include <foo/bar.h>"

    The LIBRARIES_DIRS tells the linker where the libs are (and will end up as an -L argument for the linker), while the LIBRARIES tells the linker what libraries to link. For example, LIBRARIES_DIRS could be set to -L /my/path/to/libraries, while the LIBRARIES could be set to -lfoo -lbar.

    Depending on your platform and installation, some of these variables might not be needed (that is, the headers or libs are found even when the variables are missing or set wrong). This might explain why the code still compiles. For example, if the libs are located under /usr/lib, they will be found by the linker even when you set the LIBRARIES_DIR to something like /my/inexistent/dir.

    ReplyDelete
  5. Well...by changing variables what i meant was not changing the path that they point to but the name of the variable itself...
    can i choose these variable names arbitrarily? like instead of:
    target_link_libraries (
    helloworld
    ${Boost_LIBRARIES}
    )
    i write:
    target_link_libraries (
    helloworld
    ${B_LIBRARIES}
    )

    would this make any difference?
    i think it should because the Boost_LIBRARIES variable would be set by cmake upon finding the libraries names that i want my exec to get linked to in my code,i.e the components that i mentioned like regex thread etc..

    whats the real situation?

    and Thanks for the time and knowledge sharing.. :)

    ReplyDelete
  6. It makes a difference and using the arbitrary B_LIBRARIES should fail.

    The simplest way to debug CMake is to add message() calls. For example:

    message(STATUS "Boost_LIBRARIES=${Boost_LIBRARIES}")
    message(STATUS "B_LIBRARIES=${B_LIBRARIES}")

    ReplyDelete
  7. Nice post thanks.
    I wondered how can I use different BOOSt versions?
    I have installed 1.35 and 1.38.
    The command
    find_package( Boost 1.35 COMPONENTS regex )
    finds always 1.35.
    how set search dirs for find_package?
    Thanks.
    Arman.

    ReplyDelete
  8. Have you set BOOST_ROOT shell variable to point to your 1.38 Boost? That should have worked.

    ReplyDelete
  9. tnx!!

    Saved me a lot of time. :)

    But something still confusing for me:

    # When I use
    set(Boost_USE_MULTITHREADED ON)
    find_package( Boost 1.41.0 COMPONENTS date_time-mt regex-mt thread-mt )

    # The resule is
    -- Found the following Boost libraries:
    -- date_time-mt
    -- regex-mt
    -- thread-mt


    # And when I use
    set(Boost_USE_MULTITHREADED ON)
    find_package( Boost 1.41.0 COMPONENTS date_time regex thread )

    # The resule is
    -- Found the following Boost libraries:
    -- date_time
    -- regex
    -- thread


    In 2nd case does Boost_USE_MULTITHREADED ON really matters?? or I must use -mt postfix with this option (like 1st case)??

    ReplyDelete
  10. Ha!!

    Found the answer:

    It's always multithreaded. The 2nd case is OK.

    babaei@mbp13x17 $ldd 3rr0r.fcgi
    linux-gate.so.1 => (0xb771d000)
    libfastcgipp.so => /usr/local/lib/libfastcgipp.so (0xb768b000)
    libboost_date_time-mt-1_42.so.1.42.0 => /usr/lib/libboost_date_time-mt-1_42.so.1.42.0 (0xb7654000)
    libboost_regex-mt-1_42.so.1.42.0 => /usr/lib/libboost_regex-mt-1_42.so.1.42.0 (0xb7580000)
    libboost_thread-mt-1_42.so.1.42.0 => /usr/lib/libboost_thread-mt-1_42.so.1.42.0 (0xb756b000)
    libstdc++.so.6 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/libstdc++.so.6 (0xb7479000)
    libm.so.6 => /lib/libm.so.6 (0xb7453000)
    libgcc_s.so.1 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/libgcc_s.so.1 (0xb7437000)
    libc.so.6 => /lib/libc.so.6 (0xb72f0000)
    libpthread.so.0 => /lib/libpthread.so.0 (0xb72d7000)
    librt.so.1 => /lib/librt.so.1 (0xb72cd000)
    /lib/ld-linux.so.2 (0xb7701000)

    ReplyDelete
  11. I was banging my head on the wall for two days trying to get cmake to find boost and then I came across your post, and it worked like a charm. Thank you for posting this.

    ReplyDelete
  12. Thanks for the cmake --help-module FindBoost bit, that helped me find out why it wasn't finding my libs.

    ReplyDelete
  13. thanx :) its help me for start programming with kdevelop

    ReplyDelete