c - 使用 #ifndef 指令在多个 C 代码中重新定义符号

标签 c include directive defined ifndef

我有一个愚蠢的问题,但我不知道它来自哪里。我负责使用 #ifndef 指令来确保我的所有 #include 都不会被重新定义。对于他们三个人来说,不幸的事情正在发生。这是我的多个文件拱门:

t_include.h

#ifndef T_INCLUDE_H_
#define T_INCLUDE_H_

/* Project specific dependencies*/
#include "utilities.h"
#include "fsp_function.h"

#include "ti/csl/csl_tsc.h"
#include "ti/csl/csl_cache.h"
#include "ti/csl/csl_cacheAux.h"

#include "ti_sp_complex_convolution_A_input1.h"
#include "ti_sp_complex_convolution_A_input2.h"
#include "to_sp_complex_convolution_A_output.h"

#endif /* T_INCLUDE_H_ */

t_function.h

#ifndef T_FUNCTION_H_
#define T_FUNCTION_H_

#include "t_include.h"

/*output vector*/
#define INPUT1A_LENGTH  5000
#define INPUT2A_LENGTH  2800
#define OUTPUTA_LENGTH  2202
extern FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];

/*misc parameter*/
#define CPU_CLOCK_KHZ           1400000
#define CPU_CLOCK_MS            1/CPU_CLOCK_KHZ
#define FIR_NB_MACS             INPUT1A_LENGTH * OUTPUTA_LENGTH     /*   FIR algorithm complexity */
#define NB_OF_REP               10
#define UMA_L2CACHE_L1DCACHE    0

/* Project specific types */
typedef struct{
ect...

现在c文件只包含t_function.h:

t_function.c

/* Dependencies */
#include "t_function.h"
FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
/* API  */
etc...

和 t_main_function.c

/* dependencies */
#include "t_function.h"
void main(void) {
etc...

它应该可以工作,但是在链接期间出现错误:

<Linking>
error #10056: symbol "sp_complex_convolution_A_output" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_input2" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_input1" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"

error #10056: symbol "sp_complex_convolution_A_output_thales" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
>> Compilation failure
error #10010: errors encountered during linking; "CONVOLUTION_COMPLEX.out" not built

因此,错误仅来自三个符号 sp_complex_volving_A_output、sp_complex_volving_A_input1 和 sp_complex_volving_A_input2,它们在自己的 .h 中定义,也受 #ifndef 指令保护:

ti_sp_complex_convolution_A_input1.h

#ifndef __TI_SP_COMPLEX_CONVOLUTION_A_INPUT1_H_
#define __TI_SP_COMPLEX_CONVOLUTION_A_INPUT1_H_

FLOAT32 sp_complex_convolution_A_input1[2 * 2500] = {
etc... 

其他两个也一样......

所以我真的不知道为什么会发生这种情况。 谢谢帮助

最佳答案

定义如下:

FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];

应该进入源文件。

头文件应该只包含如下声明:

extern FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
<小时/>

根据经验,不要将任何分配内存的内容放入头文件中。

关于c - 使用 #ifndef 指令在多个 C 代码中重新定义符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46891730/

相关文章:

c - 如何让用户在我的 C 程序中命名 output.txt 文件?

c - 鉴于我打开了一个文件,有没有办法确定其他进程是否也打开了该文件

python - 从哪里获取适用于 Visual Studio 的 Python DLL?

javascript - 我怎样才能并且应该从不同的文件加载我的 .js 文件以保持清洁?

php - 什么时候脚本会在 PHP 中包含两次?

javascript - 将 html 附加到指令中的元素并创建一个与其交互的本地函数

angular templateRef nativeElement 是空注释而不是原始元素

编译 LD_PRELOAD 包装器的冲突类型

javascript - 从自定义指令内部调用 Angular UI Bootstrap 指令

c - 为什么在 Struct 中使用 -> 运算符在 C 中不起作用