C Makefile collect2:错误:

时间:2014-05-25 19:11:27

标签: c gcc

Processor.c

#include<time.h>
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include<unistd.h>
#include"Process_struct.h"

sem_t empty;//semaphores
#define MAX_PROCS 5
#define EXIT 1
#define TRUE 1

char outbaseStr [100];
int numProcessors;
FILE *outLog=NULL;
FILE *file=NULL;
FILE *outFile=NULL;
FILE *temp=NULL;
pthread_t producer;//Producer Thread ID
pthread_t consumer[MAX_PROCS];//consumer thread ID

int main(int argc, char *argv[])
{
    /* Initialize Data */
    initializeData();

    printf("argc equals %d\n", argc);
    int num_processors=atoi(argv[2]);
    int case_num=atoi(argv[3]);
    char filename;
    char *outfilename=argv[1];

    printf("outfilename equals %s\n", outfilename);
    printf("num_processors equals %d\n\n", num_processors);

    switch(case_num)
    {
    case 1:
        //printf("case 1\n");
        /* Reading in from the text  */

        file = fopen("temp.txt", "wr");

        /* fopen returns 0, the NULL pointer, on failure  */
        if(file==0||file==NULL)
        {
            printf("Error: couldn't open the file\n");
            exit(EXIT);
        }

/*****************************************************************************/
//write to temp
        int length=argc-4;
        for(int i=0;i<length;i++)
        {
            if(i%2==0)
            {
                //printf("%d ", atoi(argv[i+3]));
                fprintf(file, "%d ", atoi(argv[i+4]));
            }
            else
            {
                //printf("%d\n", atoi(argv[i+3]));
                fprintf(file,"%d\n", atoi(argv[i+4]));
            }
        }

/*****************************************************************************/

        fclose(file);
        file = fopen("temp.txt", "r");

        /* Create the producer thread  */
        pthread_create(&producer, NULL, get_request, (void *)file);

        break;
    case 2:

        //char filename[100];
        //filename=argv[3];
        //printf("usage: %s filename\n", argv[3]);


        /* Reading in from the text  */
        file = fopen(argv[4], "r");

        /* fopen returns 0, the NULL pointer, on failure  */
        if(file==0||file==NULL)
        {
            printf("Error: couldn't open the file\n");
            exit(EXIT);
        }


        /* Create the producer thread  */
        pthread_create(&producer, NULL, get_request, (void *)file);

        break;
    default:
        printf("Error: should be either case 1 or case 2\n");
        exit(EXIT);
        break;
    }

    pthread_join(producer, NULL);


    //displayQ();


    // Create the consumer threads
    for(int i=0;i<num_processors;i++)
    {

        sprintf(outbaseStr, "%s.%ld", outfilename, (long)(i+1));
        //printf("outbaseStr equals %s\n", outbaseStr);
        outLog=fopen(outbaseStr, "w");
        if(outLog==NULL)
        {
            printf("Error: couldn't open the file\n");
            exit(EXIT);
        }

        pthread_create(&consumer[i], NULL, processor, (void *)outLog);
    }


    for(int i=0;i<num_processors;i++)
    {
        pthread_join(consumer[i], NULL);
    }

    //printf("\nfclose\n");
    close((FILE *)file);
    //fclose((FILE *)file);

    if(case_num==1)
    {
        if(remove("temp.txt")!=0)
        {
            printf("error deleting file");
        }
        else
        {
            //printf("success deleting file");
        }
    }

}

Process_struct.c

 #include<time.h>
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include<unistd.h>
#include"Process_struct.h"

sem_t empty;//semaphores
#define MAX_PROCS 5
#define EXIT 1
#define TRUE 1

char outbaseStr [100];
int numProcessors;
FILE *outLog=NULL;
FILE *file=NULL;
FILE *outFile=NULL;
FILE *temp=NULL;
pthread_t producer;//Producer Thread ID
pthread_t consumer[MAX_PROCS];//consumer thread ID

void initializeData()
{
    //printf("initializeData\n");
    //Create the empty semaphore and initialize it
    sem_init(&empty, 0, MAX_PROCS);

//  pthread_attr_init(attr);
}
void *get_request(void *argv)//this produces a queue
{

    prcmd_t *process;
    //printf("get_request\n");

    if(file==NULL)
    {
        printf("\nwe have a file null error\n");
    }

    while(!feof(file))
    {

        process=(prcmd_t *)malloc(sizeof(prcmd_t));

        fscanf(file, "%d %d", &process->owner, &process->burst_time);

        if(process->owner!=0||process->burst_time!=0)
        {
            //printf("%d %d\n", process->owner, process->burst_time);

            if(add_queue(process)==-1)
            {
                printf("failure from add_queue");
            }

        }
    }
    //printf("this is the end of get_request\n");

}
void *processor(void *argv)//this consumes a queue
{
    prcmd_t *process;
//  process=(prcmd_t *)malloc(sizeof(prcmd_t));
    process=pr_head;
    int sleep_time=process->burst_time;

    //printf("\nprocessor\n");
    while(TRUE)
    {
        if(get_number_request()>0)
        {

            if(remove_queue(&process)==0)
            {
                fprintf(outLog, "\n->Process with id %d and it %d de-equeue by thread \n", process->owner, process->burst_time);
                clock_t start=clock();
                sleep(process->burst_time);
                clock_t end=clock();
                fprintf(outLog, "\nSlept for %d seconds\n", sleep_time);
            }


        }
        close((FILE *)outLog);
        //fclose((FILE *)outLog);

        //printf("the end of the processor\n");
        //displayQ();
        return NULL;
    }
}
int get_number_request()
{
    return pending_request;
}
void displayQ()
{
    printf("\n\nthis is the beginning of displayQ\n");

    prcmd_t *process=pr_head;

    do
    {
        printf("%d %d\n", process->owner, process->burst_time);
        process=process->next;

    }while(process!=NULL);

    printf("\nthe end of displayQ\n\n");
}
int add_queue(prcmd_t *node)
{
    prcmd_t *temp;
    temp=node;

    pthread_mutex_lock(&prmutex);
    //printf("add_queue\n");

    //printf("%d %d\n", temp->owner, temp->burst_time);

    /* adding a linkedlist to a queue */
    if(pr_head==NULL)//then pr_tail==NULL
    {
        //printf("pr_head==NULL\n");

        temp->next=NULL;
        pr_head=temp;
        pr_tail=temp;
    }
    else
    {
        //printf("pr_head!=NULL\n");
        temp->next=NULL;
        pr_tail->next=temp;
        pr_tail=temp;
    }


    pending_request++;
    pthread_mutex_unlock(&prmutex);
    //printf("add_queue success\n");
    return(0);
}
int remove_queue(prcmd_t **node)
{
    //printf("\nremove_queue/enqueue\n");
    pthread_mutex_lock(&prmutex);

    prcmd_t *temp;

//  printf("this is the end of remove_queue/enqueue and is returning 0\n");

    if(pr_head==NULL)
    {
        //printf("pr_head==NULL");
        pr_head = pr_tail = NULL;        // Reset everything to empty queue

        pthread_mutex_unlock(&prmutex);
        //printf("this is the end of remove_queue/enqueue and is returning -1\n");
        return(-1);
    }
    else
    {
        //printf("pr_head!=NULL\n");
        temp=pr_head;
        temp->next=pr_head->next;
        pr_head=temp->next;

        pending_request--;
        pthread_mutex_unlock(&prmutex);
        //printf("this is the end of remove_queue/enqueue and is returning 0\n");
        return(0);
    }
}

Process_struct.h

   GNU nano 2.2.6            File: Process_struct.h

#include<time.h>
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include<unistd.h>

typedef struct pr_struct
{
        int owner;
        int burst_time;
        struct pr_struct *next;

} prcmd_t;

void displayQ();//this displays the queue
void initializeData();//initializes the data for the program
void *get_request(void *args);//to be calls as a thread to enqueue input reques$
void *processor(void *args);//which removes a request from the process request $
int get_number_request();//returns the number of request
int add_queue(prcmd_t *);//adds a node at the end of the request queue
int remove_queue(prcmd_t **);//removes a node for the queue

#define MAX_PROCS 5
#define EXIT 1
#define TRUE 1

这是我的错误:

    /tmp/ccvDJUQI.o:(.bss+0x0): multiple definition of `outLog'
    /tmp/cc4RWdZ4.o:(.bss+0x0): first defined here
    /tmp/ccvDJUQI.o:(.bss+0x8): multiple definition of `file'
    /tmp/cc4RWdZ4.o:(.bss+0x8): first defined here
    /tmp/ccvDJUQI.o:(.bss+0x10): multiple definition of `outFile'
    /tmp/cc4RWdZ4.o:(.bss+0x10): first defined here
    /tmp/ccvDJUQI.o:(.bss+0x18): multiple definition of `temp'
    /tmp/cc4RWdZ4.o:(.bss+0x18): first defined here
    collect2: error: ld returned 1 exit status
    make: *** [Multiprocessor] Error 1

每当我去运行make文件时,这就是我得到的错误。我不明白错误会在哪里,所以我无法找到它并进行更改。我希望有人能告诉我什么是collect2:错误:ld返回1退出状态意味着错误将在何处。

2 个答案:

答案 0 :(得分:1)

从您的评论中,看起来您正在尝试编译头文件

  

gcc Processor.c Process_struct.h Process_struct.c -o Multiprocessor -std = c99 -lm -lpthread

头文件包含在.c文件中,不单独编译。您的命令行应该更像

  

gcc Processor.c Process_struct.c -o Multiprocessor -std = c99 -lm -lpthread


此类错误的另一个来源是,您在头文件中定义变量并将头文件包含在多个源文件中,例如在Process_struct.h

FILE *file = NULL;
char temp[] = "/tmp";

当您在Processor.c Process_struct.c中包含此标头文件时,您可以在两个来源中定义这些变量,从而获得多个已定义的变量。

要解决此问题,您不能定义,只能在头文件中声明变量。然后,您可以在一个源文件中定义它们,例如

Process_struct.h

extern FILE *file;
extern char temp[];

Process_struct.c

FILE *file = NULL;
char temp[] = "/tmp";

multiple definitions的又一个来源就是,你已经在多个地方定义了相同的变量。这意味着当你有

Processor.c:

FILE *file = NULL;
char temp[] = "/tmp";

Process_struct.c:

FILE *file = NULL;
char temp[] = "/tmp";

你会收到这个错误。解决这个问题取决于你的意图。如果这些变量是文件的本地变量(彼此独立),则通过前缀static

将其范围缩小到文件
static FILE *file = NULL;
static char temp[] = "/tmp";

但是,如果要在这两个源之间共享变量,则必须保留一个定义,并使另一个定义仅为声明。更好的是,将声明移动到头文件并将定义仅保留在一个源文件中,如上面第二部分所述。


在任何情况下,您都应该以不同的方式构建makefile。尽可能使用内置规则,请参阅Using Implicit Rules。例如。

CFLAGS = -std=c99 -pthread
LDLIBS = -lm -lpthread
OBJS = Processor.o Process_struct.o

Multiprocessor: $(OBJS)
    $(CC) $(CFLAGS) -o $@ $(OBJS) $(LDLIBS)

答案 1 :(得分:0)

对于寻找collect2答案的所有用户:错误:ld返回1退出状态,请记住,将头文件中的所有变量设为 STATIC 变量会很有帮助。这就是让我的错误消失的原因,所有其他用户都可以解决您的错误。