使用动态链接使用Boost 1.60构建具有boost :: coroutine2的fibonnacci示例的链接器错误

时间:2016-02-16 08:41:26

标签: c++ boost visual-studio-2015 boost-coroutine2

我在visual studio 2015中做了一个简单的项目来重现我在Boost 1.60的更大代码库中遇到的问题

我试图简单地编译并运行在此处找到的示例:https://github.com/boostorg/coroutine2/blob/develop/example/fibonacci.cpp 稍微改变 - 使用动态库。

因此,我的完整代码如下:

#include <cstdlib>
#include <iostream>

#define BOOST_ALL_DYN_LINK //This is the only difference

#include <boost/coroutine2/all.hpp>

int main() {
    boost::coroutines2::coroutine< int >::pull_type source(
        [](boost::coroutines2::coroutine< int >::push_type & sink) {
        int first = 1, second = 1;
        sink(first);
        sink(second);
        for (int i = 0; i < 8; ++i) {
            int third = first + second;
            first = second;
            second = third;
            sink(third);
        }
    });
    for (auto i : source) {
        std::cout << i << " ";
    }
    std::cout << "\nDone" << std::endl;
    return EXIT_SUCCESS;
}

但是,我收到链接器错误:

1>------ Build started: Project: coroutine2-test, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\lynden\boost_1_60_0\boost\context\execution_context.ipp(209): warning C4251: 'boost::context::execution_context::ptr_': class 'boost::intrusive_ptr<boost::context::detail::activation_record>' needs to have dll-interface to be used by clients of class 'boost::context::execution_context'
1>Source.obj : error LNK2001: unresolved external symbol "public: static class boost::intrusive_ptr<struct boost::context::detail::activation_record> boost::context::detail::activation_record::current_rec" (?current_rec@activation_record@detail@context@boost@@2V?$intrusive_ptr@Uactivation_record@detail@context@boost@@@4@A)
1>D:\random projects\coroutine2-test\Debug\coroutine2-test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我当然将include目录设置为boost目录,将链接器附加目录设置为boost / stage / lib目录。

2 个答案:

答案 0 :(得分:0)

您需要链接boost.context(由boost.coroutine2使用)。 告诉编译器/链接器它也可以在哪里找到boost.context的共享库。

答案 1 :(得分:-1)

您需要使用标准C ++ 14(选项“-std = c ++ 14”)编译Boost,否则Boost.Context将不提供支持Boost.Coroutine2的必要实现,并且编译器将无法链接。