在命令行上编译c++boost测试程序

compile c++ boost test program on command line

本文关键字:c++boost 测试程序 编译 命令行      更新时间:2023-10-16

我已经在exercisem.io注册了一个帐户,正在处理c++测试用例。试着让我的头脑围绕升压测试,我创建了这个简单的bob.cpp程序:

#include "bob.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[]) {
    string s = bob::hey("Claus");
    cout << s << endl;
    return 0;
}

bob.h:

#include <string>
namespace bob {
    std::string hey(std::string s) {
        return "Hello " + s;
    }
}

使用"clang++bob.cpp"在终端中编译并使用运行/a.out作品。使用此链接编写了一个升压测试:c++使用升压测试

bob_test.cpp:

#include "bob.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(greeting) {
    BOOST_CHECK_EQUAL("Hello Claus", bob::hey("Claus"));
}

但当我尝试使用编译它时

~/devel/cpp/boost%>clang++ -I /opt/local/include -l boost_unit_test_framework bob_test.cpp 
ld: library not found for -lboost_unit_test_framework
clang: error: linker command failed with exit code 1 (use -v to see invocation)

问候Claus

这是Yosemite上通过macports安装的Xcode 6.0.1,boost 1.56。在小牛队尝试了同样的Xcode,提高了1.55,但结果相同。

我通过更改传递给链接器的参数使其工作:

clang++ -I /opt/local/include -Wl,/opt/local/lib/libboost_unit_test_framework.a bob_test.cpp
                              ^^^^

并提供到库的完整路径。

要启用c++11功能,请添加以下内容:

-std=c++11

您忘记了库路径:

$ clang++ -I /opt/local/include -L /opt/local/lib -l boost_unit_test_framework bob_test.cpp
                                ^^^^^^^^^^^^^^^^^

修复后出现的链接错误表明您没有main()函数-如果您有所有必要的样板,boost单元测试框架似乎会为您生成此函数-请参阅http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/test-organization/auto-test-suite.html详细信息,但看起来你可能需要:

#define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp>

而不是:

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>