0

这个测试代码是可以的,问题一定出在我构建它的方式上:

#include <boost/thread.hpp>
#include <iostream>

void Wait(int seconds)
{
    boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void Thread()
{
    for (int i = 0; i < 5; ++i)
    {
        Wait(1);
        std::cout << i << std::endl;
    }
}

int main()
{
    std::cout << "Boost threads test:" << std::endl;
    boost::thread t(Thread);
    t.join();
}

这是 CMakeLists.txt 文件:

project(BoostThreadsTest)
cmake_minimum_required(VERSION 2.8)

find_package(Boost 1.46 REQUIRED COMPONENTS thread)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    message(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}")
else()
    message(WARNING "Boost headers/libraries not found.")
endif()

aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

target_link_libraries(${PROJECT_NAME}
    ${Boost_LIBRARIES}
    ${THREADING_LIBRARY}
    )

内置:

cmake -G "MinGW Makefiles" .
mingw32-make.exe

一切顺利,只有这个警告:

In file included from C:/boost/include/boost-1_48/boost/thread/win32/thread_data.hpp:12:0,
                 from C:/boost/include/boost-1_48/boost/thread/thread.hpp:15,
                 from C:/boost/include/boost-1_48/boost/thread.hpp:13,
                 from C:\Users\pietro.mele\projects\tests\buildSystem_test\BoostTest\BoostThreadsTest\BoostThreadsTest\main.cpp:4:
C:/boost/include/boost-1_48/boost/thread/win32/thread_heap_alloc.hpp:59:40: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int
)' declared as  dllimport: attribute ignored [-Wattributes]
C:/boost/include/boost-1_48/boost/thread/win32/thread_heap_alloc.hpp:69:39: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared
as  dllimport: attribute ignored [-Wattributes]

我得到了可执行文件,但是当我运行它时,它什么也没做。甚至没有打印出不涉及多线程的“Boost threads test:”字符串。不会产生错误。就像按下 [return] 键一样。

谢谢你。

4

1 回答 1

2

(只需粘贴我的评论,以便问题可以标记为已关闭)。

我怀疑没有找到 DLL。我在从 cygwin 执行 exe 时看到了这种行为:什么都没有发生。但是,当我在 Windows 资源管理器中双击 exe 时,我看到一个消息框抱怨缺少 DLL:试试看。如果这是原因,请调整您PATH以启用 DLL 定位。


我不确定消息框被抑制的原因,如果我发现它(或者如果有人将它添加为评论),我会更新这个答案。

于 2013-07-24T12:58:28.353 回答