LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2006, 05:25 AM   #1
juricheguevara
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Rep: Reputation: 0
boost library


hi..
I've installed Boost library from an rpm (Mandriva.2006) and now I want to use Boost.Thread..
I've tried to compile an example file but I've received many errors from, I think, the linker. How can I link boost shared library? They are in /usr/include/boost..
thanks
 
Old 04-13-2006, 05:45 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
What are the errors? Post your compiler/linker output, and possibly your program's source code (or just the section with which you're having trouble) please.

Last edited by scuzzman; 04-13-2006 at 05:46 AM.
 
Old 04-13-2006, 12:32 PM   #3
juricheguevara
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Original Poster
Rep: Reputation: 0
source code:
Code:
#include <iostream>
#include <vector>
#include <boost/utility.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>

class bounded_buffer : private boost::noncopyable
{
public:
    typedef boost::mutex::scoped_lock lock;

    bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { }

    void send (int m) {
        lock lk(monitor);
        while (buffered == circular_buf.size())
            buffer_not_full.wait(lk);
        circular_buf[end] = m;
        end = (end+1) % circular_buf.size();
        ++buffered;
        buffer_not_empty.notify_one();
    }
    int receive() {
      lock lk(monitor);
        while (buffered == 0)
            buffer_not_empty.wait(lk);
        int i = circular_buf[begin];
        begin = (begin+1) % circular_buf.size();
        --buffered;
        buffer_not_full.notify_one();
        return i;
    }

private:
    int begin, end, buffered;
    std::vector<int> circular_buf;
    boost::condition buffer_not_full, buffer_not_empty;
    boost::mutex monitor;
};

bounded_buffer buf(2);

void sender() {
    int n = 0;
    while (n < 100) {
        buf.send(n);
        std::cout << "sent: " << n << std::endl;
        ++n;
    }
    buf.send(-1);
}

void receiver() {
    int n;
    do {
        n = buf.receive();
        std::cout << "received: " << n << std::endl;
    } while (n != -1); // -1 indicates end of buffer
}

int main(int, char*[])
{
    boost::thread thrd1(&sender);
    boost::thread thrd2(&receiver);
    thrd1.join();
    thrd2.join();
    return 0;
}

the command is: g++ condition.cpp -o condition

Code:
/home/juri/tmp/ccsbveqQ.o: In function `main':
condition.cpp:(.text+0x3f): undefined reference to `boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)'
condition.cpp:(.text+0x9c): undefined reference to `boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)'
condition.cpp:(.text+0xd6): undefined reference to `boost::thread::join()'
condition.cpp:(.text+0xe5): undefined reference to `boost::thread::join()'
condition.cpp:(.text+0xfb): undefined reference to `boost::thread::~thread()'
condition.cpp:(.text+0x112): undefined reference to `boost::thread::~thread()'
condition.cpp:(.text+0x126): undefined reference to `boost::thread::~thread()'
condition.cpp:(.text+0x143): undefined reference to `boost::thread::~thread()'
/home/juri/tmp/ccsbveqQ.o: In function `boost::condition::condition()':
condition.cpp:(.gnu.linkonce.t._ZN5boost9conditionC1Ev[boost::condition::condition()]+0x21): undefined reference to `boost::detail::condition_impl::condition_impl()'
/home/juri/tmp/ccsbveqQ.o: In function `boost::condition::~condition()':
condition.cpp:(.gnu.linkonce.t._ZN5boost9conditionD1Ev[boost::condition::~condition()]+0x12): undefined reference to `boost::detail::condition_impl::~condition_impl()'
/home/juri/tmp/ccsbveqQ.o: In function `boost::condition::notify_one()':
condition.cpp:(.gnu.linkonce.t._ZN5boost9condition10notify_oneEv[boost::condition::notify_one()]+0x11): undefined reference to `boost::detail::condition_impl::notify_one()'
/home/juri/tmp/ccsbveqQ.o: In function `boost::detail::thread::lock_ops<boost::mutex>::lock(boost::mutex&)':
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread8lock_opsINS_5mutexEE4lockERS3_[boost::detail::thread::lock_ops<boost::mutex>::lock(boost::mutex&)]+0xd): undefined reference to `boost::mutex::do_lock()'
/home/juri/tmp/ccsbveqQ.o: In function `boost::detail::thread::scoped_lock<boost::mutex>::lock()':
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread11scoped_lockINS_5mutexEE4lockEv[boost::detail::thread::scoped_lock<boost::mutex>::lock()]+0x29): undefined reference to `boost::lock_error::lock_error()'
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread11scoped_lockINS_5mutexEE4lockEv[boost::detail::thread::scoped_lock<boost::mutex>::lock()]+0x34): undefined reference to `boost::lock_error::~lock_error()'
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread11scoped_lockINS_5mutexEE4lockEv[boost::detail::thread::scoped_lock<boost::mutex>::lock()]+0x39): undefined reference to `typeinfo for boost::lock_error'
/home/juri/tmp/ccsbveqQ.o: In function `boost::detail::thread::lock_ops<boost::mutex>::unlock(boost::mutex&)':
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread8lock_opsINS_5mutexEE6unlockERS3_[boost::detail::thread::lock_ops<boost::mutex>::unlock(boost::mutex&)]+0xd): undefined reference to `boost::mutex::do_unlock()'
/home/juri/tmp/ccsbveqQ.o: In function `boost::detail::thread::scoped_lock<boost::mutex>::unlock()':
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread11scoped_lockINS_5mutexEE6unlockEv[boost::detail::thread::scoped_lock<boost::mutex>::unlock()]+0x2c): undefined reference to `boost::lock_error::lock_error()'
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread11scoped_lockINS_5mutexEE6unlockEv[boost::detail::thread::scoped_lock<boost::mutex>::unlock()]+0x37): undefined reference to `boost::lock_error::~lock_error()'
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread11scoped_lockINS_5mutexEE6unlockEv[boost::detail::thread::scoped_lock<boost::mutex>::unlock()]+0x3c): undefined reference to `typeinfo for boost::lock_error'
/home/juri/tmp/ccsbveqQ.o: In function `boost::detail::thread::lock_ops<boost::mutex>::unlock(boost::mutex&, boost::mutex::cv_state&)':
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread8lock_opsINS_5mutexEE6unlockERS3_RNS3_8cv_stateE[boost::detail::thread::lock_ops<boost::mutex>::unlock(boost::mutex&, boost::mutex::cv_state&)]+0x10): undefined reference to `boost::mutex::do_unlock(boost::mutex::cv_state&)'
/home/juri/tmp/ccsbveqQ.o: In function `boost::detail::thread::lock_ops<boost::mutex>::lock(boost::mutex&, boost::mutex::cv_state&)':
condition.cpp:(.gnu.linkonce.t._ZN5boost6detail6thread8lock_opsINS_5mutexEE4lockERS3_RNS3_8cv_stateE[boost::detail::thread::lock_ops<boost::mutex>::lock(boost::mutex&, boost::mutex::cv_state&)]+0x10): undefined reference to `boost::mutex::do_lock(boost::mutex::cv_state&)'
/home/juri/tmp/ccsbveqQ.o: In function `void boost::condition::do_wait<boost::mutex>(boost::mutex&)':
condition.cpp:(.gnu.linkonce.t._ZN5boost9condition7do_waitINS_5mutexEEEvRT_[void boost::condition::do_wait<boost::mutex>(boost::mutex&)]+0x27): undefined reference to `boost::detail::condition_impl::do_wait(pthread_mutex_t*)'
/home/juri/tmp/ccsbveqQ.o: In function `void boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex> >(boost::detail::thread::scoped_lock<boost::mutex>&)':
condition.cpp:(.gnu.linkonce.t._ZN5boost9condition4waitINS_6detail6thread11scoped_lockINS_5mutexEEEEEvRT_[void boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex> >(boost::detail::thread::scoped_lock<boost::mutex>&)]+0x36): undefined reference to `boost::lock_error::lock_error()'
condition.cpp:(.gnu.linkonce.t._ZN5boost9condition4waitINS_6detail6thread11scoped_lockINS_5mutexEEEEEvRT_[void boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex> >(boost::detail::thread::scoped_lock<boost::mutex>&)]+0x41): undefined reference to `boost::lock_error::~lock_error()'
condition.cpp:(.gnu.linkonce.t._ZN5boost9condition4waitINS_6detail6thread11scoped_lockINS_5mutexEEEEEvRT_[void boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex> >(boost::detail::thread::scoped_lock<boost::mutex>&)]+0x46): undefined reference to `typeinfo for boost::lock_error'
/home/juri/tmp/ccsbveqQ.o: In function `bounded_buffer::bounded_buffer(int)':
condition.cpp:(.gnu.linkonce.t._ZN14bounded_bufferC1Ei[bounded_buffer::bounded_buffer(int)]+0x78): undefined reference to `boost::mutex::mutex()'
/home/juri/tmp/ccsbveqQ.o: In function `bounded_buffer::~bounded_buffer()':
condition.cpp:(.gnu.linkonce.t._ZN14bounded_bufferD1Ev[bounded_buffer::~bounded_buffer()]+0x12): undefined reference to `boost::mutex::~mutex()'
collect2: ld returned 1 exit status
thanks
 
Old 06-09-2006, 03:50 AM   #4
turkman
LQ Newbie
 
Registered: Jun 2006
Posts: 2

Rep: Reputation: 0
Smile



I also have such a problem. I am using cygwin enviroment.
The library path, as I think, is correct, since if you change the librayname like

g++ -lboost-whats-going-on y.cpp -o y.exe
then the following reported
..../ld cannot find -lboost-whats-going-on
after I corrected it as
g++ -L/usr/lib -boost-program_options_gcc-mt-s-1_33_1 y.cpp -o y.exe

then the name mangling problem found because collect2 cannot find all the boost library
I use
ar -t /usr/lib/libboost-program_options_gcc-mt-s-1_33_1.a
And it shows meaningful results, whereas g++ seemed to do name mangling on my y.cpp
I guess C Linkage has to be applied in y.cpp, but I dont know how...

Best Regards,
 
Old 07-01-2006, 04:54 AM   #5
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
Code:
SOURCE := threadtest.cpp
PROG := threadsample

INCLUDES := -I/usr/local/include/boost-1_33_1
LIBPATH := -L/usr/local/lib
LIBS := -lboost_thread-gcc-mt-d

all: $(PROG)

$(PROG): threadtest.o
	cc -o $(PROG) threadtest.o $(LIBPATH) $(LIBS)

threadtest.o: $(SOURCE)
	cc -c $(SOURCE) $(INCLUDES)
Can you try the following makefile? For my version of boost it worked.
After building the exe, make sure to set the LD_LIBRARY_PATH.
 
Old 07-04-2006, 10:53 AM   #6
turkman
LQ Newbie
 
Registered: Jun 2006
Posts: 2

Rep: Reputation: 0
Hi,

I still get the error report shown in this mail series.

I just
(1) download cygwin, gcc, boost lib from cygwin
(2) copy the threadtest.cpp
(3) copy the makefile
(4) modify includes to /usr/include and libs to /usr/lib because cygwin put boost to those directories
(5) modify to -lboost_thread-gcc-mt-s because i cannot find libboost_thread-gcc-mt-d.a
(6) set LD_LIBRARY_PATH to /usr/lib and verify it through echoing in the shell

Then, should i get a copy of source code of bin utility to debug "collect2" behavior???
Best Regards,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with boost nathacof Programming 10 11-04-2005 12:56 PM
g++ and boost problem mixx Programming 1 07-20-2005 12:53 PM
how to install Boost 1.3.20 chutsu Linux - Software 0 11-10-2004 02:22 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
Boost threading brianvdc Programming 1 10-16-2003 04:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration