编辑:在 C 模块中初始化我的项目的常量并在相应的标头中声明它们解决了问题。
我正在致力于制作 AES 密码的 C 实现(链接: https: //github.com/SuperTotoGo/AES_Cipher)。一切都工作正常,直到我尝试将我的项目分成模块。现在,当我编译时,我收到此错误:
gcc -std=c99 -g -o aes.out *.c
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:8: multiple definition of `AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:8: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:28: multiple definition of `INV_AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:28: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:48: multiple definition of `AES_LOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:48: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:68: multiple definition of `AES_ALOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:68: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:88: multiple definition of `AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:88: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:96: multiple definition of `INV_AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:96: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:104: multiple definition of `RCON'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:104: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:8: multiple definition of `AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:8: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:28: multiple definition of `INV_AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:28: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:48: multiple definition of `AES_LOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:48: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:68: multiple definition of `AES_ALOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:68: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:88: multiple definition of `AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:88: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:96: multiple definition of `INV_AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:96: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:104: multiple definition of `RCON'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:104: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:8: multiple definition of `AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:8: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:28: multiple definition of `INV_AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:28: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:48: multiple definition of `AES_LOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:48: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:68: multiple definition of `AES_ALOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:68: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:88: multiple definition of `AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:88: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:96: multiple definition of `INV_AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:96: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:104: multiple definition of `RCON'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:104: first defined here
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我的函数原型在头文件中定义,我的函数在相应的 c 文件中实现,根据我对模块化项目的理解,我通常只需要在主文件中包含头文件即可使其工作。
那么,你知道我做错了什么吗?
另外,如果您对制作模块化 C 项目有任何建议,或者对我如何做到这一点有任何建议,请告诉我!
您在 aes_const.h 头文件中定义了多个变量。因此,编译的每个源文件都有这些变量的副本。因此,当所有内容都链接起来时,您最终会得到多个定义。
将这些变量定义移至单个源文件中,然后将extern它们的声明放入头文件中。
所以标题应该包含:
extern const uint8_t AES_SUB_BOX[16][16];
extern const uint8_t INV_AES_SUB_BOX[16][16];
extern const uint8_t AES_LOG_TABLE[16][16];
extern const uint8_t AES_ALOG_TABLE[16][16];
extern const uint8_t AES_MULT_MAT[4][4];
extern const uint8_t INV_AES_MULT_MAT[4][4];
extern uint8_t RCON[10];
Run Code Online (Sandbox Code Playgroud)
现有定义应移至 .c 文件,例如 aes_const.c。