Cygwin编译使用boost库的程序时出现链接问题的可能原因

在使用 Cygwin 提供的 boost 库进行编译时,出现了链接错误,这里记录一下问题。需要编译的代码如下:

// filename:test.cpp
#include <iostream>
#include <string>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int argc,char *argv[] )
{
    po::options_description opts("all options");
    po::variables_map vm;

    opts.add_options()
    ("filename",po::value<std::string>(),"the filename which want to be found")
    ("help","this is a program to find a specified file");


    try
    {
        po::store(po::parse_command_line(argc,argv,opts),vm );
    }
    catch(...)
    {
        std::cout << "输入的参数中存在未定义选项!" << std::endl;
        return 0;
    }

    if(vm.count("help") )
    {
        std::cout << opts << std::endl;
    }
    if(vm.count("filename") )
    {
        std::cout << "find " << vm["filename"].as<std::string>() << std::endl;
    }
    if(vm.empty() ) std::cout << "no options found!" << std::endl;
    return 0;
}

这是一个简单的 boost::program_options 的练习项目,但在 make 时发生了链接错误,make 的主要语句如下:

g++ -o test.exe test.cpp -LE:/Cygwin/lib -lboost_program_options

要说明的是在 Cygwin 中安装的 boost 的 lib 文件默认被安装在我的 E:/Cygwin/lib 文件夹下,文件为 libboost_program_options.dll.a 。
编译中一直报链接错误,后来经过一段时间的尝试,终于发现不能使用 -l 这种链接的方式,而必须要使用

g++ -o test.exe test.cpp E:/Cygwin/lib/boost_program_options

这种将库文件作为一个原始文件的方式,但为什么在 Cygwin 下必须这样还不得而知。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值