tags:

views:

137

answers:

3

Hi everyone,

I'm working on a simulation in Qt (C++), and would like to make use of a Semaphore wrapper class I made for the sem_t type.

Although I am including semaphore.h in my wrapper class, running qmake provides the following error:

'sem_t does not name a type'

I believe this is a library/linking error, since I can compile the class without problems from the command line.

I've read that you can specify external libraries to include during compilation. However, I'm a) not sure how to do this in the project file, and b) not sure which library to include in order to access semaphore.h.

Any help would be greatly appreciated.

Thanks,

Tom

Here's the wrapper class for reference:

Semaphore.h

#ifndef SEMAPHORE_H
#define SEMAPHORE_H

#include <semaphore.h>

class Semaphore {
public:
    Semaphore(int initialValue = 1);
    int getValue();
    void wait();
    void post();

private:
    sem_t mSemaphore;
};

#endif

Semaphore.cpp

#include "Semaphore.h"

Semaphore::Semaphore(int initialValue) {
    sem_init(&mSemaphore, 0, initialValue);
}

int Semaphore::getValue() {
    int value;
    sem_getvalue(&mSemaphore, &value);
    return value;
}

void Semaphore::wait() {
    sem_wait(&mSemaphore);
}

void Semaphore::post() {
    sem_post(&mSemaphore);
}

And, the QT Project File:

TARGET = RestaurantSimulation
TEMPLATE = app
QT +=
SOURCES += main.cpp \
  RestaurantGUI.cpp \
  RestaurantSetup.cpp \
  WidgetManager.cpp \
  RestaurantView.cpp \
  Table.cpp \
  GUIFood.cpp \
  GUIItem.cpp \
  GUICustomer.cpp \
  GUIWaiter.cpp \
  Semaphore.cpp
HEADERS += RestaurantGUI.h \
  RestaurantSetup.h \
  WidgetManager.h \
  RestaurantView.h \
  Table.h \
  GUIFood.h \
  GUIItem.h \
  GUICustomer.h \
  GUIWaiter.h \
  Semaphore.h
FORMS += RestaurantSetup.ui
LIBS += 

Full Compiler Output:

g++ -c -pipe -g -gdwarf-2 -arch i386 -Wall -W -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -  
I/usr/local/Qt4.6/mkspecs/macx-g++ -I. - 
I/Library/Frameworks/QtCore.framework/Versions/4/Headers -I/usr/include/QtCore -
I/Library/Frameworks/QtGui.framework/Versions/4/Headers -I/usr/include/QtGui -
I/usr/include -I. -I. -F/Library/Frameworks -o main.o main.cpp

In file included from RestaurantGUI.h:10,
from main.cpp:2:
Semaphore.h:14: error: 'sem_t' does not name a type
make: *** [main.o] Error 1
make: Leaving directory `/Users/thauburger/Desktop/RestaurantSimulation'
Exited with code 2.
Error while building project RestaurantSimulation
When executing build step 'Make'
A: 

Why don't you use semaphore mechanism provided by the Qt framework? I'd use QSemaphores just to stay within Qt ecosystem.

Tadeusz A. Kadłubowski
Yes, I saw Qt provided QSemaphores. I was trying to avoid using them since we are only employing Qt for the UI, and the vast majority of our core business logic is already using the sem_t type via my wrapper class.
thauburger
A: 

QMake adds external include path using INCLUDEPATH
like INCLUDEPATH += include_dir

What is your INCLUDEPATH set to in the pro file?

Vicken Simonian
I've added the pro file above. No INCLUDEPATH parameter was created in the default project file generated by Qt Creator.
thauburger
+2  A: 

I was able to compile and link your semaphore class using qmake without any unexpected steps (including linking in the rt or pthread libraries). I created the following main:

#include "Semaphore.h"

int main(int argc, char* argv[])
{
        Semaphore sem;
        return 0;
}

And then I generated the following project file using qmake -project:

######################################################################
# Automatically generated by qmake (2.01a) Mon May 24 12:50:02 2010
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += Semaphore.h
SOURCES += main.cpp Semaphore.cpp

Whatever error you're seeing is caused by something other than your Semaphore class. I'd recommend taking a good look at your RestaurantGUI.h file. You may need to take a look at the preprocessed output (gcc's -E flag) in order to see what's really happening.

NOTE: I'd recommend renaming your semaphore files to something that will work on case-insensitive filesystems, such as Windows.

Kaleb Pederson
+1: Fine on my system too.
Troubadour
Thanks Kaleb, Troubadour! Turns out, Kaleb's suggestion about naming was the fix. I renamed the Semaphore class to THSemaphore, and the project built successfully. I had never run into problems with case-insensitivity before, but a valuable lesson for a young programmer. Can't get away with #include Semaphore.h and <semaphore.h>!
thauburger