Thread: Help with error

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    31

    Help with error

    I'm trying to run this sample code and I keep getting this error:

    "process1.cpp:92: implicit declaration of function `int wait(...)' "

    I thought the error was that I didn't include a specific library but I think I have every thing I need. Help please.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    #define SHMSZ    1024
    
    using namespace std;
    
    int write(int *b)
    {
        char c;
        int shmid;
        key_t key;
        int *shm;
        char *s;
    
        /*
        * We'll name our shared memory segment
        * "5678".
        */
        key = 5678;
    
        /*
        * Create the segment.
        */
        if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
            cerr<<"shmget";
            exit(1);
        }
    
        /*
        * Now we attach the segment to our data space.
        */
        if ((shm = (int *)/*(char *)*/shmat(shmid, 0, 0)) == /*(char *)*/(int *) -1) {
            cerr<<"shmat";
            exit(1);
        }
    
        /*
        * Now put some things into the memory for the
        * other process to read.
        */
        // s = shm;
        int i;
        for (i=0; i<=1000; i++,shm++)
            *shm = i;
    
        *b=0;
        cout<<"write is complited"<<endl;
        cout<<*b<<endl;
        return(*b);
    }
    main()
    {
    
        char c;
        int shmid;
        key_t key;
        static int *b;
    
        key = 5679;
    
        /*
        * Create the one bit  segment.
        */
        if ((shmid = shmget(key, 1, IPC_CREAT | 0666)) < 0) {
            cerr<<"shmget";
            exit(1);
        }
    
        /*
        * Now we attach the segment to our data space.
        */
        if ((b = ( int *)shmat(shmid, 0, 0)) == (int *) -1) 
        {
            cerr<<"shmat";
            exit(1);
    
        }
        cout<<*b<<endl;
        while(1)
        {
            if(*b==1)
            {
                write(b);
            }
            else
                wait(5);
        }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you haven't declared a function wait, and whilst there is a Linux/Unix system call by the name of wait [and one called write], perhaps you didn't include the header file for that function - just as well, because passing 5 as a constant to wait will not do much useful.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Where is wait() declared? There is one in sys/wait.h, but that one takes a pointer, not an integer.

    Also, main() should be declared as returning an int.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Ok, I declared wait and now it gives me this error. No idea what this means.

    /tmp/ccjgTrxM.o: In function `main':
    /tmp/ccjgTrxM.o(.text+0x212): undefined reference to `wait(void)'
    collect2: ld returned 1 exit status

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MSkiLLz View Post
    Ok, I declared wait and now it gives me this error. No idea what this means.

    /tmp/ccjgTrxM.o: In function `main':
    /tmp/ccjgTrxM.o(.text+0x212): undefined reference to `wait(void)'
    collect2: ld returned 1 exit status
    So, you are calling wait() and declared it how?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Well, I've tried both "int wait()" and "void wait()" and I get the same outcome.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MSkiLLz View Post
    Well, I've tried both "int wait()" and "void wait()" and I get the same outcome.
    Can you post the code for your wait() function and for the code calling wait() - it appears like it's changed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    #define SHMSZ    1024
    
    using namespace std;
    
    int wait();
    
    int write(int *b)
    {
        char c;
        int shmid;
        key_t key;
        int *shm;
        char *s;
    
        /*
        * We'll name our shared memory segment
        * "5678".
        */
        key = 5678;
    
        /*
        * Create the segment.
        */
        if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
            cerr<<"shmget";
            exit(1);
        }
    
        /*
        * Now we attach the segment to our data space.
        */
        if ((shm = (int *)/*(char *)*/shmat(shmid, 0, 0)) == /*(char *)*/(int *) -1) {
            cerr<<"shmat";
            exit(1);
        }
    
        /*
        * Now put some things into the memory for the
        * other process to read.
        */
        // s = shm;
        int i;
        for (i=0; i<=1000; i++,shm++)
            *shm = i;
    
        *b=0;
        cout<<"write is complited"<<endl;
        cout<<*b<<endl;
        return(*b);
    }
    main()
    {
    
        char c;
        int shmid;
        key_t key;
        static int *b;
    
        key = 5679;
    
        /*
        * Create the one bit  segment.
        */
        if ((shmid = shmget(key, 1, IPC_CREAT | 0666)) < 0) {
            cerr<<"shmget";
            exit(1);
        }
    
        /*
        * Now we attach the segment to our data space.
        */
        if ((b = ( int *)shmat(shmid, 0, 0)) == (int *) -1) 
        {
            cerr<<"shmat";
            exit(1);
    
        }
        cout<<*b<<endl;
        while(1)
        {
            if(*b==1)
            {
                write(b);
            }
            else
                wait();
        }
    }

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I think you are misunderstanding how functions work. To call a function (wait() in this case), it must be both declared, and implemented. Just writing:
    Code:
    int wait();
    declares the function wait(), but you still cannot call it without an implementation like so:
    Code:
    int wait() { return something; }
    Now sometimes the function you want to call is implemented in an external library. In this case you need to #include that library's header file, and then link to that library. If the library is part of the platform's C library, then it will be linked to automatically. For instance, this wait() function is implemented in the GNU C library, so you just need to include sys/wait.h in order to use it.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And where do you expect the compiler to find the function wait()? There is no wait function DEFINED, you have only declared it. That tells the compiler "you can expect to find this function somewhere when you link, I'll define it later". But the linker says "Uhoh! No wait function defined", because you haven't actually defined the function.

    What is wait supposed to do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bithub View Post
    Now sometimes the function you want to call is implemented in an external library. In this case you need to #include that library's header file, and then link to that library. If the library is part of the platform's C library, then it will be linked to automatically. For instance, this wait() function is implemented in the GNU C library, so you just need to include sys/wait.h in order to use it.
    But given that there is no fork() in the code posted, there is no point in calling that wait() function.

    Now, we can argue that naming a function the same as a system call is a bad idea - both write and wait are existing system call functions. But in C++, as long as the parameters are different, it will work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Well, how exactly will it be implemented? I'm still sort of a novice at this stuff. Like I said, this is just a sample code that I want to run to understand shared memory. I included the wait header but it still didn't help.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by matsp View Post
    And where do you expect the compiler to find the function wait()? There is no wait function DEFINED, you have only declared it. That tells the compiler "you can expect to find this function somewhere when you link, I'll define it later". But the linker says "Uhoh! No wait function defined", because you haven't actually defined the function.

    What is wait supposed to do?

    --
    Mats
    Its supposed to wait on the other process to finish writing. The author says "One wants to write the data and another wants to read the data to the shared memory."

    So I'm guessing it reads while it wait for the other process to finish writing.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's what I meant by "what is wait supposed to do"? What are you waiting for - better weather, the tide to come in, someone to bring you a beer, or the computer to perform something, the morning paper to arrive... ran out of silly things to wait for.

    But we can not tell you how to write the "wait" function - only you know what you are waiting for.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM