在windwos环境下用gcc编译boost动态库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Bobsweetie/article/details/78778729
之前在网上也下过一些别人编译好的boost动态库或者静态库文件,发现都不是很好用,因此决定自己重新编译。

1、下载boost库

下载最新的boost库,下载地址:http://www.boost.org/users/download/
解压,得到boost库的源代码,得到boost_1_65_1文件。

2、配置编译环境

首先需要安装gcc编译器,因为我在windows系统上已经安装了带有mingw(gcc的windwos版本)Qt环境,所以不需要再安装mingw,只需要再windwos的环境变量中添加gcc.exe可执行文件的环境变量,这样在任何目录下都可以使用gcc命令。添加环境变量的具体操作这里不再介绍。

3、生成编译工具

配置好环境变量之后,Win+R->输出cmd,以管理员身份打开windows 的命令行界面,然后进入boost_1_65_1文件的目录,然后执行bootstrap.bat命令,等待一分钟左右就会生成两个可执行文件,bjam.exe和b2.exe;

4、编译

在命令行,执行下面的编译命令bjam.exe –build-type=complete toolset=gcc stage variant=release link=shared threading=multi runtime-link=shared

5、编译完成

大概1个小时左右,将在stage文件夹中生成动态库文件,其中.a文件为链接库的文件,.dll文件为运行的时候的动态库文件。文件命令说明,例如:
libboost_system-mgw53-mt-1_65_1.dll和libboost_system-mgw53-mt-1_65_1.a文件
mgw53:编译器的版本mingw5.3版本;
mt:多线程的意思multithreaded;
1_65_1:boost的版本号。

6、链接库

创建Qt工程链接库,在.pro文件添加

INCLUDEPATH += "E:\zhangbo\boost_net\boost_1_65_1"

LIBS += -LE:\zhangbo\boost_net\boost_1_65_1\stage\lib   -lboost_filesystem-mgw53-mt-1_65_1 \
                                                         -lboost_system-mgw53-mt-1_65_1

main.cpp文件

#include <boost/math/special_functions/acosh.hpp>
#include <boost/math/special_functions/bessel.hpp>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/timer.hpp>
#include <iostream>


void TestBesse(){
    std::cout << "Test Boost:" <<std::endl;
    std::cout << boost::math::acosh(2.5) << std::endl;
    std::cout << boost::math::detail::bessel_i0(3.2) << std::endl;
    std::cout << "Test Finished !" <<std::endl;
}

void TestFileSystem(){
    boost::filesystem::path full_path(".");
    boost::filesystem::directory_iterator end_iter;
    std::cout << "Current Directory Files: " << std::endl;
    for(boost::filesystem::directory_iterator dir_itr(full_path); dir_itr != end_iter; ++dir_itr){
        std::cout << dir_itr->path().filename() << std::endl;
    }
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TestBesse();

    TestFileSystem();

    return a.exec();
}

遇到的问题

第一次的编译指令是
bjam.exe –build-type=complete toolset=gcc stage
链接boost库运行的程序的时候出现“duplicate section”错误
下面的回答说是编译的问题,但也没讲清楚到底是什么编译问题。
https://stackoverflow.com/questions/14181351/i-got-duplicate-section-errors-when-compiling-boost-regex-with-size-optimizati

我查看了bjam –help可以看到bjam的一些编译选项,然后指定了具体的编译选项。bjam -show-libraries会列出所有要编译的库。

我将原来编译的文件删除了,然后修改用下面的指令重新编译了一次boost库
bjam.exe –build-type=complete toolset=gcc stage variant=release link=shared threading=multi runtime-link=shared
再次链接的时候发现没有问题了,可以确定是编译的问题,至于到底是什么问题还没弄清楚

参考:
[1]http://www.boost.org/doc/libs/1_55_0/more/getting_started/windows.html

猜你喜欢

转载自blog.csdn.net/Bobsweetie/article/details/78778729