操作系统:Win7
IDE:Visual Studio 2010
提升版本:1.47
我是Boost的新手,我正在尝试的非常简单。我在头文件中创建了一个单独的线程并尝试将其置于睡眠状态。但我无法让它发挥作用。这是代码和编译错误
main.h -
#pragma once
#include <conio.h>
#include <boost\thread.hpp>
boost::posix_time::seconds workTime ( 120 );
boost::this_thread::sleep ( workTime );
的main.cpp
#include "main.h"
void main ( void ) {
_getch();
};
输出 -
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2365: 'boost::this_thread::sleep' : redefinition; previous definition was 'function'
error C2491: 'boost::this_thread::sleep' : definition of dllimport data not allowed
error C2482: 'boost::this_thread::sleep' : dynamic initialization of 'thread' data not allowed
现在使用以下代码,全部在main.cpp中:
#include <boost\thread.hpp>
#include <conio.h>
void thread_func()
{
boost::posix_time::seconds workTime ( 120 );
boost::this_thread::sleep ( workTime );
}
int main(int argc, char* argv[])
{
boost::thread t(thread_func);
_getch();
}
收到以下错误:
1&gt; LIBCMTD.lib(dbgheap.obj):错误LNK2005:已在LIBCMT.lib(malloc.obj)中定义__heap_alloc
1&gt; LIBCMTD.lib(dbgheap.obj):错误LNK2005:__ recalloc已在LIBCMT.lib中定义(recalloc.obj)
1&gt; LIBCMTD.lib(dbgheap.obj):错误LNK2005:__ msize已在LIBCMT.lib中定义(msize.obj)
1&gt; LIBCMTD.lib(dbghook.obj):错误LNK2005:__crt_debugger_hook已在LIBCMT.lib(dbghook.obj)中定义了
1&gt; LIBCMTD.lib(isctype.obj):错误LNK2005:__ isisype_l已在LIBCMT.lib(isctype.obj)中定义
1&gt; LIBCMTD.lib(isctype.obj):错误LNK2005:__ isctype已在LIBCMT.lib(isctype.obj)中定义
1&gt; LINK:警告LNK4098:defaultlib'LIBCMTD'与使用其他库冲突; use / NODEFAULTLIB:library
1&gt;致命错误LNK1169:找到一个或多个多重定义的符号
答案 0 :(得分:1)
您在任何控制流程之外调用boost::this_thread::sleep ( workTime )
。你应该做点什么:
void thread_func()
{
boost::posix_time::seconds workTime ( 120 );
boost::this_thread::sleep ( workTime );
}
int main(int argc, char* argv[])
{
boost::thread t(thread_func);
_getch();
}