Makefile multiple definitions errors - c

In my makefile I have
readline: readline.h readline.c
gcc -c -o readline readline.c
car: car.c car.h readline.h
gcc -c -o car car.c
dealer.o: dealer.c car.h
gcc -c -o dealer dealer.c
dealer: car.o dealer.o readline.o
gcc car.o dealer.o readline.o
readline.h contains a function prototype that reads in strings that my professor gave us, readline.c is the function,car.c contains a struct with car information, and the various functions for operating on a linked list, dealer.c is where the main function is.
This is my first time with makefiles but I have been getting various errors that are hard to understand saying something about multiple definition of certain functions (some actually in my code and some I guess deep in my code for the system) I don't know why I'm getting these errors.
My goal is to make the main program by linking all the components together.
Errors:
gcc car.o dealer.o readline.o
dealer.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.text+0x0): first defined here
dealer.o: In function `printList':
(.text+0x4ee): multiple definition of `printList'
car.o:car.c:(.text+0x33a): first defined here
dealer.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o:(.fini+0x0): first defined here
dealer.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
dealer.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.data+0x0): first defined here
dealer.o:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtbegin.o:(.rodata+0x0): first defined here
dealer.o: In function `clearList':
(.text+0x5e2): multiple definition of `clearList'
car.o:car.c:(.text+0x42e): first defined here
dealer.o: In function `append_to_list':
(.text+0x1b4): multiple definition of `append_to_list'
car.o:car.c:(.text+0x0): first defined here
dealer.o: In function `find_car':
(.text+0x3db): multiple definition of `find_car'
car.o:car.c:(.text+0x227): first defined here
dealer.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o:(.init+0x0): first defined here
readline.o: In function `readLine':
readline.c:(.text+0x0): multiple definition of `readLine'
dealer.o:(.text+0x620): first defined here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
dealer.o:(.dtors+0x8): first defined here
/usr/bin/ld: error in dealer.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
make: *** [dealer] Error 1
I understand what makefiles are for and what they are suppose to do but I cant seem to get this working.

Your makefile makes no sense.
readline: readline.h readline.c
gcc -c -o readline readline.c
So your target is "readline", but you use -c, so really it is just "readline.o" - very non standard naming convention. Specifically designed to confuse? This will produce an object file (not an exe) called "readline"
car: car.c car.h readline.h
gcc -c -o car car.c
Same as "readline". This will produce an object file (not an exe) called "car"
dealer.o: dealer.c car.h
gcc -c -o dealer dealer.c
Not even close: the gcc is creating and object file (not an exe) called "dealer" but the target is "dealer.o". At least this time the -c is used properly - you are trying to make a ".o", it's just that the -o is wrong. Since the target is dealer.o (which is not produced) this will always run trying in vain to make dealer.o
dealer: car.o dealer.o readline.o
gcc car.o dealer.o readline.o
No -o, so you don't create "dealer", you create "a.out". Nothing even builds car.o, dealer.o or readline.o and so it will never create anything useful. At least there is no -c so this is perhaps closer to correct...

Related

MYSYS2 GCC Static Linking Error: main: linker input file not found: No such file or directory

I'm attempting to statically link a library at a location using MYSYS2 GCC 11.2.0 on Windows.
Here's my makefile: (Edit 1 from M.M and n. 1.8e9-where's-my-share m.)
#changed .o to .so, ty "n. 1.8e9-where's-my-share m."
libmxn_external_test.so: mxn_external_test.h mxn_external_test.c
gcc -o libmxn_external_test.so -fpic -shared mxn_external_test.h mxn_external_test.c
#changed .o to .so
libmxn_external_test.a: libmxn_external_test.so
ar rcs C:\libs\c\libmxn_external_test.a libmxn_external_test.so -v
#changed main to main.c, ty "M.M"
main.o: main.c
gcc -c main.c
main: main.o
gcc main.o -LC:\libs\c -lmxn_external_test -o main
#this kinda works but after graduating with a CS degree, and a few hours of make videos, haven't figured out much more yet
quick: main.o libmxn_external_test.so
gcc -o main main.o libmxn_external_test.o
clean:
rm libmxn_external_test.h.gch libmxn_external_test.so main.o
And I receive this error when running "make main":
PS C:\Users\U53R\Desktop\workflow\coding\c\c\projects\PRACTICE\ExternalLibrary> gcc main.o -LC:\libs\c -lmxn_external_test -o main C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find main.o: No such file or directory
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o): in function `atexit':
C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtdll.c:205: multiple definition of `atexit'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:418: first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):cygming-crtbeg:(.text+0x380): multiple definition of `__gcc_register_frame'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/crtbegin.o:cygming-crtbeg:(.text+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):cygming-crtbeg:(.text+0x390): multiple definition of `__gcc_deregister_frame'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/crtbegin.o:cygming-crtbeg:(.text+0x10): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x5f0): multiple definition of `.refptr.__native_startup_state'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__native_startup_state[.refptr.__native_startup_state]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):crtdll.c:(.data+0x0): multiple definition of `__mingw_module_is_dll'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:422: first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x5c0): multiple definition of `.refptr.__mingw_app_type'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__mingw_app_type[.refptr.__mingw_app_type]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x630): multiple definition of `.refptr.__xi_a'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__xi_a[.refptr.__xi_a]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x5e0): multiple definition of `.refptr.__native_startup_lock'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__native_startup_lock[.refptr.__native_startup_lock]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x5a0): multiple definition of `.refptr.__dyn_tls_init_callback'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__dyn_tls_init_callback[.refptr.__dyn_tls_init_callback]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x5b0): multiple definition of `.refptr.__image_base__'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__image_base__[.refptr.__image_base__]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x610): multiple definition of `.refptr.__xc_a'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__xc_a[.refptr.__xc_a]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x640): multiple definition of `.refptr.__xi_z'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__xi_z[.refptr.__xi_z]+0x0): first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\libs\c/libmxn_external_test.a(libmxn_external_test.o):misc.c:(.rdata+0x620): multiple definition of `.refptr.__xc_z'; C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.rdata$.refptr.__xc_z[.refptr.__xc_z]+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Two things I noticed from this is:
The "wrong" kind of slash for Windows C:\libs\c/libmxn_external_test.a
And absurdly long possibly looping filepaths, which may be inherent to using mys64 and mingw.
I DO have my library in C:\libs\c\libmxn_external_test.a
What can I do to get the -L and -l options to work properly on for MYSYS2 gcc?

Linking 2 object files in c to create an executable

I am having an issue with a makefile for something I am making. My makefile looks like this
bag: main.o bow.o
gcc bow.o main.o -o bag
main.o: main.c bow.h
gcc -Wall -ansi -pedantic main.c -o main.o
bow.o: bow.c bow.h
gcc -Wall -ansi -pedantic -c bow.c -o -bow.o
I also have a header file called "bow.h" that is used in both bow.o and main.o. bow.h consists of 8 function definitions and 2 structs, bow.c contains the 8 functions and NO MAIN file. main.c is suppose to be a minimal main file so it only consists of
#include "bow.h"
When I run my makefile in the Terminal with
make
I get this message
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'bag' failed
make: *** [bag] Error 1
What exactly does this mean, how is it caused and how can I fix it?
Even a minimal program (executable) needs a point to start. For a C program, this is the main() function. Thus, the linker seeks for that function (more precisely, it links the start-up object where main is an unresolved symbol), does not find it, and issues an error.
Thus, you have to provide a main(). Alternatively, you may not generate an executable but a library.

Should I worry about gcc -pedantic option now?

I have just started to learn C as a hobby. I am doing it with the "C programming: A Modern Approach" book. There is a first program there which is called pun.c. Here is the code:
#include <stdio.h>
int main(void)
{
int int_figure;
float float_figure;
int_figure = 12;
float_figure = 12.0;
printf("To C or not to C, this is a question\n");
printf("%d\n", int_figure);
printf("%.2f\n", float_figure);
return 0;
}
Actually it does not matter because the thing I want to ask about is the same with any .c file compilation with gcc.
So in the book there are some options for gcc which allow finding errors during compilation.One of them is -Wall, another one is -pedantic. So when I compile the file with this option, the output in terminal is the following:
nickdudaev|c $ gcc -o -Wall pun pun.c
pun: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crti.o:(.fini+0x0): first defined here
pun: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crt1.o:(.data+0x0): first defined here
pun: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/crtbegin.o:(.data+0x0): first defined here
pun:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
pun: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crt1.o:(.text+0x0): first defined here
pun: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../lib/crti.o:(.init+0x0): first defined here
/tmp/cc2TRR93.o: In function `main':
pun.c:(.text+0x0): multiple definition of `main'
pun:(.text+0xf6): first defined here
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
pun:(.data+0x10): first defined here
/usr/bin/ld: error in pun(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
The program although runs correctly.
nickdudaev|c $ ./pun
To C or not to C, this is a question
12
12.00
So to the questions.
Should I on this stage (of learning) worry about this output and at all use those options?
Do I have probably some systematic error? Because output is the same for any file compilation. Maybe something is not properly installed?
I tried to search Google, but the only thing I found is descriptions of what gcc options do. But nothing about possible output and what to do with it.
This:
gcc -o -Wall pun pun.c
doesn't look right. You're saying -Wall where you should say the name of the output, i.e. the argument to -o.
Try:
gcc -Wall -o pun pun.c
Basically you're feeding pun, the old binary, to gcc as a source file.
The -o option in gcc is used to specify a custom output file name, otherwise,
If -o is not specified, the default is to put an executable file in a.out.
The recommended format is -o file. The next expected argument after -o is a filename, not another switch. See the online manual for more details.
You should re-write your compilation statement as
gcc -o pun pun.c -Wall

Error in creating .so file from .o files generated using multiple .c files

I have created the .o file as follows,
*gcc -I/home/vineeshvs/Dropbox/wel/workspace/Atmel -I /home/vineeshvs/Downloads/libusb-1.0.9 -I /home/vineeshvs/Downloads/libusb-1.0.9/libusb/ usb_comm.c hex2bin.c hex_read.c crc32.c -o vs.o -lusb-1.0*
Then I used the following command to get .so file
*gcc vs.o -shared -o libhello.so*
Then I am getting error as follows
*vs.o: In function `__i686.get_pc_thunk.bx':
(.text+0xaa6): multiple definition of `__i686.get_pc_thunk.bx'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbeginS.o:crtstuff.c:
(.text.__i686.get_pc_thunk.bx[__i686.get_pc_thunk.bx]+0x0): first defined here
vs.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here
vs.o: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbeginS.o:(.data.rel+0x0): first defined here
vs.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i686-linux-gnu/4.6/crtendS.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
vs.o:(.dtors+0x4): first defined here
/usr/bin/ld.bfd.real: error in vs.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
*
What could be the problem? (Thanks for stopping by :))
The problem is that you're linking the object files, not just compiling them.
Make sure that you only compile the files, don't link them! You do that using the -c option. Do not use the -l option, you don't want to link anything at this stage. So:
gcc -c -o usb_comm.o usb_comm.c
gcc -c -o hex2bin.o hex2bin.c
gcc -c -o hex_read.o hex_read.c
gcc -c -o crc32.o crc32.c
(I omitted the -I flags to save space here.)
Then finally link all those object files into a shared library, and link against usb-1.0:
gcc -shared -o libhello.so usb_comm.o hex2bin.o hex_read.o crc32.o -lusb-1.0
You should use a Makefile though for this. Or, even better, use a proper build system, like CMake, which is very easy to use. It's provided by all common Linux distros, so simply install it with the package manager (it it's not installed already), and read a quick tutorial on it.

When compiling,I write " gcc -g -Wall dene2 dene2.c", then gcc emits some trace

When I compile my code,I write gcc -g -Wall dene2 dene2.c in the console. Then gcc emits some text on the screen. I don't understand what this output means (I couldn't think of a meaningful title for that reason, sorry).
I have tried Google searching but haven't had any luck.
I'm not asking for a detailed examination of all of the output below. Just show me "how to catch fish".
dene2: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:65: multiple
definition of `_start'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:/build/buildd/eglibc-2.10.1
/csu/../sysdeps/i386/elf/start.S:65: first defined here
dene2:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata+0x0): first
defined here
dene2: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.fini+0x0): first defined
here
dene2:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata.cst4+0x0): first
defined here
dene2: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.data+0x0): first defined
here
dene2: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i486-linux-gnu/4.4.1/crtbegin.o:(.data+0x0): first defined here
dene2: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.init+0x0): first defined
here
/tmp/ccMlGkkV.o: In function `main':
/home/fatih/Desktop/dene2.c:5: multiple definition of `main'
dene2:(.text+0xb4): first defined here
/usr/lib/gcc/i486-linux-gnu/4.4.1/crtend.o:(.dtors+0x0): multiple definition of
`__DTOR_END__'
dene2:(.dtors+0x4): first defined here
collect2: ld returned 1 exit status
I think you probably want a -o in that command line:
gcc -g -Wall -o dene2 dene2.c
What you have there without the -o is trying to link dene2 with the result of compiling dene2.c. dene2 is probably left over in your directory from previous build attempt. That's why you're seeing all the duplicate symbol errors.
You are missing the -o in your compile line.
gcc -g -Wall -o dene2 dene2.c
The linker is trying to link your executable together with the source code you're current compiling, resulting in multiple definitions.

Resources