开发者

Using sem_t in a Qt Project

开发者 https://www.devze.com 2022-12-31 16:45 出处:网络
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.

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 开发者_如何学JAVAhow 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'


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.


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


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

What is your INCLUDEPATH set to in the pro file?

0

精彩评论

暂无评论...
验证码 换一张
取 消