So I made a program that uses four differrent processes to do some stuff. I works perfectly, but now I want to split it up so each of the processes has its own .c file. I tried to do this using a header file “processes.h” that has the function prototypes for all the processes. It looks like this:
#ifndef PROCESSES_H_
#define PROCESSES_H_
/*------------------------------------------------------------------------------
definitions (defines, typedefs, ...)
------------------------------------------------------------------------------*/
#define NR_OF_PROCESSES 4
enum errCode {
ERR_NONE = 0, ERR_SYNTAX, ERR_OPEN, ERR_TOKEN, ERR_ARG, ERR_END
};
typedef enum errCode ErrCode;
enum toktype {
NUMBER = 0, OPERATOR, ENDOFLINE, ENDOFFILE
};
typedef enum toktype Token;
/*------------------------------------------------------------------------------
function prototypes
------------------------------------------------------------------------------*/
void readProcess(int pfds[5][2]);
void tokenProcess(int pfds[5][2]);
void calculatorProcess(int pfds[5][2]);
void errorProcess(int pfds[5][2]);
/*------------------------------------------------------------------------------
global variable declarations
------------------------------------------------------------------------------*/
void (*functionTable[NR_OF_PROCESSES]) (int pfds[5][2]) = {
readProcess,
tokenProcess,
calculatorProcess,
errorProcess
};
#endif /*PROCESSES_H_*/
All the .c files which contain the implementation of the function #include “processes.h”, and the main function (which uses the functionTable to set up the processes) also includes processes.h.
When I try to compile I get the error:
ld: duplicate symbol _functionTable in /var/folders/eH/eHF8LmdvHzSsNgT01V3jyk+++TI/-Tmp-//ccDgTW2X.o and /var/folders/eH/eHF8LmdvHzSsNgT01V3jyk+++TI/-Tmp-//ccp7zO9L.o
collect2: ld returned 1 exit status
Is this the right way of doing the splitting up into different files? Or do I need a separate .h file for each .c file?
Put this in your .h:
Then put this in exactly one file, e.g. the source file with “main()”: