Makefile throwing relocation errors - c

I am trying to write a makefile to compile several files that depend on each other. The file structure is as follows:
- ext2.h
- ext_helper.c
- ext2_cp.c
- ext2_ln.c
- ext2_mkdir.c
- ext2_rm
Each of the source files depend on the ext2.h header file. The last four files are independent of each other but depend on ext2_helper.c for some helper functions.
I've tried to write a makefile for this situation as follows:
ext2_cp : ext2_helper.o
gcc -Wall -g -o ext2_cp $^
ext2_mkdir : ext2_helper.o
gcc -Wall -g -o ext2_mkdir $^
ext2_ln : ext2_helper.o
gcc -Wall -g -o ext2_ln $^
ext2_rm : ext2_helper.o
gcc -Wall -g -o ext2_rm $^
%.o : %.c ext2.h
gcc -Wall -g -c $<
clean :
rm *.o ext2_cp ext2_mkdir ext2_ln ext2_rm
However, on running this makefile, I receive several relocation erros as follows:
make
gcc -Wall -g -c ext2_helper.c
gcc -Wall -g -o ext2_cp ext2_helper.o
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [ext2_cp] Error 1
I've had a look at some other posts with the same problem but do not entirely understand the cause of the error. How would I go about fixing this?

The error message says it all: you're trying to create executables with no main(). It seems that you have forgotten something in your list of dependencies.
Try this:
ext2_cp: ext2_cp.o ext2_helper.o
gcc -Wall -g -o ext2_cp $^
ext2_mkdir: ext2_mkdir.o ext2_helper.o
gcc -Wall -g -o ext2_mkdir $^
ext2_ln: ext2_ln.o ext2_helper.o
gcc -Wall -g -o ext2_ln $^
ext2_rm: ext2_rm.o ext2_helper.o
gcc -Wall -g -o ext2_rm $^
%.o: %.c ext2.h
gcc -Wall -g -c $<
clean:
rm *.o ext2_cp ext2_mkdir ext2_ln ext2_rm

Related

Issue with compiling a c project on Ubuntu 20.4 WSL

I am trying to compile a project using WSL bash on Windows (it compiles using cmd, but I want to test it using a bash terminal). I have three modules and these are the libraries I include in the project:
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
The WSL still compiles a hello world program, but it could not compile my project as it gives the following error.
The error:
gcc line.o main.o textutil.o content.o -o textutil
/usr/bin/ld: line.o:line.c:(.text+0x69): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0xb3): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x123): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x417): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x444): undefined reference to `__imp__assert'
/usr/bin/ld: line.o:line.c:(.text+0x471): more undefined references to `__imp__assert' follow
/usr/bin/ld: textutil.o:textutil.c:(.text+0x33): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x25d): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x318): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x45c): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x503): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x65c): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x703): more undefined references to `__imp___acrt_iob_func' follow
/usr/bin/ld: textutil.o:textutil.c:(.text+0x100b): undefined reference to `___chkstk_ms'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x1079): undefined reference to `___chkstk_ms'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x1196): undefined reference to `___chkstk_ms'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x1520): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: textutil.o:textutil.c:(.text+0x15d4): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: content.o:content.c:(.text+0xac): undefined reference to `__imp__assert'
/usr/bin/ld: content.o:content.c:(.text+0xfa): undefined reference to `__imp__assert'
/usr/bin/ld: content.o:content.c:(.text+0x16a): undefined reference to `__imp__assert'
collect2: error: ld returned 1 exit status
make: *** [Makefile:17: textutil] Error 1
this is the Makefile
CC = gcc
CFLAGS = -std=c11 -W -Wall -Werror -pedantic -Wfatal -errors
CFLAGS += -D_XOPEN_SOURCE=700 # we use strdup() and strndup()
LIBS = # -lm
EXEC= textutil
HEADERS = $(wildcard *.h)
SOURCES = $(wildcard *.c)
MODULES = $(basename $(SOURCES))
OBJECTS = $(addsuffix .o , $(MODULES))
all: $(EXEC)
run: ./$(EXEC).exe
$(EXEC): $(OBJECTS)
#echo === LINKING $# ===
$(CC) $^ $(LIBS) -o $#
%.o : %.c
#echo === COMPILING $# ===
$(CC) $< $(CFLAGS) -c
depend: $(SOURCES) $(HEADERS)
#echo === COMPUTING $# ===
$(CC) -MM $(SOURCES) | tee $#
clean ::
#echo === CLEANING ===
rm -f *.o depend
-include depend
this is gcc version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Linking error for compiling Cython on aarch64

I am trying to make an executable on aarch64 using cython.
I know these flags are excessive, I tried compiling it on my x86_64 system and it worked with just gcc -I/usr/include/python3.9 -lpython3.9 sample.c but on aarch64 it gives these long linking errors.
If anyone has managed to make an executable on arm64 using cython that would also help.
root#f6753f1e6043:~# cython sample.pyx --embed -X language_level=3
root#f6753f1e6043:~# python3-config --ldflags --libs --cflags --embed
-L/usr/lib/python3.8/config-3.8-aarch64-linux-gnu -L/usr/lib -lpython3.8 -lcrypt -lpthread -ldl -lutil -lm -lm
-lpython3.8 -lcrypt -lpthread -ldl -lutil -lm -lm
-I/usr/include/python3.8 -I/usr/include/python3.8 -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-pqDzXG/python3.8-3.8.10=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall
root#f6753f1e6043:~# gcc $(python3-config --ldflags --libs --cflags --embed) sample.c
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `__Pyx_PyObject_GetAttrStr':
/root/sample.c:1489: undefined reference to `PyObject_GetAttr'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `__Pyx_copy_spec_to_module':
/root/sample.c:1258: undefined reference to `PyObject_GetAttrString'
/usr/bin/ld: /root/sample.c:1261: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /tmp/ccBjCsrj.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_Py_NoneStruct' which may bind externally can not be used when making a shared object; recompile with -fPIC
/root/sample.c:1261:(.text.unlikely+0xbc): dangerous relocation: unsupported relocation
/usr/bin/ld: /root/sample.c:1261: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /root/sample.c:1262: undefined reference to `PyDict_SetItemString'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `_Py_DECREF':
/usr/include/python3.8/object.h:478: undefined reference to `_Py_Dealloc'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `__Pyx_copy_spec_to_module':
/root/sample.c:1265: undefined reference to `PyExc_AttributeError'
/usr/bin/ld: /tmp/ccBjCsrj.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `PyExc_AttributeError' which may bind externally can not be used when making a shared object; recompile with -fPIC
/root/sample.c:1265:(.text.unlikely+0xfc): dangerous relocation: unsupported relocation
/usr/bin/ld: /root/sample.c:1265: undefined reference to `PyExc_AttributeError'
/usr/bin/ld: /root/sample.c:1265: undefined reference to `PyErr_ExceptionMatches'
/usr/bin/ld: /root/sample.c:1266: undefined reference to `PyErr_Clear'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `__Pyx_check_single_interpreter':
/root/sample.c:1237: undefined reference to `PyThreadState_Get'
/usr/bin/ld: /root/sample.c:1237: undefined reference to `PyInterpreterState_GetID'
/usr/bin/ld: /root/sample.c:1250: undefined reference to `PyExc_ImportError'
/usr/bin/ld: /tmp/ccBjCsrj.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `PyExc_ImportError' which may bind externally can not be used when making a shared object; recompile with -fPIC
/root/sample.c:1250:(.text.unlikely+0x17c): dangerous relocation: unsupported relocation
/usr/bin/ld: /root/sample.c:1250: undefined reference to `PyExc_ImportError'
/usr/bin/ld: /root/sample.c:1250: undefined reference to `PyErr_SetString'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `__pyx_pymod_create':
/root/sample.c:1278: undefined reference to `PyObject_GetAttrString'
/usr/bin/ld: /root/sample.c:1280: undefined reference to `PyModule_NewObject'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `_Py_DECREF':
/usr/include/python3.8/object.h:478: undefined reference to `_Py_Dealloc'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `__pyx_pymod_create':
/root/sample.c:1283: undefined reference to `PyModule_GetDict'
/usr/bin/ld: /tmp/ccBjCsrj.o: in function `_Py_DECREF':
/usr/include/python3.8/object.h:478: undefined reference to `_Py_Dealloc'
/usr/bin/ld: /tmp/ccBjCsrj.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__stack_chk_guard##GLIBC_2.17' which may bind externally can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /tmp/ccBjCsrj.o(.text.unlikely+0x2e0): unresolvable R_AARCH64_ADR_PREL_PG_HI21 relocation against symbol `__stack_chk_guard##GLIBC_2.17'
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

With a new custom compiled, builded version of glibc/libc.so, what happens during a “relocation has invalid symbol index” error?

From the link, I would like to compile a new glibc by myself. I know that compiling a glic is difficult, so on my first step, I would like to comiple a new glibc that match the exact same version already running on my linux system. I can skip toolchain dependency checking and start to focus on the glic iteself.
My ubuntu info is like:
abbott#abbott-VirtualBox:/software/glibc/code$ uname -a
Linux abbott-VirtualBox 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
abbott#abbott-VirtualBox:/software/glibc/code$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
The already existed glibc is: 2.19. check the version like:
abbott#abbott-VirtualBox:/software/glibc/code$ ldd --version
ldd (Ubuntu EGLIBC 2.19-0ubuntu6.9) 2.19
I prepare a very simple c code for testing:
#include <stdio.h>
int main(){
long z; printf("Long int size is %i bytes long!\n", sizeof(z));
return 0;
}
I compile this code using the already existed gcc,
gcc simple.c
I goe the "a.out", it's ok. it's runnable it's wonderful:
abbott#abbott-VirtualBox:/software/glibc/code$ ldd a.out
linux-vdso.so.1 => (0x00007ffceaf0b000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1a66f64000)
/lib64/ld-linux-x86-64.so.2 (0x000055b9118c7000)
So, I download from here, I choose the "glibc-2.19.tar.gz".
I put the source at
/software/glibc/glibc-2.19
compile at
/software/glibc/compile-2.19
compile result is OK:
abbott#abbott-VirtualBox:/software/glibc/compile-2.19$ ../glibc-2.19/configure -prefix=/usr
abbott#abbott-VirtualBox:/software/glibc/compile-2.19$ make
In the "compile-2.19" folder, there is a testrun.sh shell code,
I follow the "Compile normally, run under new glibc" section in the link, it's ok, it works.
abbott#abbott-VirtualBox:/software/glibc/compile-2.19$ ./testrun.sh ../code/a.out
I follow the "Compile against glibc build tree" section in the link,
GLIBC=/software/glibc/compile-2.19
gcc \
-Wl,-rpath=${GLIBC}:\
${GLIBC}/math:\
${GLIBC}/elf:\
${GLIBC}/dlfcn:\
${GLIBC}/nss:\
${GLIBC}/nis:\
${GLIBC}/rt:\
${GLIBC}/resolv:\
${GLIBC}/crypt:\
${GLIBC}/nptl:\
${GLIBC}/dfp \
-Wl,--dynamic-linker=${GLIBC}/elf/ld.so
-o myligcsimple simple.c
It output:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
I googled for several days, I cound not find the answers. some says that the main func is missing. but I have the main func, and the code works fine with the already existed glibc.
Question 1:
Would anybody help to find out how to fix the problem?
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
I follow the "Compile against glibc build tree" section in the link, ...
/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info)
Under no circumstances should your link use any objects from /usr/lib/debug/, and the command line arguments you show will certainly not cause that to happen. See also this answer.
Therefore, it is likely that you are not telling the whole story. Perhaps you've modified some environment variables, or the GCC driver, or you are not showing the actual command you used.

Linking errors from gcc -o0 optimization level

I have a project where I am trying to do some library and system call benchmarking. I want to make sure that the compiler does not optimize away my calls.
Here is my makefile:
CC=gcc
CFLAGS= -I ./
LIBFLAGS= -lm
RM=/bin/rm -f
all: osbench
osbench: osbench.c
$(CC) $(CFLAGS) -o $# osbench.c $(LIBFLAGS)
clean:
$(RM) osbench *~
When I set the flag to -o everything compiles and runs perfectly but I am not sure if the compiler has omitted my calls to the math.h library.
According to the gcc manual (https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) -o0 means that no optimization takes place - which is what I want. When I set the flag to -o0 I get all sorts of linking errors.
$(CC) $(CFLAGS) -o0 $# osbench.c $(LIBFLAGS)
Is there a another flag I am missing?
gcc -I ./ -o0 osbench osbench.c -lm
osbench: 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
osbench: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 12 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 13 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 14 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 15 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 16 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 17 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca tion 20 has invalid symbol index 19
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
osbench: 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
osbench:(.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
osbench: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): reloca tion 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:/build/buildd /eglibc-2.15/csu/../sysdeps/x86_64/elf/start.S:109: first defined here
osbench: 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/ccOMIYX2.o:(.rodata+0x0): multiple definition of `numOfCallsPerTrial'
osbench:(.rodata+0x8): first defined here
/tmp/ccOMIYX2.o:(.rodata+0x8): multiple definition of `numOfTrials'
osbench:(.rodata+0x10): first defined here
/tmp/ccOMIYX2.o: In function `runLibCallTrial':
osbench.c:(.text+0x0): multiple definition of `runLibCallTrial'
osbench:(.text+0xe4): first defined here
/tmp/ccOMIYX2.o: In function `calcLibCallTime':
osbench.c:(.text+0xab): multiple definition of `calcLibCallTime'
osbench:(.text+0x18f): first defined here
/tmp/ccOMIYX2.o: In function `main':
osbench.c:(.text+0x143): multiple definition of `main'
osbench:(.text+0x38c): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
osbench:(.dtors+0x8): first defined here
/usr/bin/ld: error in osbench(.eh_frame); no .eh_frame_hdr table will be created .
collect2: ld returned 1 exit status
I can post my code if necessary, however the benchmarking code part is for a homework assignment so I'm not sure I am allowed to. It compiles fine otherwise so I don't think it's a problem in the .c
Thanks.
-O not -o. That is, upper case not lower case. Anyway, you can just leave it out altogether. Default is -O0.
In case it's not clear, -o has a different meaning. It determines the compilation output name. So you should keep that in your command line. Add -O to change the optimisation level (or leave it out for default). So in fact, your original build already has optimisations disabled.

C compile error: ld returned 1 exit status [duplicate]

I have just dist-upgraded a Debian Weezy machine to run gcc-4.8 from gcc-4.7. Previously the build environment was sane and was compiling normally. Now it gives the following linker errors, with any program (even a trivial hello world):
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 20
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
collect2: error: ld returned 1 exit status
I'm sure this is something simple connected with libc6, but I can't see what? I'm quite surprised to be seeing this error since I'd have assumed dpkg would have taken care of any dependencies, so I'm not sure if it's something wrong with this specific system or an issue with the debian package (which seems unlikely, since nobody else seems to have this problem!)
Any ideas? :)
Ah! As soon as I finished typing this, as a last ditch, I tried:
apt-get install libc6-dev --reinstall
(although I was convinced I'd already done it previously), and lo and behold, the issue went away!

Resources