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':
doSomething'
a.c:(.text+0xba): undefined reference to
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?
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.
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);
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With