kometen
kometen

Reputation: 7852

compile c++ boost test program on command line

I've registered an account at exercism.io and is working on the c++ test case. Trying to wrap my head around boost test I created this simple bob.cpp program:

#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;
    }
}

Compiling in terminal with 'clang++ bob.cpp' and running with ./a.out works. Wrote a boost test using this link: c++ Using boost test

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"));
}

But when I try to compile it using

~/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)

regards Claus

This is on Yosemite with Xcode 6.0.1, boost 1.56 installed via macports. Tried on a Mavericks with same Xcode and boost 1.55 but same result.

I got it working by changing the parameters passed to the linker:

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

and provide the complete path to the library.

And to enable c++11 features add this:

-std=c++11

Upvotes: 2

Views: 1550

Answers (1)

Paul R
Paul R

Reputation: 213190

You forgot the library path:

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

The link error you get after fixing this indicates that you have no main() function - it seems that the boost unit test framework will generate this for you provided you have all the necessary boilerplate in place - see http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/test-organization/auto-test-suite.html for details, but it looks like you may need:

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

rather than:

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

Upvotes: 2

Related Questions