0

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);
    }

  }
}
5
  • You should compile your source files with gcc -Wall -g (then use the gdb debugger) Commented Oct 11, 2014 at 14:36
  • 2
    When the linker complains that it can't find a symbol you know you have defined, always check spelling first. Commented Oct 11, 2014 at 14:38
  • @JoachimPileborg, thanks for the advice I'm ashamed that I didn't catch that, I'm so used to being explicitly yelled at by IDEs I didn't notice that problem. Commented Oct 11, 2014 at 14:57
  • @BasileStarynkevitch, I'll take a look at what -g is research on how to use gdb. Thanks. Commented Oct 11, 2014 at 14:58
  • @user1945925: You were "yelled at" by the compiler this time, so what? ;-) Commented Oct 11, 2014 at 17:17

2 Answers 2

3

very simple :)

You have readFIle defined but declaration name is readFile and you are trying to call it which has no definition. Check the spelling of both.

Sign up to request clarification or add additional context in comments.

1 Comment

can not believe I missed the most basic troubleshooting task....i didn't notice since I am used to being yelled at by the IDE when things like that happen.
3
readFIle

readFile

SO demands I enter more text, so here it is.

1 Comment

Thanks it seems like everyone caught this really quick, I up-voted your response but I gave it to the guy below since he answered first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.