Dining Philosophers / C++/ Linux question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gotejjeken
    New Member
    • Sep 2007
    • 27

    Dining Philosophers / C++/ Linux question

    I am attempting to compile this in Fedora:

    http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/c++/philosopher.cc

    I've used all of the source files and the makefile provided yet I am getting the errors:

    undefined reference to 'msecond'
    undefined reference to 'random_int'

    Our Linux teacher was very sketchy on how to actually write makefiles / compile programs in general (he basically told us to go read the Man Pages and use Google) so I've been using these lines to compile, make, and finally run the program:

    Code:
    g++ -lpthread dining_philosophers.cc
    make
    ./dining_philosophers
    If I can get this to compile and run I can actually see what the Dining Philosophers problem is supposed to look like, and then be able to start my own program.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    msecond.h is available in the path and r u specifying the path properly?
    Also in the file compile option is being specified.

    Raghuram

    Comment

    • Gotejjeken
      New Member
      • Sep 2007
      • 27

      #3
      Yeah I wasn't compiling right, I wasn't linking the two .h files with the .cpp/cc file. This line worked though:

      Code:
      g++ dining_philosophers.cc -lpthread msecond.o random_int.o
      Thanks for the tip.

      Comment

      • Gotejjeken
        New Member
        • Sep 2007
        • 27

        #4
        Alright I have another question regarding this. I modified the program so it works for any number of Philosophers, however the downside is that number has to be a predefined constant. I'm using this Semaphore class:

        Code:
        class Semaphore
        {
        private:
             sem_t semaphore
        public:
             Semaphore(int value = 1) {(void) sem_init(&semaphore,0,value);};
           ~Semaphore(void)   {(void) sem_destroy(&semaphore);};
             void wait(void)    {(void)sem_wait(&semaphore);};
             void signal(void) {(void)sem_post(&semaphore);};
        };
        This will take an array declaration like:

        Code:
        Semaphore a[index];
        Does anyone know of a way to make a Semaphore based class take a vector structure? I need to use a Vector or some other structure that takes a variable input, so I can scale the number of Philosophers needed.

        Or can I just create a generic Semaphore to block access while I do work on a normal vector? Such as:

        Code:
        s.wait();
        //-- Vector operations here
        s.signal();

        Comment

        • Gotejjeken
          New Member
          • Sep 2007
          • 27

          #5
          The edit button disappeared >_<. Anyway, I figured the above out, turns out I didn't have to use a vector afterall.

          Anyway, I'm trying to use a makefile to put this thing all together, yet it's throwing errors at me. Anyone know what's wrong with this:

          Code:
          CC = g++ 
          CFLAGS = -Wall -Ansi 
          OBJECTS = dining_philosophers.o msecond.o random_int.o
          project2: $(OBJECTS)
          	$(CC)$(CFLAGS)$(OBJECTS) -o dining_philosophers msecond random_int
          dining_philosophers.o: dining_philosophers.cpp
          	$(CC)$(CFLAGS)$(OBJECTS) -lpthread dining_philosophers.cpp
          msecond.o: msecond.c msecond.h
          random_int.o: random_int.c random_int.h
          
          #clean
          clean:
          	/bin/rm -f dining_philosophers*.o
          The terminal looks like this (with errors):

          Code:
          g++ -Wall -Ansi dining_philosophers.o msecond.o random_int.o -lpthread dining_philosophers.cpp
          g++: dining_philosophers.o: No such file or directory
          g++: msecond.o: No such file or directory
          g++: random_int.o: No such file or directory
          <command line>:1:4: error: missing '(' after predicate
          make: *** [dining_philosophers.o] Error 1
          I'm really new with makefiles, so do I have to do something else to make it generate the .o files?

          Comment

          Working...