Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to use srand48() after changing to c++ 11

Why am I not able to compile my code to c++ 11 and use the srand48 function?

I have a program where I play around with some matrices. The problem is that when I compile the code with the -std=c++0x flag. I want to use some c++11 only functions and this is my approach to do so. It compiles without any problems if I do not specify the c++ version. Like this:

g++ -O2 -Wall test.cpp -o test -g

Please correct me if I have misunderstood what the mentioned flag does.

I run my code on a Windows 7 64-bit machine and compile through cygwin. I use g++ version 4.5.3 (GCC). Please comment if more information is required.

For some unknown reason (even to myself) then all my code is written in one compilation unit. If the error is caused by a structural error then you should also feel free to point it out. :)

I receive the following errors:

g++ -std=c++0x -O2 -Wall test.cpp -o test -g

test.cpp: In function ‘void gen_mat(T*, size_t)’:
test.cpp:28:16: error: there are no arguments to ‘srand48’ that depend on a template parameter, so a declaration of ‘srand48’ must be available
test.cpp:28:16: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
test.cpp:33:28: error: there are no arguments to ‘drand48’ that depend on a template parameter, so a declaration of ‘drand48’ must be available

Here is a sub of my code, it generates the errors shown above.

#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <limits.h>
#include <math.h>

#define RANGE(S) (S)

// Precision for checking identity.
#define PRECISION 1e-10

using namespace std;

template <typename T> 
void gen_mat(T *a, size_t dim)
{
    srand48(dim);
    for(size_t i = 0; i < dim; ++i)
    {
        for(size_t j = 0; j < dim; ++j)
        {
            T z = (drand48() - 0.5)*RANGE(dim);
            a[i*dim+j] = (z < 10*PRECISION && z > -10*PRECISION) ? 0.0 : z;
        }
    }
}

int main(int argc, char *argv[])
{

}

Regards Kim.

This is the solution that solved the problem for me:

First n.m. explained that srand() can not be used when compiling with -std=c++0x. The correct flag to use is -std=gnu++11 however it require g++ version 4.7+ Therefore, the solution for me was to compile my code with -std=gnu++0x The compile command = g++ -O2 -Wall test.cpp -o test -g -std=gnu++0x

like image 583
Kim Ahlstrøm Meyn Mathiassen Avatar asked May 16 '13 07:05

Kim Ahlstrøm Meyn Mathiassen


People also ask

What is srand48?

The srand48() function is used to reinitialize the most recent 48-bit value in this storage. The srand48() function replaces the high-order (leftmost) 32 bits of this storage with seedval argument value. The srand48() function replaces the low-order 16 bits of this storage with the value 330E (base 16).

What is drand48 C++?

The drand48() function generates the next 48-bit integer value in a sequence of 48-bit integer values, X(i), according to the linear congruential formula: X(n+1) = (aX(n) + c)mod(2**48) n>=0. The initial values of X, a, and c are: X(0) = 1 a = 5deece66d (base 16) c = b (base 16)


1 Answers

If you explicitly set -stc=c++03 you will get the same error. This is because drand48 and friends are not actually a part of any C++ standard. gcc includes these functions as an extension, and disables them if standard behaviour is requested.

The default standard mode of g++ is actually -std=gnu++03. You may want to use -std=gnu++11 instead of -std=c++0x, or pass -U__STRICT_ANSI__ to the compiler.

like image 111
n. 1.8e9-where's-my-share m. Avatar answered Sep 28 '22 01:09

n. 1.8e9-where's-my-share m.