What does the "undefined reference to varName" in C mean?

115,798

Solution 1

You need to link both a.o and b.o:

gcc -o program a.c b.c

If you have a main() in each file, you cannot link them together.

However, your a.c file contains a reference to doSomething() and expects to be linked with a source file that defines doSomething() and does not define any function that is defined in a.c (such as main()).

You cannot call a function in Process B from Process A. You cannot send a signal to a function; you send signals to processes, using the kill() system call.

The signal() function specifies which function in your current process (program) is going to handle the signal when your process receives the signal.

You have some serious work to do understanding how this is going to work - how ProgramA is going to know which process ID to send the signal to. The code in b.c is going to need to call signal() with dosomething as the signal handler. The code in a.c is simply going to send the signal to the other process.

Solution 2

It is very bad style to define external interfaces in .c files. .

You should do this

a.h

    extern void doSomething (int    sig);

a.c

    void doSomething (int    sig)
    {
       ... do stuff 
    }

b.c

#include "a.h"
.....
signal(SIGNAL, doSomething); 

.

Solution 3

An initial reaction to this would be to ask and ensure that the two object files are being linked together. This is done at the compile stage by compiling both files at the same time:

gcc -o programName a.c b.c

Or if you want to compile separately, it would be:

gcc -c a.c
gcc -c b.c
gcc -o programName a.o b.o

Solution 4

You need to compile and then link the object files like this:

gcc -c a.c  
gcc -c b.c  
gcc a.o b.o -o prog  

Solution 5

You're getting a linker error, so your extern is working (the compiler compiled a.c without a problem), but when it went to link the object files together at the end it couldn't resolve your extern -- void doSomething(int); wasn't actually found anywhere. Did you mess up the extern? Make sure there's actually a doSomething defined in b.c that takes an int and returns void, and make sure you remembered to include b.c in your file list (i.e. you're doing something like gcc a.c b.c, not just gcc a.c)

Share:
115,798
James Raitsev
Author by

James Raitsev

I ask a lot of questions. Some of them are good.

Updated on June 12, 2020

Comments

  • James Raitsev
    James Raitsev almost 4 years

    I have 2 files: a.c and b.c

    In a.c I am sending a signal to a function located in b.c

    signal(SIGUSR1,doSomething);
    

    On top of the a.c file, I have:

    extern void doSomething (int    sig);
    

    When I compile, however, I get an error:

    /tmp/ccCw9Yun.o: In function main':
    a.c:(.text+0xba): undefined reference to
    doSomething'
    collect2: ld returned 1 exit status

    The following headers are included:

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

    How do I fix this?

  • James Raitsev
    James Raitsev about 13 years
    The thing is .. a.c and b.c each define each own main function. Compiling .o files result in a error multiple definition of `main'
  • Michael Mrozek
    Michael Mrozek about 13 years
    @mac Well, you need to link them together if one is going to depend on functions in the other; if they have duplicate functions you need to resolve it somehow (maybe pull doSomething out into a third file that a.c and b.c both depend on)
  • James Raitsev
    James Raitsev about 13 years
    I got it. Instead of using signal ()... i used kill(pid_to_send_signal_to, handler);
  • higgs241
    higgs241 almost 11 years
    The first line of this answer inspired me to the solution. In large programs with Makefiles, make sure to include every source file you newly create!