使用 -lrt 时 Posix Semaphore 编译错误

发布于 2024-12-27 17:20:46 字数 6358 浏览 4 评论 0原文

可能的重复:
sem_open() 错误:“ Linux (Ubuntu 10.10) 上对 sem_open() 的未定义引用

编译 posix 信号量时遇到问题。我的目标是创建一个共享内存段并通过信号量保护它。共享内存工作正常,但信号量代码给了我编译错误,即使我包含 semaphore.h 并将 -lrt 添加到编译标志,

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <time.h>


int main (int argc, char** argv) {
    FILE *configFile;
    int i,j;


    int CashDesksNo=0;
    int maxCashDesksNo;
    int n,m;
    int TmaxServe;
    int custPerc; //customer ratio policy
    int maxCapacity;


    char * nlptr=NULL;
    char * pch=NULL;
    char line[125];

    char termInput[30];
    char tmpString[40];

    int flg1,flg2;
    int flag;

    int execResult=0;
    int fd;
    int rc;

    int randNum;

    int status;
    pid_t ch_pid;

    int a,b,c;
    int shmid=0;
    int *shm_ptr;
    int * err;

    int retval;
    sem_t *sp;
    char semName[10];

    strcpy(semName,"mutex");
    //---------- Davasma kai elegxos in line parameters-----------------
        //    read inline params and config file    
                              .
                              .
                              .

    //------------ Print Configuration Data ----------------------------
    if(CashDesksNo>maxCashDesksNo || CashDesksNo<1)
    {
        printf("\n# of Cash Desks should be between 1 and %d",maxCashDesksNo);
        printf("\nsupermarket will now exit...\n");
        exit(1);
    }

    printf("\n//-----------------------------------------------");
    printf("\nSupermarket initialization");
    printf("\n# of Cash Desks: %d",CashDesksNo);
    printf("\n# of max products: %d",n);
    printf("\nMax price: %d",m);
    printf("\nMaximum serving time(secs): %d",TmaxServe);
    printf("\n%% Customer/Cashier Percentage: %d/%d",custPerc,100-custPerc);
    printf("\nMax Supermarket capacity: %d",maxCapacity);
    printf("\n//-----------------------------------------------\n");

    printf("\nAbout to create customer and cashier processes");

// ----------- Shared Memory Attachment --------------------------------
shmid=shmget(IPC_PRIVATE,sizeof(int*),0666); 

if (shmid < 0)
{
    perror("shmget");
    exit(1);
}

shm_ptr=(int*)shmat(shmid,(void *)0,0);

if (shm_ptr == (int *)(-1))
{
    perror("shmat");
    exit(1);
}

a=0;    //shm
shm_ptr=(int*)a;
printf("shmPtr:%d",(int)shm_ptr);



// ----- create & initialize semaphore ---------------------------------

  sp = sem_open(semName,O_CREAT,0644,1);
  if(sp == SEM_FAILED)
    {
      perror("unable to create semaphore");
      exit(-1);
    }


    while(1)
    {
        printf("\nPress enter to start a new day at the supermarket(type exit to quit)\n");
        fgets(termInput,sizeof(termInput),stdin);

        nlptr = strchr(termInput, '\n');// termatismos string
        if (nlptr) *nlptr = '\0';

        if(strcmp(termInput,"exit")==0)//exit apo tin efarmogi
        {
            printf("\nExiting Supermarket..\n");
            exit(0);
        }

        i=0;
        while(i<maxCapacity)
        {


            //-Fork new process for the simulation --------------------

            ch_pid = fork();

            if (ch_pid == -1) {
            perror("\nFailed to fork initial spliter/merger process  \n");
            exit(1);
            }


            if ( ch_pid == 0 ) //this is the child process
            {
            srand (getpid());//pid based seed

            // "itoa" - Metatropi ari8mou se string
            sprintf( tmpString, "%d", shmid );

            randNum=rand() % 100 + 1;
            if(randNum<custPerc)// customer : cashier ratio
            {
                execResult=execl("customer","customer","0",tmpString,NULL);
                //printf("\nCreated a customer,randNum %d",randNum);
            }else
            {
                execResult=execl("cashier","cashier","0",tmpString,NULL);
                //printf("\nCreated a cashier,randNum %d",randNum);

            }

                if(execResult==-1)
                {
                    perror("Could not perform exec to create cashier/customer process");
                }

            }
        i++;        
        }

        //Root process
        //wait for childs to terminate
                for (i = 0; i < maxCapacity; ++i) 
                {
                    waitpid(-1,&status,0);
                }
        printf("supermarket shared memory content: %d",(int)shm_ptr);
        sem_close(sp);
        sem_unlink(semName);

        err = (int*)shmctl(shmid, IPC_RMID, 0);
        if (err == -1) 
            perror ("Shared Memory Removal.");
        else 
            printf("Shared Memory Removed. %d\n", (int)(err));


    }



} 

这是 makefile:

OBJS    = supermarket.o cashier.o customer.o 
SOURCE  = supermarket.c cashier.c customer.c 
HEADER  = struct.h
OUT     = supermarket cashier customer
CC  = gcc
FLAGS   = -lrt -g -c 
LIBS    = -lm
# -g option enables debugging mode 
# -c flag generates object code for separate files
# -lm math library

all: supermarket cashier customer

supermarket: supermarket.c
    $(CC) supermarket.c -o supermarket

cashier: cashier.c
    $(CC) cashier.c -o cashier

customer: customer.c
    $(CC) customer.c -o customer


# clean house
clean:
    rm -f $(OBJS) $(OUT)

# do a bit of accounting
count:
    wc $(SOURCE) $(HEADER)

我不断收到此错误:

george@george-System-Product-Name:~/Desktop/prj3$ make
gcc supermarket.c -o supermarket
supermarket.c: In function ‘main’:
supermarket.c:310:11: warning: comparison between pointer and integer [enabled by default]
/tmp/ccYxA2Wi.o: In function `main':
supermarket.c:(.text+0x75c): undefined reference to `sem_open'
supermarket.c:(.text+0x9b0): undefined reference to `sem_close'
supermarket.c:(.text+0x9bf): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [supermarket] Error 1

我能做什么? 这是一个操作系统类的项目,我的系统是linux Ubuntu

Possible Duplicate:
sem_open() error: “undefined reference to sem_open()” on linux (Ubuntu 10.10)

Having issues with compilation of posix semaphores. My goal is to create a shared memory segment and protect it by semaphores. shared memory works fine but semaphores code gives me compilation errors even though I included semaphore.h and added -lrt to compilation flags

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <time.h>


int main (int argc, char** argv) {
    FILE *configFile;
    int i,j;


    int CashDesksNo=0;
    int maxCashDesksNo;
    int n,m;
    int TmaxServe;
    int custPerc; //customer ratio policy
    int maxCapacity;


    char * nlptr=NULL;
    char * pch=NULL;
    char line[125];

    char termInput[30];
    char tmpString[40];

    int flg1,flg2;
    int flag;

    int execResult=0;
    int fd;
    int rc;

    int randNum;

    int status;
    pid_t ch_pid;

    int a,b,c;
    int shmid=0;
    int *shm_ptr;
    int * err;

    int retval;
    sem_t *sp;
    char semName[10];

    strcpy(semName,"mutex");
    //---------- Davasma kai elegxos in line parameters-----------------
        //    read inline params and config file    
                              .
                              .
                              .

    //------------ Print Configuration Data ----------------------------
    if(CashDesksNo>maxCashDesksNo || CashDesksNo<1)
    {
        printf("\n# of Cash Desks should be between 1 and %d",maxCashDesksNo);
        printf("\nsupermarket will now exit...\n");
        exit(1);
    }

    printf("\n//-----------------------------------------------");
    printf("\nSupermarket initialization");
    printf("\n# of Cash Desks: %d",CashDesksNo);
    printf("\n# of max products: %d",n);
    printf("\nMax price: %d",m);
    printf("\nMaximum serving time(secs): %d",TmaxServe);
    printf("\n%% Customer/Cashier Percentage: %d/%d",custPerc,100-custPerc);
    printf("\nMax Supermarket capacity: %d",maxCapacity);
    printf("\n//-----------------------------------------------\n");

    printf("\nAbout to create customer and cashier processes");

// ----------- Shared Memory Attachment --------------------------------
shmid=shmget(IPC_PRIVATE,sizeof(int*),0666); 

if (shmid < 0)
{
    perror("shmget");
    exit(1);
}

shm_ptr=(int*)shmat(shmid,(void *)0,0);

if (shm_ptr == (int *)(-1))
{
    perror("shmat");
    exit(1);
}

a=0;    //shm
shm_ptr=(int*)a;
printf("shmPtr:%d",(int)shm_ptr);



// ----- create & initialize semaphore ---------------------------------

  sp = sem_open(semName,O_CREAT,0644,1);
  if(sp == SEM_FAILED)
    {
      perror("unable to create semaphore");
      exit(-1);
    }


    while(1)
    {
        printf("\nPress enter to start a new day at the supermarket(type exit to quit)\n");
        fgets(termInput,sizeof(termInput),stdin);

        nlptr = strchr(termInput, '\n');// termatismos string
        if (nlptr) *nlptr = '\0';

        if(strcmp(termInput,"exit")==0)//exit apo tin efarmogi
        {
            printf("\nExiting Supermarket..\n");
            exit(0);
        }

        i=0;
        while(i<maxCapacity)
        {


            //-Fork new process for the simulation --------------------

            ch_pid = fork();

            if (ch_pid == -1) {
            perror("\nFailed to fork initial spliter/merger process  \n");
            exit(1);
            }


            if ( ch_pid == 0 ) //this is the child process
            {
            srand (getpid());//pid based seed

            // "itoa" - Metatropi ari8mou se string
            sprintf( tmpString, "%d", shmid );

            randNum=rand() % 100 + 1;
            if(randNum<custPerc)// customer : cashier ratio
            {
                execResult=execl("customer","customer","0",tmpString,NULL);
                //printf("\nCreated a customer,randNum %d",randNum);
            }else
            {
                execResult=execl("cashier","cashier","0",tmpString,NULL);
                //printf("\nCreated a cashier,randNum %d",randNum);

            }

                if(execResult==-1)
                {
                    perror("Could not perform exec to create cashier/customer process");
                }

            }
        i++;        
        }

        //Root process
        //wait for childs to terminate
                for (i = 0; i < maxCapacity; ++i) 
                {
                    waitpid(-1,&status,0);
                }
        printf("supermarket shared memory content: %d",(int)shm_ptr);
        sem_close(sp);
        sem_unlink(semName);

        err = (int*)shmctl(shmid, IPC_RMID, 0);
        if (err == -1) 
            perror ("Shared Memory Removal.");
        else 
            printf("Shared Memory Removed. %d\n", (int)(err));


    }



} 

this is the makefile:

OBJS    = supermarket.o cashier.o customer.o 
SOURCE  = supermarket.c cashier.c customer.c 
HEADER  = struct.h
OUT     = supermarket cashier customer
CC  = gcc
FLAGS   = -lrt -g -c 
LIBS    = -lm
# -g option enables debugging mode 
# -c flag generates object code for separate files
# -lm math library

all: supermarket cashier customer

supermarket: supermarket.c
    $(CC) supermarket.c -o supermarket

cashier: cashier.c
    $(CC) cashier.c -o cashier

customer: customer.c
    $(CC) customer.c -o customer


# clean house
clean:
    rm -f $(OBJS) $(OUT)

# do a bit of accounting
count:
    wc $(SOURCE) $(HEADER)

I keep getting this error:

george@george-System-Product-Name:~/Desktop/prj3$ make
gcc supermarket.c -o supermarket
supermarket.c: In function ‘main’:
supermarket.c:310:11: warning: comparison between pointer and integer [enabled by default]
/tmp/ccYxA2Wi.o: In function `main':
supermarket.c:(.text+0x75c): undefined reference to `sem_open'
supermarket.c:(.text+0x9b0): undefined reference to `sem_close'
supermarket.c:(.text+0x9bf): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [supermarket] Error 1

what can i do?
this is a project for operating systems class, my system is linux Ubuntu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2025-01-03 17:20:46

我认为您也应该链接到 pthread:

-lpthread

来自我的旧项目的示例 makefile(请参阅评论):

CC=gcc
CFLAGS=-c -Wall -O3 -g
LDFLAGS=-pthread
SOURCES=chatzor.c clientlist.c messagequeue.c
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=chatzor_server

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
        $(CC) $(CFLAGS) 
lt; -o $@

clean:
    rm -rf *.o ${EXECUTABLE}

I think you should link against pthread as well:

-lpthread

Example makefile from an old project of mine (see comments):

CC=gcc
CFLAGS=-c -Wall -O3 -g
LDFLAGS=-pthread
SOURCES=chatzor.c clientlist.c messagequeue.c
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=chatzor_server

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
        $(CC) $(CFLAGS) 
lt; -o $@

clean:
    rm -rf *.o ${EXECUTABLE}
滥情稳全场 2025-01-03 17:20:46

完成此操作后,您的小腿将会酸痛。

编译跟踪说:

$ make
gcc supermarket.c -o supermarket
supermarket.c: In function ‘main’:
...

你的 makefile 说:

supermarket: supermarket.c
    $(CC) supermarket.c -o supermarket

它应该说(至少):

supermarket: supermarket.c
    $(CC) supermarket.c -o supermarket $(LIBS)

事实上,它可能应该说:

supermarket: supermarket.c
    $(CC) $(CFLAGS) supermarket.c -o supermarket $(LDFLAGS) $(LIBS)

You're going to have sore shins once you're done with this.

The compilation trace says:

$ make
gcc supermarket.c -o supermarket
supermarket.c: In function ‘main’:
...

Your makefile says:

supermarket: supermarket.c
    $(CC) supermarket.c -o supermarket

It should say (at minimum):

supermarket: supermarket.c
    $(CC) supermarket.c -o supermarket $(LIBS)

Indeed, it should probably be saying:

supermarket: supermarket.c
    $(CC) $(CFLAGS) supermarket.c -o supermarket $(LDFLAGS) $(LIBS)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文