Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 796
James Raitsev Avatar asked May 05 '11 02:05

James Raitsev


2 Answers

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.

like image 164
Jonathan Leffler Avatar answered Sep 28 '22 02:09

Jonathan Leffler


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

.

like image 22
mattnz Avatar answered Sep 28 '22 04:09

mattnz