c++ BOOST_TEST not in scope - c++

I'm using boost version 1.58
Boost appears to be installed correctly, and the following code compiles and runs correctly (giving an error on the third check):
#define BOOST_TEST_MODULE basics_test
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE( basics_test ) {
BOOST_CHECK(1 == 1);
BOOST_CHECK(1 + 2 < 4);
BOOST_CHECK(1 == 2);
}
However, when I attempt to run the second example from the site http://www.boost.org/doc/libs/1_59_0/libs/test/doc/html/boost_test/testing_tools/boost_test_universal_macro.html, I get the error:
test.cpp: In member function ‘void test_op_reportings::test_method()’:
test.cpp:20:20: error: ‘BOOST_TEST’ was not declared in this scope
BOOST_TEST(a == b);
When I change each instance of BOOST_TEST to BOOST_CHECK, the code compiles and runs normally. I'm using g++; when I configure g++ to use c++11 as in g++ -std=c++11 test cpp -o main I get the same error.
How should I go about fixing this?

The documents you are looking at are for Boost 1.59, yet you are using boost 1.58. what makes you think BOOST_TEST existed in 1.58?
From the 1.58 documentation, I only see support for BOOST_TEST_MESSAGE and other alternatives.
Either upgrade your Boost to at least 1.59 where the documentation shows you have BOOST_TEST, or use the features available in 1.58 such as what you have already done.

Related

cmath error on MacOS c++17: call to abs ambiguous

Having this error using c++17 on Mac OS.
As far as I can tell, code is correct and should work fine (compiles without issue w/ g++ and clang++ on linux).
Also, as far as I can tell, the current default mac version of clang [10.0.1] should support c++17 (full version info printout below).
So, my question is: is this actually a bug in my code, but it works by fluke on linux? Or is it an issue with MacOS clang e.g., not full c++17 implementation?
From cppref:
Defined in header (since C++17):
int abs( int n );
Other c++17 features seem to work completely fine.
#include <cmath>
// #include <cstdlib> //works if included
int main() {
int i = 1;
// return std::abs(1); // Works fine
return std::abs(i); // Fails
}
Compile with:
clang++ -std=c++17 test.cpp
Get this error:
test.cpp:7:10: error: call to 'abs' is ambiguous
return std::abs(i);
^~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/math.h:761:1: note:
candidate function
abs(float __lcpp_x) _NOEXCEPT {return ::fabsf(__lcpp_x);}
^
(... etc.)
1 error generated.
If you #include <cstdlib>, it works without error.
using -std=gnu++17 or -std=c++1z doesn't remove the problem either.
In the actual code (which is obviously more complex than the above, and actually uses c++17 features), the error happens depending on the order of my include files.
I can't replicate that in the simple example, but I assume it boils down to calling the cstdlib version instead of the cmath version.
Currently, my 'workaround' is to just put the header includes into the order that works..but this is hardly a long-term solution.
Does anyone know the cause?
Version info (error not specific to this MacOS version, also happens on my students' laptops):
Bens-iMac:test ben$ clang++ -v
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
See LWG Issue 2912
This has been fixed in libc++ trunk. I don't know if Apple has shipped this fix yet. As you found, including <cstdlib> is a workaround.

Why cannot I compile my code with g+ in my terminal [duplicate]

I just started using MinGW for Windows. When trying to create executable using
g++ a.cpp -o a.exe -std=c++14
for the code below:
#include <string>
using namespace std;
int main()
{
string x = to_string(123);
return 0;
}
I'm getting following error:
C:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingwex.a(vsnprintf.o):(.text+0x0): multiple definition of vsnprintf
C:\Users\..\Local\Temp\cc4sJDvK.o:c:/mingw/include/stdio.h:426: first defined here
collect2.exe: error: ld returned 1 exit status
What is the root cause for this error and how can I make it go away? While I can easily find a replacement for to_string() function I'm not sure what is causing this error to occur in the first place.
Installing MinGW packages mingw32-libmingwex-* will link an appropriate version of vsnprintf and avoid the linker error.
This issue, i.e. multiple definition of vsnprintf, still exists in MinGW as December 2019.
After investigating a lot, I found the solution in the official mailing list.
It's a bug in mingwrt-5.2.2. Downgrading to the mingwrt-5.2.1 version solves that issue.
To do that, just input the following command:
mingw-get upgrade mingwrt=5.2.1
Then restart the MinGW shell.
Read the full story here.
Note: MinGW-w64 and MinGW are separate projects, so the accepted solution is not so helpful to me, as I want to keep MinGW and not to move to MinGW-w64.
I solved this issue using MinGW w64 compiler
download mingw-w64-install.exe
setup to Version: 6.3.0, Architecture: i686, Threads: posix, Exception: dwarf and Build revision: 2.
I hope this will be of some help.
There are multiple definitions of vsnprintf in both stdio.h and libmingwex.a. I am able to work this around by adding #define __USE_MINGW_ANSI_STDIO 0 to the start of the code, before any includes, which disables the definition of vsnprintf in stdio.h.

Compiler can't find 'aligned_alloc' function

I'm trying to launch the example code from aligned alloc:
#include <cstdio>
#include <cstdlib>
int main()
{
int* p1 = static_cast<int*>(std::malloc(10*sizeof *p1));
std::printf("default-aligned address: %p\n", static_cast<void*>(p1));
std::free(p1);
int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
std::printf("1024-byte aligned address: %p\n", static_cast<void*>(p2));
std::free(p2);
}
My compilers give me this error:
$ g++-mp-8 main.cpp -std=c++17
main.cpp:10:38: error: no member named 'aligned_alloc' in namespace 'std'
int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
I am working with macOS High Sierra 10.13.6 and tried to compile this code with Macport's GCC 7.3.0, 8.2.0 and CLang (Apple LLVM version 10.0.0), they all produce the same error.
Edit: It doesn't work with either std:: present or not.
Edit2: I installed macOS Mojave and that did not fix the problem. I hoped it'll reinstall macOS's toolchain but it didn't. So I guess I cannot accept provided answers until I get a more specific one.
I am not using macOS but I have similar problems on linux using a custom g++. If you look at the cstdlib header, there is something like
#if __cplusplus >= 201703L && defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
using ::aligned_alloc;
#endif
So aligned_alloc is only pulled into the std namespace if C++17 is available and glibcxx supports it. You can check x86_64-linux-gnu/bits/c++config.h (or something similar on macOS) if _GLIBCXX_HAVE_ALIGNED_ALLOCis defined. If not your glibc version is too old.
For clang and the libc++ implementation aligned_alloc is available if _LIBCPP_HAS_C11_FEATURES is defined which again depends on a recent version of glibc.
As an alternative you can use boost.
As the accepted answer mentions it, using boost::align::aligned_alloc solves the problem.
To fix the error without source modification, just add the following on top of the file:
#ifdef __APPLE__
#include <boost/align/aligned_alloc.hpp>
using boost::alignment::aligned_alloc;
#endif

gcc compile error with sine and boost:interval

I'm required to run some code on a linux (CentOs 6.7) server with gcc/g++ installed (7.2.0) and boost (1.66) (Edit: not only 1.66, an older version of boost interfered. Keep your machines clean guys). I specified my interval type with policies like so:
// test.cpp
#include <boost/numeric/interval.hpp>
namespace bn = boost::numeric;
namespace bi = bn::interval_lib;
using Interval = bn::interval<
double,
bi::policies<
bi::save_state<bi::rounded_transc_std<double> >,
bi::checking_base<double>
>
>;
Now I want to do some simple calculation like:
// still test.cpp
int main()
{
Interval iv_arg {1.0};
Interval res = sin(iv_arg);
}
On my local machine, a mac, compiling with clang works perfectly fine. However, as soon as I try to run it on the server, compiling with g++ -std=c++11 test.cpp I'm getting the error:
/usr/include/boost/numeric/interval/rounded_arith.hpp:71:59: error:
'to_int' was not declared in this scope, and no declarations were
found by argument-dependent lookup at the point of instantiation
[-fpermissive] T int_down(const T& x) { this->downward(); return
to_int(x); }
~~~~~~^~~ /usr/include/boost/numeric/interval/rounded_arith.hpp:71:59: note:
declarations in dependent base
'boost::numeric::interval_lib::detail::c99_rounding' are not found by
unqualified lookup
/usr/include/boost/numeric/interval/rounded_arith.hpp:71:59: note: use
'this->to_int' instead
There is some more info which seems irrelevant to the topic, but I can include it if it might help. I tried to google a little but couldn't find anything relevant. If possible, I would like to find a solution that doesn't require any changes on the server. Does anyone have an idea or encountered a similar problem before?
First version of boost that compiles this is 1.58
boost 1.57 ERROR
boost 1.58 OK

getting started with boost cpu_timer, error: ‘boost::timer::cpu_timer’ has not been declared

I'm trying to compile this simple program to start learning how to use timers:
#include <boost/timer.hpp>
using boost::timer::cpu_timer;
//...
nanosecond_type last_checkpoint_time = 0;
cpu_timer checkpoint_timer; // start the timer
for (;;)
{
//process_a_transaction();
if (checkpoint_timer.elapsed().user - last_checkpoint_time > 5*1000000000LL)
{
//... create a checkpoint ...
last_checkpoint_time = checkpoint_timer.elapsed().user;
cout << checkpoint_timer.elapsed().user << endl;
}
}
I'm using gentoo linux and issuing the following command to compile:
g++ -I /usr/include/boost-1_46 timer1.cpp -o timer
I get these errors:
timer1.cpp:3:21: error: ‘boost::timer::cpu_timer’ has not been declared
timer1.cpp:5:1: error: ‘nanosecond_type’ does not name a type
timer1.cpp:6:1: error: ‘cpu_timer’ does not name a type
timer1.cpp:8:1: error: expected unqualified-id before ‘for’
timer1.cpp:8:8: error: expected unqualified-id before ‘)’ token
I was reading the docs under errors and warnings but the problem I am having is that I only have two libraries:
/usr/lib/libboost_test_exec_monitor-1_46.a
/usr/lib/libboost_test_exec_monitor-mt-1_46.a
Is this because I did not use the static-libs flag during compile of boost? Would I be better off using static-libs? Maybe this is a tangent. What else can cause the errors given above? Please forgive my ignorance as I am pretty new to C++/boost.
Thanks
I haven't been using cpu_timer my self, but a quick Google search seems to indicate you should include <boost/timer/timer.hpp> instead. As for the error of nanosecond_type you need to use another using statement for that.
I think I figured out what the problem is. I was quoting an example in my original post from the ver 1.49 documentation. cpu_timer was first discussed in the boost documentation in ver 1.48. The stable version on gentoo is currently 1.46 and testing only provides ver 1.47, ver 1.48 is hardmasked. So my only option is to remove boost from my system download the tar of 1.49 and possibly break my system wrt boost or wait for the hardmask to be removed from ver 1.48.
In any case, static-libs is certainly irrelevant because this is a compiler error, not a linker error. It doesn't look at the libraries until the linker stage, until then only the headers are relevant.