Thread: error: `O_CREAT' undeclared (first use in this function)

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question error: `O_CREAT' undeclared (first use in this function)

    I'm have the compile error
    Code:
    error: `O_CREAT' undeclared (first use in this function)
    in the line
    Code:
    start_bets[i] = sem_open(nome,O_CREAT|O_EXCL,0777,0);
    even though I have done
    Code:
    #include <semaphore.h>
    What other problems could generate this error?

    Thanks for any help

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    A search on my Mac using Spotlight does not show semaphore.h as being a "top pick" (I didn't see it actually, but I didn't scroll down all the way) for O_CREAT. I did see fcntl.h and a few others though.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    I tried that and get even worse errors. It stops recognizing sem_open althogether.

    Code:
    /tmp/ccOHDxUk.o(.text+0x59): In function `main':
    : undefined reference to `pthread_create'
    /tmp/ccOHDxUk.o(.text+0x101): In function `main':
    : undefined reference to `pthread_join'
    /tmp/ccOHDxUk.o(.text+0x3a0): In function `init':
    : undefined reference to `sem_unlink'
    /tmp/ccOHDxUk.o(.text+0x3ba): In function `init':
    : undefined reference to `sem_open'
    /tmp/ccOHDxUk.o(.text+0x46e): In function `init':
    : undefined reference to `sem_unlink'
    /tmp/ccOHDxUk.o(.text+0x488): In function `init':
    : undefined reference to `sem_open'
    /tmp/ccOHDxUk.o(.text+0x4d8): In function `init':
    : undefined reference to `sem_unlink'
    /tmp/ccOHDxUk.o(.text+0x4fa): In function `init':
    : undefined reference to `sem_open'
    /tmp/ccOHDxUk.o(.text+0x5b1): In function `end_clean':
    : undefined reference to `sem_unlink'
    /tmp/ccOHDxUk.o(.text+0x63d): In function `end_clean':
    : undefined reference to `sem_unlink'
    /tmp/ccOHDxUk.o(.text+0x678): In function `end_clean':
    : undefined reference to `sem_unlink'
    If O_CREAT isn't in semaphore.h why doesn't on the man pages say so?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You need semaphore.h AND fcntl.h.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Unhappy

    If I include both I get those errors I mentioned.

    Here is a sample code that has the same prob
    Code:
    #include <semaphore.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    sem_t* start_race;
    
    int main(void){
    	sem_unlink("start_race"); //remove se já  existir
    	if (SEM_FAILED != start_race){
    		//sem_setvalue(start_race, 0, 0);
    		start_race = sem_open("start_race",O_CREAT|O_EXCL,0777,0);
    	}
    	else{
    		printf("Erro ao criar o semaforo start_race.\n");
    		exit(0);
    	}
    }
    Like that I get
    Code:
    namedSemaphores.c: In function `main':
    namedSemaphores.c:11: error: `O_CREAT' undeclared (first use in this function)
    namedSemaphores.c:11: error: (Each undeclared identifier is reported only once
    namedSemaphores.c:11: error: for each function it appears in.)
    namedSemaphores.c:11: error: `O_EXCL' undeclared (first use in this function)
    With fcntl.h I get
    Code:
    /tmp/ccAx0hpm.o(.text+0x25): In function `main':
    : undefined reference to `sem_unlink'
    /tmp/ccAx0hpm.o(.text+0x47): In function `main':
    : undefined reference to `sem_open'
    collect2: ld returned 1 exit status
    Please help! I don't know what more to do!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Did you try reading any of the replies?
    Code:
    #include <semaphore.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    sem_t* start_race;
    
    int main(void){
    	sem_unlink("start_race"); //remove se já  existir
    	if (SEM_FAILED != start_race){
    		//sem_setvalue(start_race, 0, 0);
    		start_race = sem_open("start_race",O_CREAT|O_EXCL,0777,0);
    	}
    	else{
    		printf("Erro ao criar o semaforo start_race.\n");
    		exit(0);
    	}
    }
    Both.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    That what I'm doing!

    Look, with the code
    Code:
    #include <semaphore.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    
    sem_t* start_race;
    
    int main(void){
    	sem_unlink("start_race"); //remove se já  existir
    	if (SEM_FAILED != start_race){
    		//sem_setvalue(start_race, 0, 0);
    		start_race = sem_open("start_race",O_CREAT|O_EXCL,0777,0);
    	}
    	else{
    		printf("Erro ao criar o semaforo start_race.\n");
    		exit(0);
    	}
    }
    I get
    Code:
    /tmp/ccgdq412.o(.text+0x25): In function `main':
    : undefined reference to `sem_unlink'
    /tmp/ccgdq412.o(.text+0x47): In function `main':
    : undefined reference to `sem_open'
    collect2: ld returned 1 exit status
    Maybe I'm missing something of what you said, but that code is as far as I understood from reading the posts.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    It compiles fine here. sem_open is a system call, at least in BSD. What system are you on?

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    I'm compiling on a server, so I'm not sure.
    Is there a command for asking the system version?

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Ok with uname I got
    Code:
    Linux student2.dei.uc.pt 2.6.11-1.27_FC3smp #1 SMP Tue May 17 20:43:11 EDT 2005 i686 i686 i386 GNU/Linux
    Is that what you need?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Undefined references are linker errors, not compiler errors right? So that would mean that it's a library issue. What libraries are you linking in and what one is required to support what you're doing?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    I'm compiling with the command
    Code:
     gcc -Wall -std=c99 namedSemaphores.c -o namedSemaphores
    Not sure if this is what you asked

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    According to linux "man sem_overview":
    Linking
    Programs using the POSIX semaphores API must be compiled with cc -lrt to link against the real-time library, librt.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    YOU ARE LIFESAVERS!

    Thank you so very much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Consumer program
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-27-2006, 05:21 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM