作者:百变精灵99 | 来源:互联网 | 2023-05-19 01:09
SoImadeaprogramthatusesfourdifferrentprocessestodosomestuff.Iworksperfectly,butnow
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:
所以我制作了一个程序,使用四个不同的过程来做一些事情。我工作得很好,但现在我想把它拆分,所以每个进程都有自己的.c文件。我尝试使用包含所有进程的函数原型的头文件“processes.h”来完成此操作。它看起来像这样:
#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.
所有包含函数#include“processes.h”的实现的.c文件,以及main函数(使用functionTable来设置进程)也包括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?
这是分割成不同文件的正确方法吗?或者我是否需要为每个.c文件单独的.h文件?
2 个解决方案