Boost date/time microsec_clock not compiling correctly - c++

I'm trying to use the date/time facilities of the C++ Boost library v1.41. (Note: this is Linux, not Windows; g++ v4.4.7)
Code:
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::posix_time::ptime;
using boost::date_time::microsec_clock;
:
t1 = (boost::date_time::microsec_clock::local_time()); // line 208
The error:
tom.cpp:208: error: 'template<class time_type> class boost::date_time::microsec_clock' used without template parameters
Now, there's this in boost/date_time/posix_time/posix_time_types.hpp:
#ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
//! A time clock that has a resolution of one microsecond
/*! \ingroup time_basics
*/
typedef date_time::microsec_clock<ptime> microsec_clock;
#endif
What I'm concluding is that BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK is undefined, resulting in the typedef never happening, resulting in the reference to "microsec_clock" looking like it needed a template parameter.
As far as I can tell, I'm following the Boost date_time documentation to the letter. Any ideas?

I have the same problem right now.
Yesterday it worked without any problems but today I needed to delete all my compiled libraries and recompile them due to a svn corruption problem. Ever since this error occurred.
The way to fix it is rather simple.
Just use
t1 = (boost::posix_time::microsec_clock::local_time());
instead of
t1 = (boost::date_time::microsec_clock::local_time());
This will preset the time type to posix format however it will not fix the initial problem with BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK.
I hope this was of help to you.

Related

BGL: call to strong_components fails to compile when random_spanning_tree.hpp is included

This is a weird one, I was hoping to use boost::random_spanning_tree in an existing function which also calls boost::strong_components but simply including boost/graph/random_spanning_tree.hpp without otherwise modifying the code causes gcc to produce a compilation error.
The function looks something like this:
#include "boost/graph/adjacency_list.hpp"
#include "boost/graph/strong_components.hpp"
//#include "boost/graph/random_spanning_tree.hpp" // can't include this!
void foo() {
boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> g;
// TODO: add some vertices
std::vector<unsigned> component(boost::num_vertices(g));
unsigned num = boost::strong_components(g, &component[0]);
}
and gcc complains:
error: no matching function for call to ‘get(unsigned int*&, boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> >::vertex_descriptor&)’
if (get(comp, w) == (std::numeric_limits<comp_type>::max)())
Is the code itself flawed or is this an issue with Boost?
EDIT:
I must apologize, I have made a slight mistake in my minimal example, the code above only fails to compile on my system (Linus, g++ 8.2.1, Boost 1.67) if boost/graph/random_spanning_tree.hpp is included before boost/graph/strong_components.hpp. Using wandbox, I have found no combination of compiler / Boost version for which this does not happen.
I would file a ticket for this but Boost makes this needlessly difficult (I don't have TICKET_CREATE rights for Boost's Trac but how to obtain those is not specified anywhere).
It's no problem here: Live On Coliru
Also try
https://wandbox.org/permlink/jDIYB9oCjAP2wxHo wandbox which lets you switch compiler versions, flags and every possible boost version
Which leads me to believe it's a problem with the rest of your code/compiler config.
Are you on windows? I see max which is a MACRO in windows.h. Be sure to not include that (also check stdafx.h if you use it)
UPDATE
After your edit I was able to repro the error. That's a bug in boost, and I've created a ticket for it at the bug tracker

Errors using boost numeric bindings and lapack call to gesvd

I've done my best recently to set up boost's numeric bindings to allow me to use LAPACK from C++, but I've run into some roadblocks. First off, I have confirmed that boost is working fine, so it's something to do with my LAPACK libraries or the boost numeric bindings.
Here's a bit of code to test what I'm trying to do:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/bindings/lapack/gesvd.hpp>
#include <boost/numeric/bindings/traits/ublas_matrix.hpp>
//#include <boost/numeric/bindings/traits/ublas_vector2.hpp>
//#include <boost/numeric/bindings/traits/matrix_traits.hpp>
typedef boost::numeric::ublas::matrix<int> iMatrix;
typedef boost::numeric::ublas::matrix<double> dMatrix;
typedef boost::numeric::ublas::vector<int> iVector;
typedef boost::numeric::ublas::vector<double> dVector;
namespace ublas = boost::numeric::ublas;
namespace lapack = boost::numeric::bindings::lapack;
void function() {
int n = 10;
dMatrix jacobi(n,n); // then actually initialize it
dVector eigenvals(n);
dMatrix eigenvects(n);
dVector work(n);
int error = lapack::gesvd('N', 'A', jacobi, eigenvals, eigenvects, work);
std::cout << eigenvals << std::endl;
}
Now while I'm not 100% correct that this code should compile when everything is set up correctly, the errors I've been getting when building don't seem to make much sense to me.
In file included from C:\MinGW\boost\boost_1_57_0/boost/serialization/tracking.hpp:20:0,
In file included from C:\MinGW\boost\boost_1_57_0/boost/serialization/tracking.hpp:20:0,
.............
from ..\Solver.cpp:6:
C:\MinGW\boost\boost_1_57_0/boost/numeric/bindings/traits/ublas_matrix.hpp: In instantiation of 'struct boost::numeric::bindings::traits::matrix_detail_traits, boost::numeric::ublas::matrix >':
C:\MinGW\boost\boost_1_57_0/boost/numeric/bindings/traits/matrix_traits.hpp:48:10: required from 'struct boost::numeric::bindings::traits::matrix_traits >'
C:\MinGW\boost\boost_1_57_0/boost/numeric/bindings/lapack/gesvd.hpp:167:7: required from 'int boost::numeric::bindings::lapack::gesvd(char, char, char, MatrA&, VecS&, MatrU&, MatrV&) '
C:\MinGW\boost\boost_1_57_0/boost/numeric/bindings/lapack/gesvd.hpp:477:50: required from 'int boost::numeric::bindings::lapack::gesvd(char, char, MatrA&, VecS&, MatrU&, MatrV&)'
..\Solver.cpp:85:77: required from here
C:\MinGW\boost\boost_1_57_0/boost/numeric/bindings/traits/ublas_matrix.hpp:46:5: error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE'
BOOST_STATIC_ASSERT((boost::is_same<
Again, I have tested that boost and ublas by itself is working fine. When I comment out the lapack::gesvd line of code, everything compiles and runs fine. As far as I can tell, these errors means that I have correctly linked LAPACK to the program (there are no unresolved symbols), and my program is able to find the correct binding files (calling lapack::gesvd returns a different error when you give it incorrect input). So I'm at a loss.
I'm on Windows 64 bit, using Eclipse, C++, boost, ublas, and LAPACK. Information about the boost numeric bindings to LAPACK can be found here: http://git.tiker.net/boost-numeric-bindings.git/blob_plain/be4a548307f3e95786acb3487e571bdffe738e4a:/libs/numeric/bindings/lapack/doc/index.html
Any advice about the overall linking/compiling process using boost numeric bindings+LAPACK would be appreciated. I honestly haven't been able to find any good examples online.
So I figured out my problem(s) -- there were several,-- and I thought I should answer my own question so that others might benefit.
First off, my LAPACK installation was incorrect. I had downloaded the 64-bit version instead of the 32-bit version. Even though it's 2015, somehow I'm stuck using a 32-bit version of the lapack dll...
Secondly, linking in Eclipse works a little differently than I thought. Going to the project properties, C/C++ Build -> Settings -> Tool Settings -> MinGW C++ Linker -> Libraries allows you to link libraries. Under the top libraries option (-l), I added lapack and blas. Under the bottom Library search path (-L), I added the location of the .dll files.
At this point, I could run sample LAPACK code, just not use the boost numeric bindings. Thirdly, I figured out what the numeric bindings traits includes were. From the traits overview page, I was able to figure out that in order to use particular vector or matrix class in bindings to LAPACK, I had to include proper traits specialization. For instance, using the boost::numeric::ublas::matrix object and sending it to LAPACK required including the trait header file <boost/numeric/bindings/traits/ublas_matrix.hpp>.
This solved my error that you see from the original post, and I could use the boost numeric bindings. Finally, I had messed up my example code because I didn't actually understand what gesvd was doing. It was just a test program, so it's no big deal, but I'll attach the working code below to show the singular value decomposition that I had initially attempted.
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/bindings/lapack/gesvd.hpp>
#include <boost/numeric/bindings/traits/ublas_matrix.hpp>
#include <boost/numeric/bindings/traits/ublas_vector.hpp>
#include <boost/numeric/bindings/traits/ublas_vector2.hpp>
typedef boost::numeric::ublas::matrix<int> iMatrix;
typedef boost::numeric::ublas::matrix<double> dMatrix;
typedef boost::numeric::ublas::vector<int> iVector;
typedef boost::numeric::ublas::vector<double> dVector;
namespace ublas = boost::numeric::ublas;
namespace lapack = boost::numeric::bindings::lapack;
void function() {
int n = 10;
dMatrix jacobi(n,n); // then actually initialize it
dVector eigenvals(n);
//int error = lapack::gesvd('S','S', jacobi, eigenvals, eigenvects1, eigenvects2);
int error = lapack::syevd('V','L', jacobi, eigenvals, lapack::optimal_workspace() );
std::cout << eigenvals << std::endl;
std::cout << jacobi << std::endl;
}

How can I use boost::thread::timed_join with nanoseconds enabled in boost::date_time?

Here is some C++ code illustrating my problem with a minimal expample:
// uncomment the next line, to make it hang up:
//#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG //needed for nanosecond support of boost
#include <boost/thread.hpp>
void foo()
{
while(true);
}
int main(int noParameters, char **parameterArray)
{
boost::thread MyThread(&foo);
if ( MyThread.timed_join( boost::posix_time::seconds(1) ) )
{
std::cout<<"\nDone!\n";
}
else
{
std::cerr<<"\nTimed out!\n";
}
}
As long as I don't turn on the nanosecond support everthing works as expected, but as soon as I uncomment the #define needed for the nanosecond support in boost::posix_time the program doesn't get past the if-statement any more, just as if I had called join() instead of timed_join().
Now I've already figured out, that this happens because BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG changes the actual data representation of the timestamps from a single 64bit integer to 64+32 bit. A lot boost stuff is completely implemented inside the headers but the thread methods are not and because of that they cannot adapt to the new data format without compiling them again with the apropriate options. Since the code is meant to run on an external server, compiling my own version of boost is not an option and neither is turning off the nanosecond support.
Therefore my question is as follows: Is there a way to pass on a value (on the order of seconds) to timed_join() without using the incompatible 96bit posix_time methods and without modifying the standard boost packages?
I'm running on Ubuntu 12.04 with boost 1.46.1.
Unfortunately I don't think your problem can be cleanly solved as written. Since the library you're linking against was compiled without nanosecond support, by definition you violate the one-definition rule if you happen to enable nanosecond support for any piece that's already compiled into the library binary. In this case, you're enabling it across the function calls to timed_join.
The obvious solution is to decide which is less painful to give up: Building your own boost, or removing nanosecond times.
The less obvious "hack" that may or may not totally work is to write your own timed_join wrapper that takes a thread object and an int representing seconds or ms or whatever. Then this function is implemented in a source file with nothing else and that does not enable nanosecond times for the specific purpose of calling into the compiled boost binary. Again I want to stress that if at any point you fail to completely segregate such usages you'll violate the one definition rule and run into undefined behavior.

Boost ptime under MinGW not thread safe

I have a problem with boost library. I'm using MinGW with gcc 4.5.2 to compile the following code:
unsigned long GetEpochSeconds()
{
using namespace boost::posix_time;
using namespace boost::gregorian;
ptime now(second_clock::universal_time());
ptime epoch(date(1970,1,1));
time_duration diff = now-epoch;
return diff.total_seconds();
}
The problem is that this code is not thread-safe. When I run it from within multiple threads, my application crashes. For now I've converted to c-standard functions like time, mktime etc. and everything works fine, but in the future I will need a few boost time functions.
I was compiling also with -D_REENTRANT, but this didn't help.
Thanks for any suggestions.
Check whether your code is calling gmtime() or gmtime_r() (use a debugger for this). See http://www.boost.org/doc/libs/1_48_0/boost/date_time/c_time.hpp and note that BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS must be defined in order for getting the time to be thread-safe.

boost::filesystem: Updating from 1.34.1 to current

I've got some legacy code here that makes use of boost::filesystem in version 1.34.1. I've isolated it into a little test program:
#include <iostream>
#include <boost/filesystem/path.hpp>
int main()
{
// note the second parameter
boost::filesystem::path p( "/tmp/foo", boost::filesystem::native );
std::cout << p.string() << std::endl;
return 0;
}
When I try to compile this code with the current (1.46.1) version of Boost, I get the following error:
test.cpp: In function ‘int main()’:
test.cpp:7: error: invalid conversion from ‘bool (*)(const std::string&)’ to ‘void*’
test.cpp:7: error: initializing argument 2 of ‘boost::filesystem3::path::path(const Source&, typename boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename boost::decay<T>::type>, void>::type*) [with Source = char [9]]’
I've tried to make heads or tails of the Boost documentation, but I can't seem to figure out what that second parameter was good for, or what to replace it with. Can anybody shed some light on this?
UPDATE: I was not really clear on the requirements. For a transition period at least, I will have to support both versions (1.34.1 and 1.46.1). Is there a compatible way to cover both versions of Boost with the same code, or will I have to resort to #if BOOST_VERSION magic?
UPDATE 2: Used #if BOOST_VERSION ... as no further opinion was forthcoming. Thanks for the help.
boost::filesystem defines two formats for filenames: native, which differs from system to system, and generic which is identical across systems. Under POSIX (which appears to be what you are using) the two are identical. Under Windows the native format allows backslashes whereas the generic format does not. Under VMS (for example) the two are very different (the native format is something like [dir.subdir]file).
boost::filesystem::native appears to have been intended to indicate that the filename you are providing is in native format. I believe the current version of boost::filesystem is supposed to decide automatically, and since you're on a POSIX system it makes no difference anyway.
In short, the correct thing to do is simply to omit the parameter.
native seems to be a function now and the path constructor doesn't seem to need the native specifier. So just removing it should be fine.