2014-10-11 79 views
0

我在Linux中编译时遇到了问题,而且我的工作与许多其他线程和书籍所建议的完全相同,但无法理解我的错误。这里是我的源文件:main.c,process_info.c和process_info.h在Linux上编译许多源文件时出错

现在我忽略了我的代码的正确性,因为我试图编译它以查看它是否工作并修复错误,关于(我对Linux非常陌生,在没有使用IDE的情况下没有进行任何调试)。

编辑关于薄荷Linux的17我的消息来源 - 肉桂,我想这对终端:

gcc main.o process_info.o -o newmain 

main.o: In function `main': 
main.c:(.text+0x26): undefined reference to `readFile' 
collect2: error: ld returned 1 exit status 

而且我也试着这样做:

gcc -Wall main.c process_info.c -o newmain 

/tmp/ccnBKqCu.o: In function `main': 
main.c:(.text+0x26): undefined reference to `readFile' 
collect2: error: ld returned 1 exit status 

我不知道为什么我因为我认为我在做正确的事情,所以我得到了错误。

下面是我的源文件的内容(里面的代码是不正确的,但正在测试):

/****************************************************************** 
* 
* File: main.c 
* Description: Main file to execute code 
* 
*******************************************************************/ 
#include "process_info.h" 

int main() { 

    char *msg; 
    char msg_buffer[MAX_BUFF_SIZE]; 
    char file_buffer[MAX_BUFF_SIZE]; 
    int fileDes[FILE_DESC_SIZE]; 

    readFile(file_buffer); 

    msg = "hello world\n"; 
    pipe(fileDes); 

    if (fork() == IS_CHILD) { 
     // This is the child process 
     printf("child process: \n"); 
     write(fileDes[WRITE_INDEX], msg,MESSAGE_SIZE); 
    exit(0); // ends process 

    } 

    // This is the parent process 
    read(fileDes[READ_INDEX],msg_buffer, MESSAGE_SIZE); 
    write(WRITE_INDEX,msg_buffer, MESSAGE_SIZE); 

return 0; 

} 

/****************************************************************** 
* 
* File: OS_Proj1.h 
* Description: OS Fork and Pipe Project header 
* 
*******************************************************************/ 
#ifndef PROCESS_INFO_H 
#define PROCESS_INFO_H 

#include<stdlib.h> 
#include<stdio.h> 
#include <unistd.h> 
#include <stdbool.h> 

#define MAX_BUFF_SIZE 1024 // common value 
#define FILE_DESC_SIZE 2 
#define IS_CHILD  0 
#define MESSAGE_SIZE 12 
#define READ_INDEX  0 
#define WRITE_INDEX 1 
#define PERMISSION  0 
#define FILE_DIR_1  "/workspace/CSCI_474/Project1/filedat1" 
#define FILE_DIR_2  "/workspace/CSCI_474/Project1/filedat2" 
#define FILE_DIR_3  "/workspace/CSCI_474/Project1/filedat3" 
#define FILE_DIR_4  "/workspace/CSCI_474/Project1/filedat4" 
#define FD_ERROR  -1 

// Function prototypes 
void readFile(char *buffer); 


#endif 


/****************************************************************** 
* 
* File: process_info.c 
* Description: Main file to execute code 
* 
*******************************************************************/ 
#include "process_info.h" 
#include <fcntl.h> 

void readFIle(char *buffer){ 
    int fileDesc; 
    char filename[] = FILE_DIR_1 ; 

    fileDesc = open(filename,O_RDONLY,PERMISSION); 

    // test if file opened successfully 
    if(fileDesc == FD_ERROR){ 
    perror("Cannot open output file\n"); 
    exit(1); 
    } 
    // read the file if it opened successfully 
    else { 
    while( read(fileDesc,buffer,MAX_BUFF_SIZE) > 0 ) { 
     write(WRITE_INDEX,buffer,MAX_BUFF_SIZE); 
    } 

    } 
} 
+0

你应该使用'gcc -Wall -g'编译你的源文件(然后使用'gdb'调试器) – 2014-10-11 14:36:11

+2

当连接器发现它找不到你已经定义的符号时,总是先检查拼写。 – 2014-10-11 14:38:47

+0

@JoachimPileborg,感谢您的建议我很惭愧,我没有听清楚,我很习惯被IDE明确地吼过,但我没有注意到这个问题。 – user1945925 2014-10-11 14:57:41

回答

3

很简单:)

你READFILE定义,但声明的名字是ReadFile和你想叫它它有没有定义。检查两者的拼写。

+0

不能相信我错过了最基本的故障排除任务....我没有注意到,因为我习惯于在这样的事情发生时被IDE大吼大叫。 – user1945925 2014-10-11 14:55:47

3
readFIle 

readFile 

SO要求我输入更多的文字,所以在这儿呢。

+0

感谢看起来每个人都很快抓到了这个,我投了你的回应,但是我先把它给了下面的人,因为他先回答了。 – user1945925 2014-10-11 14:59:39