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

1

6

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

Ancestry answered 16/5, 2013 at 7:36 Comment(4)
And in your real code you include <cstdlib>?Conditioning
Yeah, I'll add my include to the post.Ransome
Also remember that you might have to #define either _SVID_SOURCE or _XOPEN_SOURCE before including <cstdlib> (see e.g. linux.die.net/man/3/drand48).Conditioning
It does not seem to make any difference. :SRansome
G
6

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.

Gisela answered 16/5, 2013 at 9:4 Comment(3)
"You may want to use -std=gnu++11" - Rather gnu++0x, since gnu++11 (like c++11) is only available for gcc 4.7+. Or use C++11's platform-independent random number facilities in the first place.Soccer
@ChristianRau it works with -std=gnu++0x. Thank you very much. If you make an answer I will accept it as the solution, otherwise I will mark n.m.s answer as correct :)Ransome
@KimA.Jakobsen It's really n.m.'s answer, just with gnu++0x instead of gnu++11, making that a separate answer would be quite ridiculous from me. What could be an answer would be to use C++11's random number generators instead of C's (which would be preferable over platform-dependent GNU stuff), but I'm not sure that would really answer your actual question either (and I don't understand your code enough for this, anyway).Soccer

© 2022 - 2024 — McMap. All rights reserved.