2013-10-09 40 views
0

试图编译此代码,并不断收到“未定义的引用......”编译器错误。我不确定发生了什么事。我被告知,只要您引用其“.h”文件,就不需要包含对“.c”文件的引用。未定义的引用,而使用gcc

我的终端命令:gcc Main.c semaphore.o -L. -lst -o test

/tmp/ccGSIjXz.o: In function `main': 
HW3.c:(.text+0x15): undefined reference to `init_buffer' 
HW3.c:(.text+0x26): undefined reference to `init_buffer' 
HW3.c:(.text+0x37): undefined reference to `init_buffer' 
/tmp/ccGSIjXz.o: In function `Thread1': 
HW3.c:(.text+0x159): undefined reference to `c_deposit' 
/tmp/ccGSIjXz.o: In function `Thread2': 
HW3.c:(.text+0x18c): undefined reference to `c_remove' 
HW3.c:(.text+0x1b9): undefined reference to `c_deposit' 
/tmp/ccGSIjXz.o: In function `Thread3': 
HW3.c:(.text+0x1e8): undefined reference to `c_remove' 
HW3.c:(.text+0x206): undefined reference to `c_remove' 
HW3.c:(.text+0x21a): undefined reference to `c_remove' 
HW3.c:(.text+0x238): undefined reference to `c_deposit' 
HW3.c:(.text+0x252): undefined reference to `c_deposit' 
/tmp/ccGSIjXz.o: In function `Thread4': 
HW3.c:(.text+0x28c): undefined reference to `c_remove' 
collect2: ld returned 1 exit status 

下面的代码:

MAIN.C:

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include "st.h" 
#include "buffer.h" 

#define MAX_CHARS 81 
#define BUFF_SIZE 12 
#define NULL_CHAR 
typedef struct { 
}ThreadInit; 

static buffer *buffer1; 
static buffer *buffer2; 
static buffer *buffer3; 

void *Thread1(); 
void *Thread2(); 
void *Thread3(); 
void *Thread4(); 

int main(int argc, char const *argv[]) 
{ 
    buffer1=init_buffer(BUFF_SIZE); 
    buffer2=init_buffer(BUFF_SIZE); 
    buffer3=init_buffer(BUFF_SIZE); 

    ThreadInit thread1={}; 
    ThreadInit thread2={}; 
    ThreadInit thread3={}; 
    ThreadInit thread4={}; 

    if (st_thread_create(Thread1(), &thread1, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 1"); 
     exit(EXIT_FAILURE); 
    } 

    if (st_thread_create(Thread2(), &thread2, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 2"); 
     exit(EXIT_FAILURE); 
    } 
    if (st_thread_create(Thread3(), &thread3, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 3"); 
     exit(EXIT_FAILURE); 
    } 
    if (st_thread_create(Thread4(), &thread4, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 4"); 
     exit(EXIT_FAILURE); 
    } 


    return 0; 
} 
void *Thread1() 
{ 
    int c; 
    while (1) 
    { 
     c=fgetc(stdin); 
     c_deposit(buffer1,c); 
     if(c==EOF) 
     { 
      break; 
     } 
    } 
    st_thread_exit(NULL); 
    return NULL; 
} 
void *Thread2(void *state) 
{ 
    int c; 
    while(1) 
    { 

     c=c_remove(buffer1); 
     if(c==EOF) 
     { 
      break; 
     } 
     if(c=='\n') 
     { 
      c=' '; 
     } 
     c_deposit(buffer2,c); 
    } 
    st_thread_exit(NULL); 
    return NULL; 

} 
void *Thread3(void *state) 
{ 
    int c; 
    while(1) 
    { 
     c=c_remove(buffer2); 
     if(c==EOF) 
     { 
      break; 
     } 
     if(c=='*' && c_remove(buffer2)=='*') 
     { 
      c_remove(buffer2); 
      c='^'; 
      c_deposit(buffer3,c); 
     } 
     else 
     { 
      c_deposit(buffer3,c); 
     } 
    } 
    st_thread_exit(NULL); 
    return NULL; 
} 
void *Thread4(void *state) 
{ 
    int counter=0; 
    int c; 
    char output[MAX_CHARS]; 
    output[MAX_CHARS-1]='\0'; 
    while(1) 
    { 
     c=c_remove(buffer3); 
     if(c==EOF) 
     { 
      break; 
     } 
     else 
     { 
      output[counter]=c; 
      if(counter==80) 
      { 
       printf("%s",output); 
       counter=-1; 
       memset(output,'\0',BUFF_SIZE); 
      } 
      counter++; 
     } 
    } 
    st_thread_exit(NULL); 
    return NULL; 
} 

buffer.c中:

#include <stdio.h> 
#include <stdlib.h> 
#include "semaphore.h" 
#include "buffer.h" 

buffer *init_buffer(int size) 
{ 

    buffer *new_Buffer; 
    new_Buffer=malloc((sizeof(buffer))); 

    createSem(new_Buffer->emptyBuffer, size); 
    new_Buffer->emptyBuffer=malloc(sizeof(semaphore)); 

    createSem(new_Buffer->fullBuffer, 0); 
    new_Buffer->fullBuffer=malloc(sizeof(semaphore)); 

    new_Buffer->chars=malloc(sizeof(char)*size); 

    new_Buffer->size=size; 

    new_Buffer->nextIn=0; 
    new_Buffer->nextOut=0; 

    return new_Buffer; 
} 

void c_deposit(buffer *buffer, char c) 
{ 
    down(buffer->emptyBuffer); 
    buffer->chars[buffer->nextIn]=c; 
    buffer->nextIn=(buffer->nextIn+1)%buffer->size; 
    up(buffer->fullBuffer); 
} 
int c_remove(buffer *buffer) 
{ 
    int c; 
    down(buffer->fullBuffer); 
    c=buffer->chars[buffer->nextOut]; 
    buffer->nextOut=(buffer->nextOut+1)%buffer->size; 
    up(buffer->emptyBuffer); 
    return c; 
} 

buffer.h:

#include <stdio.h> 
#include "semaphore.h" 

typedef struct{ 
    semaphore emptyBuffer; 
    semaphore fullBuffer; 
    int nextIn; 
    int nextOut; 
    int size; 
    char *chars; 
}buffer; 

void c_deposit(buffer *buffer, char c); 
int c_remove(buffer *buffer); 
buffer *init_buffer(int size); 
+0

这是一个链接器错误。你忘了添加'buffer.c'到文件列表吗? – akonsu

+0

请发布您的'Makefile'或gcc命令行。 –

+0

刚刚添加它,意识到这可能会有所帮助 – sreya

回答

1

变化

gcc Main.c semaphore.o -L. -lst -o test 

gcc Main.c semaphore.o buffer.c -L. -lst -o test 
+0

它说没有这样的文件或目录 – sreya

+0

请参阅从buffer.o到buffer.c的编辑 –

+0

好了,但我现在正在收到一些疯狂的错误那么,我应该只是发表另一个问题吗? – sreya

0

有人告诉我,你并不需要包括一个“.c”的文件的引用,只要你引用其“.h”头文件。

这是不正确的。每个.c源文件需要编译为.o目标文件。目标文件然后链接在一起形成最终的二进制文件。你可以让gcc为你做这个,只需在你的gcc命令行中指定你所有的.c文件。

#include "buffer.h"唯一的问题是,将.h文件的内容复制到当前的.c文件中。你这样做,这样.c文件知道所有引用的类型,以及你将要调用的函数的原型(所以它可以为调用生成正确的代码)。

在这种情况下,您只需将buffer.c添加到您的gcc命令行。

+0

当我说我的意思是,'#include“buffer.h”'和'#include“buffer.c”'' – sreya

+0

* **绝不** ** #include“xxx。 c“' –

+0

好吧,这就是我在你最初突出显示的句子中的含义 – sreya

0

尝试

gcc Main.c buffer.c semaphore.o -L. -lst -o test 

你可能要添加一个原型init_bufferbuffer.h

buffer *init_buffer(int size); 
+0

我其实做了,坏的复制/粘贴 – sreya

相关问题