Why do I get "multiple definition" errors when compiling my project? - c

Edit : initializing the constants of my project in a C module and declaring them in a corresponding header resolved the issue.
I am working on making a C implementation of the AES cipher (link : https://github.com/SuperTotoGo/AES_Cipher). Everything was working fine until I tried to separate my project in modules. Now when I compile I get this error :
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
My functions prototypes are defined in the headers and my functions are implemented in the corresponding c files, from what I understood about modular projects I normally only need to include the header files in the main file to have it working.
So, would you have any idea what I did wrong?
Also if you have any tips about making modular C projects or any advice conecerning how I have done it, please tell me!

You have several variables defined in the aes_const.h header file. As a result, each source file that is compiled has a copy of those variables. So when everything in linked, you end up with multiple definitions.
Move these variable definitions into a single source file, then put extern declarations for them in the header file.
So the header should contain:
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];
And the existing definitions should be moved to a .c file, for example aes_const.c.

The prototypes are defined in the headers, which is why the compiler detects all the names and succeeds in compilation. The failure you're getting is during link-time. That is because the linker needs the implementation of the functions, which live in the .c files.
What you need to do is to compile all the .c files and link the resulting object files together. C projects usually build all the object files first (gcc -c *.c) and then link them all together in a separate step (gcc -o aes.out *.o)

Related

Fail to compile the Y86 simulatur

here is the error when i going to make psim.
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x0): multiple definition of `mem_wb_state'; /tmp/ccQX7AGh.o:(.bss+0x120): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x8): multiple definition of `ex_mem_state'; /tmp/ccQX7AGh.o:(.bss+0x128): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x10): multiple definition of `id_ex_state'; /tmp/ccQX7AGh.o:(.bss+0x130): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x18): multiple definition of `if_id_state'; /tmp/ccQX7AGh.o:(.bss+0x138): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x20): multiple definition of `pc_state'; /tmp/ccQX7AGh.o:(.bss+0x140): first defined here
This is the makefile which is under the directory 'pipe'
VERSION=full
GUIMODE=-DHAS_GUI
TKLIBS=-L/usr/lib -ltk -ltcl
TKINC=-isystem /usr/include/tcl8.6
CC=gcc
CFLAGS=-Wall -O2 -DUSE_INTERP_RESULT
How to fix it?

multiple definition in C project

I have a problem with this project (it is a minigame similar to space invaders).
Every time I try to compile it it gives me these errors:
❯ make
gcc main.o collisioni.o stampa.o gestione.o -o progetto -lncurses
/usr/bin/ld: collisioni.o:(.bss+0x0): multiple definition of `maxx'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: collisioni.o:(.bss+0x4): multiple definition of `maxy'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: collisioni.o:(.bss+0x8): multiple definition of `flag_navicelle'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: stampa.o:(.bss+0x0): multiple definition of `maxx'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: stampa.o:(.bss+0x4): multiple definition of `maxy'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: stampa.o:(.bss+0x8): multiple definition of `flag_navicelle'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: gestione.o:(.bss+0x0): multiple definition of `maxx'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: gestione.o:(.bss+0x4): multiple definition of `maxy'; main.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status
make: *** [makefile:2: progetto] Errore 1
The source code of the project is this: https://github.com/Stenafo/game
I compile the code using the makefile.
I don't understand where the problem is, a friend of mine manages to compile it without errors.
This project uses processes, however I have the same problem with threads.

Seeing where symbols are looked up, during gcc linking?

I have just seen this post: How to tell where the symbols are defined when linking C code ; and tried to run that approach (that is, I tried to look up via -Wl,-trace-symbol=foo during the linking command) on my code, which suffers from undefined reference - but the output is not much different from what is given as an example in that post:
/usr/bin/ld: main.o: reference to foo
/usr/bin/ld: ./libfoo.a(foo.o): definition of foo
/usr/bin/ld: ./libfoo.a(foo.o): reference to fputs
/usr/bin/ld: //lib/x86_64-linux-gnu/libc.so.6: definition of fputs
The thing is, I've manually added both -L and -l specifications to my command line, which I expect should pass - but they do not.
So what I am ultimately looking for, is a way to generate a log, that tells me, say:
/usr/bin/ld: main.o: reference to foo
/usr/bin/ld: ./libyoo.a(yooA.o): symbol foo looked up, not found
/usr/bin/ld: ./libyoo.a(yooB.o): symbol foo looked up, not found
/usr/bin/ld: ./libfoo.a(foo.o): definition of foo
... which, I hope, will make me understand a bit easier, which include and linker flag directories and files end up being scanned.
Is there a way to generate a log like this?

gcc error compiling file

[user#user Book]$ gcc -o notesearch.c notesearch
notesearch: In function `__x86.get_pc_thunk.bx':
(.text+0x30): multiple definition of `__x86.get_pc_thunk.bx'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/../../../crti.o:(.gnu.linkonce.t.__x86.get_pc_thunk.bx+0x0): first defined here
notesearch: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/../../../crti.o:(.fini+0x0): first defined here
notesearch: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/../../../crt1.o:(.data+0x0): first defined here
notesearch: In function `data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/crtbegin.o:(.data+0x0): first defined here
notesearch:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/../../../crt1.o:(.rodata.cst4+0x0): first defined here
notesearch: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/../../../crt1.o:(.text+0x0): first defined here
notesearch:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/../../../crt1.o:(.rodata+0x0): first defined here
notesearch: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/../../../crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i686-pc-linux-gnu/4.9.2/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
notesearch:(.data+0x8): first defined here
/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
/usr/bin/ld: error in notesearch(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
[user#user Book]$
I am trying to compile notesearch.c from the book "hacking the art of exploitation" and I get an error. I remember being able to do it just fine. I am on a x86 3.16.7.4-1-MANJARO Linux computer.
You need to say gcc -o notesearch notesearch.c. Otherwise, you are OUTPUTTING (-o) to the .c file, which is not good. Also, you may want to consider compiling with these other flags:
-Wall: Warn All, very useful.
-Werror: Again, very useful.
-g: This is my favorite flag. It compiles with debug symbols, so that you can start you program in gdb (Gnu DeBugger) and then examine what is happening, change values of variables during execution, and generally debug it. gdb is VERY helpful.

C program no longer compiles in Ubuntu

I made a program in C and I get these errors when I compile using gcc. I did not use to get them until recently and my program has not changed.
In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
slots: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
slots: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
slots:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
slots: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
slots: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/tmp/ccIlRWIn.o: In function `main':
slots.c:(.text+0x0): multiple definition of `main'
slots:(.text+0xe4): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
slots:(.dtors+0x8): first defined here
/usr/bin/ld: error in slots(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
You forgot -o before the output filename so the linker is trying to use your old executable as input.
You could have probably defined an extern variable in a header file and include it in another file.
For example,
extern int _fini = 1; // declared in a.h
Say in a1.c or a2.c
#include "a.h" // _fini again defined in a1.c or a2.c
Seems you have more than one main and some other functions in your code. Make sure you didn't include multiple definitions of main more than once in your makefile.

Resources