Compiler error message help

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Hi, I'm trying to complete a project for school involving the use of semaphores. I have included the proper header files (<semaphore.h> plus one for pthreads). I have pointed the compiler to the proper libraries as well. This is written in C. Yes, this is an assignment, but please be aware I am not looking for help with implementation, rather I can't seem to figure out this damnable compile error.

Here are lines 47 through 50 of my code, which are "simple" declarations of the semaphores and initializing them:

47 sem_t empty;
48sem_init(0, 0, 5);
49
50 sem_t full;
51 sem_init(&full, 0, 0);

Here are the messages I'm getting when I try and compile for line 48. I get the same set for line 50 but didn't post it for the sake of brevity:
|48|error: expected declaration specifiers or ?...? before ?&? token|
|48|error: expected declaration specifiers or ?...? before numeric constant|
|48|error: expected declaration specifiers or ?...? before numeric constant|
|48|warning: data definition has no type or storage class|
|48|warning: type defaults to ?int? in declaration of ?sem_init?|

I have declared all these outside the main() function. How can I resolve these errors? I'm baffled because it seems to indicate no data type for sem_t, but it is defined in semaphore.h, which I have included.

Thanks in advance for the help.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
What compiler are you using?

Try compiling this:
1 #include <stdio.h>
2 #include <semaphore.h>
3
4 int main() {
5 sem_t foo;
6 sem_init(&foo,0,5);
7 return 0;
8 }

This compiles fine under gcc:
gcc foo.c -lpthread
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Yes, that compiles for me too. FWIW I tried declaring the bit in my OP within the main(), and that did no good either.

I am using Code::Blocks as an IDE, the compiler is GCC. I get the same messages if I compile from the command line as well.

Any other suggestions?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
What is "Ø"? Is that supposed to be NULL?
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
I'm such a n00b. In a fit of amnesia I had overlooked where I placed the sem_init() functions, which degibson had pointed out was the problem. Perhaps I should hold off on the ether until _after_ my homework is done.

Anyway, thanks degibson!