I’m working on my exam assignment. It’s due in almost 6 hours. Suddenly my program won’t compile anymore with this error message:
gcc -g -D DEBUG -c -o obj/stringops.o src/stringops.c
gcc -g -D DEBUG -c -o obj/arrayops.o src/arrayops.c
gcc -g -D DEBUG -c -o obj/fileops.o src/fileops.c
gcc -g -D DEBUG -c -o obj/builtins.o src/builtins/*.c
gcc -g -D DEBUG -c -o obj/tomashell.o src/tomashell.c
gcc -g -D DEBUG -o bin/tomashell \
obj/stringops.o obj/arrayops.o obj/fileops.o obj/builtins.o \
obj/tomashell.o
obj/tomashell.o: In function `n_processes':
/root/sc/tomashell/src/safefork.c:11: multiple definition of `h_meta'
obj/builtins.o:/root/sc/tomashell/src/builtins/history.c:4: first defined here
obj/tomashell.o: In function `n_processes':
/root/sc/tomashell/src/safefork.c:11: multiple definition of `h_meta_len'
obj/builtins.o:/root/sc/tomashell/src/builtins/history.c:4: first defined here
collect2: ld returned 1 exit status
make: *** [bin/tomashell] Error 1
In this file:
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/errno.h>
extern int errno;
#define MAX_PROCESSES 6
static int n_processes(void)
{ // <- THIS IS LINE 11
return system("exit `ps | wc -l`")/256;
}
pid_t safefork(void)
{
static int n_initial = -1;
if (n_initial == -1)
n_initial = n_processes();
else if (n_processes() >= n_initial+MAX_PROCESSES) {
sleep(2);
errno = EAGAIN; return (pid_t)-1;
}
return fork();
}
Someone please help me or kill me. I don’t want to live in a world where this sort of error is possible.
Any ideas to what might be wrong?
Like the others have said, you should not “define” (either implicitly or explicitly allocate space for) a variable in your headers.
This example might help:
The same, of course, goes for any globals you might define in your .c/.cpp files.
A global can only be “defined” exactly once.