compile c++ boost test program on command line - c++

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

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>

Related

Where does Homebrew install C++ packages?

I would like to use C++'s std::format library to format strings. See the minimal working example below.
/* example.cpp */
#include <iostream>
#include <format>
#include <string>
int main() {
std::string s = std::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
However, when I compile my code, I get the following error message:
example.cpp:2:10: fatal error: format: No such file or directory
2 | #include <format>
I am using the latest version of macOS, and I have Homebrew installed as my package manager. I already installed clang-format through Homebrew, but for some reason, my compiler can't locate the header file. Can somebody help me figure out the problem is? I have tried using Apple's GCC and the custom GCC10 provided by Homebrew, but in both cases, I get the same error message. Is this a Homebrew issue or a C++ issue?
Following this answer to use fmt, you can install the library with brew install fmt, then modified the code to
/* example.cpp */
#include "fmt/format.h"
#include <iostream>
#include <string>
int main() {
std::string s = fmt::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
and compile with
clang++ -std=c++11 test.cpp -lfmt

How to link boost thread with Xcode

I have built Boost from the website using
./bootstrap.sh
./b2 install
I think all are installed properly. I have headers in /usr/local/include/boost and libs in /usr/local/lib.
Everything links as long as don't include boost/thread.hpp:
//
// main.cpp
// ising3
//
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <iostream>
#include <boost/date_time.hpp>
//#include <boost/thread.hpp>
using namespace ::boost::tuples;
using namespace ::boost;
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
tuple<int,int> a,b,c;
a=make_tuple(1,1);
b=make_tuple(3,2);
std::cout<<a;
return 0;
}
Runs and prints:
Hello, World!
(1 1)
However, if uncommented, it fails:
I have linked the dynamic library libboost_thread.a and libboost_thread.dylib and included /usr/local/include into header search path and /usr/local/lib into library search path.
You should also link to boost_system.
The error indicates that it needs boost::system::system_category (which exists for error reporting).

MinGW include paths conflict

I'm trying to cross-compile on Linux for Win64 using MinGW-w64. Here's my code
#include <cstdlib>
#include <iostream>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
int main(int argc, char** argv)
{
if (argc > 1)
std::cout << std::atoi(argv[1]) << std::endl;
std::cout << boost::uuids::random_generator()() << std::endl;
return 0;
}
A simple compile fails with the error
$ x86_64-w64-mingw32-c++ hello.cpp
hello.cpp:4:31: fatal error: boost/uuid/uuid.hpp: No such file or directory
#include <boost/uuid/uuid.hpp>
^
Boost is installed in /usr/include which apparently the MinGW compiler doesn't search. If I add that path, then...
$ x86_64-w64-mingw32-c++ -I/usr/include hello.cpp
In file included from /usr/include/stdlib.h:314:0,
from /usr/x86_64-w64-mingw32/include/c++/4.9.1/cstdlib:72,
from hello.cpp:1:
/usr/include/sys/types.h:109:19: error: conflicting declaration ‘typedef __ssize_t ssize_t’
typedef __ssize_t ssize_t;
^
MinGW's cstdlib is including /usr/include/stdlib.h instead of /usr/x86_64-w64-mingw32/include/stdlib.h! How do I solve this? I need the -I in order to include Boost, but then MinGW includes other headers incorrectly.
You cannot use boost headers from /usr/include to cross-compile windows binaries.
You should also cross-compile boost.
See this guide for details about how to cross-compile boost on linux (it's for vle, but the first part is about boost):
http://www.vle-project.org/wiki/Cross_compilation_Win32
Update: Given that the guide is a little bit old, makes sense to link to boost documentation:
http://www.boost.org/boost-build2/doc/html/bbv2/tasks/crosscompile.html

Code with XCppRefl does not work

I am trying to use XCppRefl lib to achieve reflections in c++. http://www.extreme.indiana.edu/reflcpp/. I could successfully install this library in linux and run the tests given with the source code of the library.
Here is the code that I have written --
#include <iostream>
using namespace std;
#include <reflcpp/ClassType_tmpl.hpp>
#include <reflcpp/BoundClassType_tmpl.hpp>
#include <reflcpp/Exceptions.hpp>
using namespace reflcpp;
#include "Complex.h"
int main()
{
//ClassType ct = ClassType::getClass( string("Complex") );
////PtrHolder_smptr_t obj = ct.createInstance();
//assert(ct.name() == "B");
Complex x;
int ret;
Complex a;
ClassType c = ClassType::getClass( string("Complex") );
//cout<<"name :: "<<c.name()<<endl;
}
It seems to compile just fine --
$ g++ -g -I /usr/local/include/reflcpp-0.2/ -L /usr/local/include/reflcpp-0.2/ -lreflcpp main.cpp
However when I execute the executable (a.out), I get a core-dump
a.out: Type.cpp:87: static const reflcpp::Type_body* reflcpp::Type_body::getType(const std::string&): Assertion `s_class_name_map' failed.
Aborted (core dumped)
Does anyone has used this lib before? Please help.
you have to link your main.o to libreflcpp.a . after compiling use this:
g++ -p -pg -o"project_name" ./A.o ./A_reflection.o ./main.o /usr/local/lib/libreflcpp.a

Boost precompiled headers problem

I try to precompile Boost headers.
First experiment - with std:: headers. I create file std.hpp:
#include <vector>
#include <iostream>
// And other std:: headers
After that:
g++ std.hpp
Copy std.hpp.gch in /usr/include/c++/4.4.5
And write test program:
#include <std.hpp>
int main() {
std::cout << "Hello, precompiled world!" << std::endl;
return 0;
}
Works fine.
Now try precompile Boost headers.
I create boost.hpp file:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
After that:
g++ boost.hpp
Copy boost.hpp.gch in /usr/local/include/boost
And write test program:
#include <boost/boost.hpp>
int main() {
// Some code...
return 0;
}
But got error:
main.cpp:2:33: error: /usr/local/include/boost/boost.hpp: No such file or directory.
Try, for experiment:
#include </usr/local/include/boost/boost.hpp>
int main() {
// Some code...
return 0;
}
Same error.
Try copy boost.hpp.gch in another place - same error.
If I put file boost.hpp in same place - works fine (so there is no problems with path):
ls /usr/local/include/boost | grep boost
boost.hpp
boost.hpp.gch
So compiler use boost.hpp header. But why compiler don't see precompiled boost.hpp.gch??
This might be a gcc bug as documented in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46110