Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sleep() in C++

Status
Not open for further replies.

brianon

Programmer
Feb 6, 2002
22
IE
Hi,

I'm using a call to sleep() in main() of a C++ program.

I've got #include unistd.h but when I compile using gnu c++ compiler I get the following error .......
" 'sleep' undeclared (first use of this function) "

Anybody got any ideas why ? I'm stumped :(

Cheers,
Brian.
 
Strange... of course you gave it its parameter did you ?
(e.g. sleep(5)) and did you do the include like this
#include <unistd.h> ??
The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
yes and yes :)

its very strange alright as I can't get anything done with this stuff until I try and get it all to compile.

Any more ideas ?

Cheers,
Brian.
 
Have you tried usleep? :) ?
The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
I'can use sleep and I include stdlib.h,
you can try that one perhaps.
The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
I just trid it now but it gives same error.

'usleep' undeclared !?

Could it be compiler flags ?

Brian.

 
I'm already including <sdtlib.h>

Brian.
 
hmmm...
the most primitive test program I could find here that
used sleep incuded the following:
stdlib.h
stdio.h
sys/types.h
sys/ipc.h
sys/shm.h

(and that was a test prog for shared mem)

hom do you compile ? The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
I've taken a look in /usr/include unistd.h,and
that is the file to use...
So, check if the file is on your pc,
also check in the file (search for the sleep function)
Normally that should all be there...
...of course it is there,otherwise you should have got
the compiler error &quot;can't find unstd.h&quot;

perhaps

#include &quot;/usr/include/unistd.h&quot;

will do the trick. The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
this works on my pc:

#include <iostream.h>
#include <unistd.h>

void main()
{
cout<<&quot;hello&quot;<<endl;
sleep(5);
cout<<&quot;after 5 seconds&quot;<<endl;
}

compiled with:

c++ test.cc -o test

So what it can be is:

1) the function isn't in the file unistd.h
2) your lib directory in 'path' is other than /usr/include
and there there is also a file unistd.h without sleep
(this is impossible)

I can't think of anything else... The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
I was using gnu gcc ver 3.0.3 and was just told that its not the best version ever so I got my hands on gnu gcc ver 2.95.3 and that seems to have solved the problem.

Very strange indeed.
Thanks for your help.

Cheer,
Brian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top