0

So in short, I have two .c files and a shared.h header file in the same directory.

This is shared.h:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <string.h>
    #include <fcntl.h>
    #include <signal.h>
    #include <errno.h>


       // declaring the number to factor and the the variable
        factor as shared gloval variables
    extern int n;
    extern int factor;

This is pfact.c:

    #include "shared.h"
    int n;
    int  factor = 0;
    // start main
    int main(int argc, char **argv) {

    // fork
    // if child, exec to child

    return 0;
    }

This is child.c:

   #include "shared.h"

    int main(){

    int m=0, k;
    int status, child_exit_status;
    pid_t pid;
    int fd[2];    //every child will create another pipe to write      to in addition to the pipe it inherits from its parent to read from

    printf("n is %d\n", n);


   // goes more code 

    return 0
    }

What am I doing wrong? the global variable n is declared once in pfact.c, "externed" in the header file shared.h and then the header file is included in child.c

Thanks in advance!

  • You don't need `extern int n;` etc. in child.c, because those declarations are in `shared.h` which you include. Make sure you are actually linking both `child.c` and `pfact.c`'s objects into your executable? – M.M Mar 14 '14 at 00:35
  • removed. still getting the same problem: undefined reference to n when doing: gcc -o child child.c – user3417884 Mar 14 '14 at 00:40
  • 1
    You need to add `pfact.c` onto that line, the compiler doesn't magically know that `pfact.c` is something you want to put into `child` – M.M Mar 14 '14 at 00:46
  • I type: bash-4.1$ gcc -Wall child.c and I get: child_here.c: In function ‘main’: child_here.c:7: warning: unused variable ‘pid’ child.c:6: warning: unused variable ‘child_exit_status’ child.c:6: warning: unused variable ‘status’ child.c:5: warning: unused variable ‘k’ child.c:5: warning: unused variable ‘m’ /tmp/cc3vvCiE.o: In function `main': child.c:(.text+0x11): undefined reference to `n' collect2: ld returned 1 exit status – user3417884 Mar 14 '14 at 00:48
  • What is `child_here.c` and why did you put it twice? You still don't have `pfact.c`. – M.M Mar 14 '14 at 00:48
  • that's the actual name of child.c. where do I need pfact.c? do I need to compile and line using both files? But the two files, pfact.c and child.c contain main functions. – user3417884 Mar 14 '14 at 00:52
  • You can only have one `main` function. Put `pfact.c` on the end of your `gcc` line and delete one of the `main` functions. – M.M Mar 14 '14 at 00:55
  • I fork in pfact.c and i get the newly created child to perform exec(path, child, NULL). I'm trying to use the global variable n in the newly forked and execed process. – user3417884 Mar 14 '14 at 00:57
  • `extern` doesn't mean "look in a separate process that might not even be running", it means the variable can be found in another `.c` file that is part of the same executable. If you want one process to share data with a separate process that you start via `exec`, then you will have to use some sort of inter-process communication channel as supported by your operating system. – M.M Mar 14 '14 at 00:59

2 Answers2

2

Those two lines in child.c are useless, you can remove it

extern int n;
extern int factor;

This could help you understand why:

How do I use extern to share variables between source files?

Child doesn't know n so you can add it in global in child.c but it's certainly not why you are trying to do.

You can't compile two main, you should maybe rethink the way you do your program.

Community
  • 1
  • 1
tcollart
  • 1,032
  • 2
  • 17
  • 29
0

You need to link your objects together...

gcc -g -Wall -c child.c
gcc -g -Wall -c pfact.c
gcc -g -Wall -o pgm child.o pfact.o

Re: extern lines useless: Yes, they aren't needed in pfact.c; but it is good practice to #include the header with the declaration anyway, so the compiler can cross-check that everything matches.

vonbrand
  • 11,412
  • 8
  • 32
  • 52