当我尝试链接以下代码时,我遇到了问题: TEST.CPP:
#include "Singleton.h"
#include "SingletonManager.h"
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
const size_t MAXTHREADS = 4;
// class holds the startuped threads + data
class test_threads
{
public:
static boost::thread * m_thread[MAXTHREADS];
static boost::thread_group m_thrgroup;
static boost::barrier m_barrier;
static size_t mpos;
virtual void pipe() =0;
};
boost::thread * test_threads::m_thread[MAXTHREADS];
boost::thread_group test_threads::m_thrgroup;
boost::barrier test_threads::m_barrier(MAXTHREADS+1); // launched threads + main
size_t test_threads::mpos = 0;
// the singleton class which we instantiate few times.
template<size_t _id>
class FastBar : public Singleton<FastBar<_id>, SingletonAllocator<FastBar<_id>, AllocationType::Static> >
{
public:
FastBar()
{}
~FastBar() throw()
{}
void pipe()
{
std::cout<<"|";
}
private:
static const size_t id = _id;
};
// recursive template to instantiate N FastBars to run in threads
template<size_t i>
class initFastBar
{
public:
initFastBar<i-1> compileloop;
typedef initFastBar<i> initbar;
static void __start()
{
test_threads::m_barrier.wait();
FastBar<i>::instance().pipe();
}
initFastBar()
{
test_threads::m_thread[test_threads::mpos] = new boost::thread(initFastBar<i>::__start);
test_threads::m_thrgroup.add_thread(test_threads::m_thread[test_threads::mpos]);
test_threads::mpos++;
}
};
// end recurse
template<>
class initFastBar<0>
{
typedef initFastBar<0> compileloop;
};
void execute_ThreadRefencing_test()
{
// startup 16 threads
test_threads::mpos = 0;
initFastBar<MAXTHREADS> init;
// syncronize
test_threads::m_barrier.wait();
// wait for to complete the tasks.
test_threads::m_thrgroup.join_all();
SingletonManager::instance()->release_all();
}
结果是相当神秘的链接器错误,但它告诉我“我没有链接agaist libboost_thread.a”:
Linking console executable: build\Singletons_Debug_i686.exe
build/debug/i686/obj/test_threading.o:test_threading.cpp:(.text$_ZN5boost12thread_groupD1Ev[boost::thread_group::~thread_group()]+0x77): undefined reference to `_imp___ZN5boost6threadD1Ev'
build/debug/i686/obj/test_threading.o:test_threading.cpp:(.text$_ZN5boost12thread_group8join_allEv[boost::thread_group::join_all()]+0x87): undefined reference to `_imp___ZN5boost6thread4joinEv'
build/debug/i686/obj/test_threading.o:test_threading.cpp:(.text$_ZN5boost6detail19basic_cv_list_entry4waitENS0_7timeoutE[boost::detail::basic_cv_list_entry::wait(boost::detail::timeout)]+0x7d): undefined reference to `_imp___ZN5boost11this_thread18interruptible_waitEPvNS_6detail7timeoutE'
build/debug/i686/obj/test_threading.o:test_threading.cpp:(.text$_ZN5boost6threadC1IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)]+0x58): undefined reference to `_imp___ZN5boost6thread12start_threadEv'
collect2: ld returned 1 exit status
但我99.999%肯定我正在连接boost线程库。它没有丢失,因为使用它的其他代码链接没问题。 execute_hreadRefencing_test()函数从main()调用,它在main.cpp中具有外部链接作为decrared。 如果-lboost_thread标志与MinGW-W64仅部分用于程序,如何链接程序? :( 我甚至在程序中还有其他两个类似的递归模板实例,它们链接得很好。