Thread: Code::Blocks, g++, Boost

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Code::Blocks, g++, Boost

    Hey,
    I'm trying to get the Boost libraries to work on my Ubuntu system. I have code::blocks configured to use the default GNU compiler (for C++), and I linked to the pertinent .a files. Doing this gives me undefined function calls, etc. If I link to the dynamic libraries (.so), it all goes smoothly. Is there any way to link to the static libraries only?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I haven't gotten around to trying Boost yet, but you should certainly be able to get a quick answer on the C-B forum:

    http://forums.codeblocks.org/index.php

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Thanks. I'll give it a try.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Since the apt repository seems to only have the dynamically linked version, I am assuming you have compiled your own copy of Boost?

    Did you follow the instructions on the Boost website? It seems to cover static builds, too.
    http://www.boost.org/doc/libs/1_37_0...-variants.html

    Also, what part of Boost are you trying to use? IIRC most of Boost is header-only, and no linking is needed (with exceptions like Boost::thread).

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    one explanation would be that the static libs depends on other static libs themself (static libs are not yet linked, so all symbols must be defined while linking the application). for dynamic libs this may not happen because they are already linked/symbols are resolved.

    on the other side, I'm not aware on any non-header-only boost library which depends on another one (but don't give much on my awareness)

    to check if your build procedure is correct you couls study this reference:
    http://www.dwheeler.com/program-libr...WTO/index.html

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I didn't know that the apt repository had only dynamically-linked versions. The .a files are in there (perhaps this is a naive conclusion of mine). Well, for that matter, no I am not using my own build. I tried to build the thing the other day and had nothing but problems. That's why I thought I'd settle for the package.

    So what are the static object files for, in the lib folder?

    Edit: Thanks for all of the responses.
    Last edited by CodeMonkey; 12-14-2008 at 05:33 PM. Reason: Thanking
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I didn't know that the apt repository had only dynamically-linked versions. The .a files are in there (perhaps this is a naive conclusion of mine).
    Hmm I may be wrong on that. Not sure.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Can you show us your command line and an example of the resulting errors?

    As for dependencies: Regex can optionally depend on ICU, MPI depends on an MPI implementation, some components of IOStream depend on zlib and libbzip2. Some Graph parts also have dependencies.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Here we are. I used some of boost::thread's simpler constructs in my code.
    The only command-line argument I can see is -fexceptions, and various optimizations. The build cries this:
    Code:
    -------------- Build: Release in console1 ---------------
    
    Compiling: main.cpp
    [. . . . . .] 
    Linking console executable: bin/Release/console1
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(mutex.o): In function `boost::try_mutex::do_trylock()':
    (.text+0x463): undefined reference to `pthread_mutex_trylock'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread::join()':
    (.text+0x1b8): undefined reference to `pthread_join'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread_group::~thread_group()':
    (.text+0x3a5): undefined reference to `pthread_detach'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread_group::~thread_group()':
    (.text+0x435): undefined reference to `pthread_detach'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread_group::join_all()':
    (.text+0x4c1): undefined reference to `pthread_join'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)':
    (.text+0x75c): undefined reference to `pthread_create'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)':
    (.text+0x8bc): undefined reference to `pthread_create'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread_group::create_thread(boost::function0<void, std::allocator<boost::function_base> > const&)':
    (.text+0xa28): undefined reference to `pthread_detach'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread::~thread()':
    (.text+0x217): undefined reference to `pthread_detach'
    ../../../../../usr/lib/libboost_thread-gcc42-mt-1_34_1.a(thread.o): In function `boost::thread::~thread()':
    (.text+0x237): undefined reference to `pthread_detach'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 2 seconds)
    10 errors, 10 warnings
    So do the libraries have functions that themselves link to the runtime files? Does this all get resolved during the build?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Pass -pthread to GCC. Possibly also -lpthread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    No change.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No error about not finding libpthread? Very strange.

    At this point I recommend you write to the boost-users mailing list for help.

    Edit: Wait. Can you post the exact command line? On Linux, the order of arguments matters.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Doing away with code::blocks, I find that
    Code:
    $ g++ main.cpp -lpthread -o main
    /tmp/cc9vkG17.o: In function `main':
    main.cpp:(.text+0x512): undefined reference to `boost::barrier::barrier(unsigned int)'
    main.cpp:(.text+0x51d): undefined reference to `boost::thread_group::thread_group()'
    main.cpp:(.text+0x60e): undefined reference to `boost::thread_group::create_thread(boost::function0<void, std::allocator<boost::function_base> > const&)'
    main.cpp:(.text+0x669): undefined reference to `boost::thread_group::join_all()'
    main.cpp:(.text+0xacd): undefined reference to `boost::thread_group::~thread_group()'
    main.cpp:(.text+0xae6): undefined reference to `boost::thread_group::~thread_group()'
    main.cpp:(.text+0xafc): undefined reference to `boost::barrier::~barrier()'
    main.cpp:(.text+0xb23): undefined reference to `boost::barrier::~barrier()'
    /tmp/cc9vkG17.o: In function `task::operator()()':
    main.cpp:(.text._ZN4taskclEv[task::operator()()]+0x91): undefined reference to `boost::barrier::wait()'
    main.cpp:(.text._ZN4taskclEv[task::operator()()]+0xf4): undefined reference to `boost::barrier::wait()'
    collect2: ld returned 1 exit status
    And I've checked again -- 'libpthread.a' is in /usr/lib/

    Fortunately, this works:
    Code:
    $ g++ main.cpp -lboost_thread -o main
    So I'm not exactly drowning. But I wonder why -lpthread doesn't cut it. I also wonder why my telling code::blocks to link to libboost_thread.a didn't work either.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    pthread is the underlying threading library. I expected it to be implicitly included, and apparently it is, if you call g++ from the command line.
    boost_thread is the Boost threading library. Of course the build fails if you omit it.

    Seems like Code::Blocks doesn't implicitly include pthread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Whoops. Thanks, CornedBee.

    To beat this horse a little further, to what could I attribute the fact that my program has no performance gains, under any conditions, using two threads over one on Linux, when it does gain for large tasks on Vista? Might my Windows boost::thread libraries be built more optimally, or is it the nature of the kernels? Compilers?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. library issues in CodeBlocks
    By everlearnin in forum C++ Programming
    Replies: 0
    Last Post: 06-02-2009, 08:44 PM
  2. Codeblocks crashes linux?
    By Shakti in forum Tech Board
    Replies: 1
    Last Post: 03-25-2009, 07:26 AM
  3. Boost Auto-Linking
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:11 AM
  4. Replies: 2
    Last Post: 12-12-2007, 06:45 AM
  5. Integrating Boost with STLPort
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-11-2006, 06:49 AM