【问题标题】:using c++ Boost in a makefile在生成文件中使用 C++ Boost
【发布时间】:2018-02-01 17:55:39
【问题描述】:

我有一个简单的 C++ 程序,我使用 Boost 库和 C++14 支持通过 clang 编译它。

我使用以下命令编译我的 sample.cpp 文件,它工作正常:

clang++ -g -std=c++1y -I$BOOST_ROOT sample.cpp -o sample

其中 $BOOST_ROOT 是我下载并解压 boost zip 文件的路径。

$BOOST_ROOT=/usr/local/boost_1_66_0/

当我尝试使用 makefile 编译相同的 sample.cpp 文件时,它不起作用。

这就是我的 ma​​kefile 的样子:

sample: sample.cpp
    clang++ -g -std=c++1y -I$BOOST_ROOT sample.cpp -o sample

运行 ma​​ke 命令,我得到错误:

Sample.cpp:9:10: fatal error: 'boost/format.hpp' file not found
#include <boost/format.hpp>
         ^~~~~~~~~~~~~~~~~~
1 error generated.

这里是sample.cpp

//
//  sample.cpp
//  
//

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

using namespace std;

int main()
{
    std::cout << "Enter your first name: " << std::endl;
    std::string firstName;
    std::cin >> firstName;

    std::cout << "Enter your surname: " << std::endl;
    std::string surname;
    std::cin >> surname;

    auto formattedName = str( boost::format("%1% %2%"s) % firstName % surname );
    std::cout << "You said your name is: " << formattedName << std::endl;

    return 0;
}

控制台输出

MacBook-Air:Listing_1_7 userd43f$ make
c++     sample.cpp   -o sample
sample.cpp:9:10: fatal error: 'boost/format.hpp' file not found
#include <boost/format.hpp>
         ^~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [sample] Error 1

MacBook-Air:Listing_1_7 userd43f$ ls $BOOST_ROOT 
INSTALL     boost       boost.png   bootstrap.sh    index.html  rst.css
Jamroot     boost-build.jam boostcpp.jam    doc     libs        status
LICENSE_1_0.txt boost.css       bootstrap.bat   index.htm   more        tools

【问题讨论】:

  • make 中的变量是这样展开的 $(VARNAME).
  • 带或不带括号没有区别。我试过了,结果是一样的
  • 为什么人们拒绝投票这个有效的问题?太荒谬了
  • 因为你使用了'using namespace std'。有些人真的很挑剔,即使它根本不关心问题。
  • 看起来它根本没有使用您的 sample 规则,因为 make 输出中缺少 -g -std=c++1y -I${BOOST_ROOT} 选项。

标签: c++ boost makefile clang clang++


【解决方案1】:

问题是我的 makefile 的命令行中有空格而不是制表符。

BOOST_ROOT := /usr/local/boost_1_66_0
BOOST_INC := ${BOOST_ROOT}/include

sample: sample.cpp
    clang++ -g -std=c++1y -I$(BOOST_ROOT) sample.cpp -o sample

在 clang++ 命令之前的最后一行中,我使用了空格而不是 TAB。我在 clang++ 之前用一个 TAB 替换了所有空格,并且我需要将 BOOST_ROOT 放在括号内(@MaximEgorushkin 提到)

然后它开始选择正确的命令,如下面的输出所示:

MacBook-Air:Listing_1_7 userd43f$ make
clang++ -g -std=c++1y -I/usr/local/boost_1_66_0 sample.cpp -o sample

【讨论】:

    【解决方案2】:

    Boost 根目录通常包含include 和lib 目录。

    应该是这样的:

    BOOST_ROOT := /usr/local/boost_1_66_0
    BOOST_INC := ${BOOST_ROOT}/include
    
    sample: sample.cpp
        clang++ -g -std=c++1y -I${BOOST_INC} sample.cpp -o sample
    

    【讨论】:

    • 谢谢,但还是不行:Sample.cpp:9:10: 致命错误:找不到'boost/format.hpp'文件#include ^~~ ~~~~~~~~~~~~~~~~~~
    • @revolutionary 显示make显示的命令行并显示/usr/local/boost_1_66_0的目录结构。
    • 好的,我已经在上面帖子的新编辑中添加了上面makefile的控制台输出
    猜你喜欢
    • 2011-03-15
    • 2021-01-28
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多