CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Mar 2019
    Posts
    17

    srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Dev-C++ version 5.5.2 (Windows 7) gives compilation error that occurs in the last line in main():

    [Error] 'srand48' was not declared in this scope

    Code:
    #include <sys/time.h>
    #include <cmath>
    #include <cstdlib>
    #include <stdlib.h>
    
    extern double drand48();
    extern long lrand48(/*long*/);
    extern int rand();
    extern void srand(long seedval);
    
    
    
    //main program
    main()
    {
    
    //set random number generator
    struct timeval tp;
    struct timezone tzp;
    gettimeofday(&tp,&tzp);
    srand48(tp.tv_usec); //compilation error occurs here: [Error] 'srand48' was not declared in this scope
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Sure, you've declared srand as external but are calling srand48.

  3. #3
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Quote Originally Posted by Arjay View Post
    Sure, you've declared srand as external but are calling srand48.
    Please tell me what to do then. I am not clear as not able to find literature for that.
    I request that the exact change or literature link be provided.
    Due to lack of any knowledge, I naively tried : extern long srand48(long seedval);, but that causes no change.


    Thanks in advance.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    A quick check shows that if the version of MinGW that came bundled with Dev-C++ 5.5.2 properly conformed to POSIX, then #include <stdlib.h> should have been sufficient: you don't need to forward declare those functions yourself. But, apparently you do.

    Solution: upgrade to the latest version of (Orwell) Dev-C++ (which was released nearly four years ago back in April 2015, so it is hardly cutting edge software). It comes bundled with a version of g++ that supports C++11, upon which you can just #include <random> and use the C++11 standard pseudorandom number generator facilities.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Thanks for suggesting Posix, that prompted me to use cygwin. I ran on Cygwin & got it running using g++. However, the code was problem area reported in a bigger code on Simulated Annealing, which is from book by Alan Parker, Algorithms and Data Structures in C++, in last section of chapter 3. I am still facing the Segmentation fault on running g++ there, with the command: $ g++ -Wall -Wextra -Werror -c sp3.cpp -o sp3.o; followed by ./sp2. The full code is at: https://onlinegdb.com/HyruMTmdN. It reported the same error at onlinegdb too, but in absence of any details provided, cannot understand what is causing the error.
    Last edited by ajiten73; March 24th, 2019 at 07:31 AM.

  6. #6
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    I ran on Cygwin & got it running using g++. However, the code was problem area reported in a bigger code on Simulated Annealing, which is from book by Alan Parker, Algorithms and Data Structures in C++, in last section of chapter 3. I am still facing the Segmentation fault on running g++ there, with the command: $ g++ -Wall -Wextra -Werror -c sp3.cpp -o sp3.o; followed by ./sp2. The full code is at: https://onlinegdb.com/HyruMTmdN. It reported the same error at onlinegdb too, but in absence of any details provided, cannot understand what is causing the error. Am attaching the file for code too. sp2.cpp
    Last edited by ajiten73; March 24th, 2019 at 07:31 AM.

  7. #7
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Sorry, the code had two logical bugs reported as warnings too. The new code is here: https://onlinegdb.com/SJ0dzGSd4, and attached as file as:sp_1.cpp

    But, the program is taking infinite time to run, both in onlinegdb, & in cygwin. The book even states that the program may not be able to find soln.
    It ended after around 30 mins. in cygwin, & am giving the last line of outputs:


    Calculated cost 1
    data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<data[present_op[i]]<<++**++*+++

    It seems all is wrong, but would see why cost is 1 only.

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Alan Parker, Algorithms and Data Structures in C++
    That is a very old book - published 1993 - and uses pre-ISO standard c++(the first standard was published 1998 known as c++98). This code is a curious mix of c & c++ syntax (with incorrect code in places). It certainly does not represent how to write standard c++ code. If you are learning c++, then this is not how you write it. You should consult a much more up-to-date c++ book/reference (preferably covering c++17 - the latest standard). if you are learning algorithms, then I strongly suggest you use a more up-to-date book (one definitely published after 2011 when the c++11 standard was produced).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Quote Originally Posted by 2kaud View Post
    That is a very old book - published 1993 - and uses pre-ISO standard c++(the first standard was published 1998 known as c++98). This code is a curious mix of c & c++ syntax (with incorrect code in places). It certainly does not represent how to write standard c++ code. If you are learning c++, then this is not how you write it. You should consult a much more up-to-date c++ book/reference (preferably covering c++17 - the latest standard). if you are learning algorithms, then I strongly suggest you use a more up-to-date book (one definitely published after 2011 when the c++11 standard was produced).
    I am able to run, but would take your suggestion in consideration. I wanted to learn AI algorithms, but the coding in Python was cumbersome. Also, the existing Python code revealed a little due to so many in-built commands & commands for data-types. Hence wanted code in C/C++. Could not find any good starter code, so came to the book, even though could find only one algorithm.

    I had put single printf statement in each function to see the program's working, but then it took around 30 mins. to complete (by just stating : ./sp_c). The code for this approach is at : https://onlinegdb.com/HJNfkjSuV. The file is at : sp_c.cpp.
    However, the absence of the same caused it to to execute in fraction of a second (i.e., by just stating : ./sp_nc). I am curious if could see the effect of putting printf for performance purposes, 'without' putting up printf (as done in the first version). Please help in this regards.
    The second version without any printf is at: https://onlinegdb.com/Bkeuc5H_V. The file is at : sp_nc.cpp

    Then tried to use alternate approach to see the difference in time taken. For it used the 'time' command.
    The command use for the second version was (on cygwin) : $ g++ sp_nc.cpp -o sp_nc, time ./sp_nc | tail -3

    If follow the same approach, then the first approach (i.e. time ./sp_c | tail -3) took seconds to complete (around 10 times slower now). I wonder why just stating : ./sp_c takes half an hour to complete.

    I am curious if the delay in the commented version is a function of printf (that caused to have only a 10 times difference), or a function of display. By display, I mean the screen display of the printf took so much addtl. time. If so, then the display is another factor, & hence causes a very high order of difference in time taken, i.e. around 30 mins. vs 3-4 seconds, as for program timed for printf.
    Attached Files Attached Files
    Last edited by ajiten73; March 24th, 2019 at 07:53 PM.

  10. #10
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    I am unable to find the significance of '(lrand48() >> 4)' in the statement at line #54 :
    Code:
     build_list->side = (lrand48() >> 4)%(SQUARE_SIZE_LIMIT)+1;
    , for the code in function: void get_list_start(square_list ** list), in file sp_nc.cpp.

    Any pointer in that direction would help me a lot.

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    >> 4 means divide by 16 (shift right by 4 bits). This is the same as (lrand48() / 16) but is quicker without certain compiler optimisations. So the result of lrand48() is first divided by 16 (and truncated to an integer). Then the remainder of integer division by SQUARE_SIZE_LIMIT is obtained (%) and then 1 is added.

    Output to the console (using printf() or c++ <<) is 'very slow'. Hence having a number of display statements in a program will slow down the timings - how much depends, of course, on how much output is displayed. Usually such output displays are used for debug purposes only and once the program is working then these are removed and the program compiled 'as release' which performs program optimisations. The 'debug' compile version performs very limited optimisation (but can be debugged, traced etc) and hence the performance is generally much slower than when compiled as 'release'.
    Last edited by 2kaud; March 25th, 2019 at 03:10 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Quote Originally Posted by 2kaud View Post
    >> 4 means divide by 16 (shift right by 4 bits). This is the same as (lrand48() / 16) but is quicker without certain compiler optimisations. So the result of lrand48() is first divided by 16 (and truncated to an integer). Then the remainder of integer division by SQUARE_SIZE_LIMIT is obtained (%) and then 1 is added.

    Output to the console (using printf() or c++ <<) is 'very slow'. Hence having a number of display statements in a program will slow down the timings - how much depends, of course, on how much output is displayed. Usually such output displays are used for debug purposes only and once the program is working then these are removed and the program compiled 'as release' which performs program optimisations. The 'debug' compile version performs very limited optimisation (but can be debugged, traced etc) and hence the performance is generally much slower than when compiled as 'release'.
    I am confused by two aspects of program:
    1. random number generators are used everywhere, I feel this use can be avoided in many places.
    For e.g., line #54 (as stated in my last request) in code can avoid the random number generator function's usage.
    2. Why the output of the program is wrong: as there should be only a permutation of the initial configuration
    given by line #33, for 11 squares' sides (with five of side =1, one of side =2, four of side = 3, one of side =6):
    Code:
        int test_set[NO_SQUARES]={1,1,1,1,1,2,3,3,3,3,6};
    But, the output is given as :
    Calculated cost 1
    11111111111+**+****+*

    I have run program many times in cygwin with $ g++ sp_nc.cpp -o sp_nc.o; followed by ./sp_nc. Every time got the same output.
    Might be the usage of random number generators is the cause. Please guide.
    Attached Files Attached Files
    Last edited by ajiten73; March 25th, 2019 at 11:05 AM.

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    If the output of the program is not as expected, then you'll need to debug the program to see where its execution differs from that expected.

    Re lines 53, 56, 62. The random number generator here is used only if the compile-time TEST has not been defined. If TEST has been defined, then testt_set is used.

    Note that TEST is defined at line 17 - hence the random number generator is not used for build_list ->side.

    Also note that srand48() 'seeds' the random number generator with an initial 'random' number so that the sequence of generated random numbers should be different every time the program is run. if you want the program to be repeatable - the same sequence of random numbers every time the program is run, then just use something like srand48(1) instead. The sequence will now be the same for each run of the program. When you want a different sequence, just change srand48() back to its original value.
    Last edited by 2kaud; March 25th, 2019 at 11:34 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Mar 2019
    Posts
    17

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Quote Originally Posted by 2kaud View Post
    If the output of the program is not as expected, then you'll need to debug the program to see where its execution differs from that expected.

    Re lines 53, 56, 62. The random number generator here is used only if the compile-time TEST has not been defined. If TEST has been defined, then testt_set is used.

    Note that TEST is defined at line 17 - hence the random number generator is not used for build_list ->side.

    Also note that srand48() 'seeds' the random number generator with an initial 'random' number so that the sequence of generated random numbers should be different every time the program is run. if you want the program to be repeatable - the same sequence of random numbers every time the program is run, then just use something like srand48(1) instead. The sequence will now be the same for each run of the program. When you want a different sequence, just change srand48() back to its original value.
    Thanks for making the purpose of random number generators clear.
    I also could trace the error why all square sides are = 1, in line #57; & replaced it with :
    Code:
    build_list ->side = test_set[i];
    Still cannot find the reason why am not able to get a correct configuration, like one in book (stated as output of running the program) as :
    11+2*11*1*3+3++33*6+*

    (I wanted to show the output by some sort of attached file, or a code. But, could not find any online/offline tool to draw so many boxes. So, choose scanned copy of hand-drawing for that.
    Have made one, & is attached below. I am unable to attach the file, tried but failed.
    Then started for c++ code, but seems that SFML is the best but a long-drawn way. So quit that option. )

    My code (after modifn. at line no. 57) is giving wrong outputs as the operators (all 10) are listed at the end.
    I am getting different answers each time on compiling my below code, two of which are listed below:
    1. 11313331162++*+*+*+++
    2. 61132133131*++++++***
    Please help me in pointing out where in code such error crops in.sp_nc.cppName:  result.jpg
Views: 2459
Size:  23.8 KB
    Last edited by ajiten73; March 26th, 2019 at 06:26 AM.

  15. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: srand48() : Unable to compile due to scope issues on Windows in Dev-C++.

    Like I said in post #13

    If the output of the program is not as expected, then you'll need to debug the program to see where its execution differs from that expected.
    Nobody here is going to debug your program for you. You need to do this and find where its execution differs from that expected. As you have stated, there are several places where rand() is used. For cases like this, I would be looking to replace these temporarily with a known value for which you know the result/output. Then trace through the code and see where its execution differs from that expected. When you this, you'll know of an error in the code. Thuis process of debugging and testing etc is repeated until the program performs as expected for defined values. Once working with these, then the rand() statements can be used again.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured