I am having trouble compiling in Linux and I am doing exactly the same as many other threads and books have suggested but can not understand my error. Here are my source files: main.c, process_info.c, and process_info.h
For now I am ignoring the correctness of my code because I am trying to compile it to see if it works and fix errors and they come about (I am very new to Linux and haven't done any debugging without using an IDE).
To compile my source on Mint Linux 17 - Cinnamon, I am trying this to the terminal:
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
And I also tried doing:
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
I don't know why I am getting the error since I thought I was doing things right.
Below is the content of my source files (the code inside isn't correct but under testing):
/******************************************************************
*
* 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);
}
}
}
gcc -Wall -g(then use thegdbdebugger)