So I've been trying to link a c program and an assembly program using the code
gcc -o test_asm add.o main.o
But it always gives me the error:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
i386 architecture of input file `add.o' is incompatible with
i386:x86-64 output
I have tried using -m32 to compile my C program as well but it still gives me the same problem and doesn't output an exe file. What is the way to fix this issue, any help would be appreciated.
I used nasm -f elf -o add.o add.asm for my assembly file
and gcc -m32 -c main.c main.o for my c program.
Related
I'm trying to accomplish what gcc is able to accomplish by running each command individually to better grasp what is happening when I compile my c programs. However, after following this chain of execution
for some file main.c:
cpp main.c a.i
/pathtocc1/cc1 a.i -o a.s
as a.s -o a.o
ld a.o /pathtolibc/libc.so -o a.out
I get the warning: ld: cannot find entry symbol _start; defaulting to 0000000000401020
and file not found when I attempt to run the linked ELF executable a.out
What do I need to do to bring _start into the equation? I'm running these commands through MATE Terminal on Linux MATE 1.24.0
I'm using GNU/Linux 64 bit Oracle VM to compile a simple c program main.c:
int main(){ return 0; }
gcc command is: gcc -c main.c -o output
When running: ./output, it results in error :
"Cannot execute binary file: Exec format error"
Why the gcc compilation results an executable that doesn't correspond to the architecture of the machine(SYSV and not GNU/Linux)?
Thanks in advance
The -c flag tells gcc to generate an object file. Object files are not executable, instead they are input to the linker which is used to create the executable file.
Either drop the -c flag:
gcc main.c -o output
Or build the object file and then link it:
gcc -c main.c
gcc main.o -o output
For more information about the command-line arguments for GCC, please read the documentation for your version of GCC.
I've used makefile to generate file.
gcc -c hello.c -o hello
and fixed the permission problem through:
chmod a+x ./hello
However, when I want to execute "hello" file.
./hello
the system told me that "cannot execute binary file"
Can someone help me? I am looking forward your reply badly.
The -c argument to gcc produces an object file which you later on must link in order to produce an executable. You can not execute the object file you produced.
Instead, to compile and link at the same time, suitable when you only have 1 .c file, do
gcc hello.c -o hello
Or if you want to break it down to separate compilation and linking steps, do
gcc -c hello.c -o hello.o
gcc hello.o -o hello
Check whether the GCC compiler is installed in your system correctly or not.
gcc -v
Compile your file:
gcc filename.cpp -o any-name
Running your program:
./any-name
As an alternative to compiling and linking at the same time you can use make:
make hello
Note: without the .c extension.
The terminal output should be:
cc hello.c -o hello
I am a Unix/Linux newbie who is trying to run a shell script written by a person who left no documentation and has since demised. This script contains line:
./search $opt1 $arg1 < $poly 2>&1 | tee $output
Which is trying to get the file $poly and call program ./search and divert the output to $output.
When I get to this line, I am given message: ./search: cannot execute binary file: Exec format error
search is a C program called from the script and is in the same folder as various other C programs to do with this project. Script and C programs were developed and originally executed on a Unix/Linux box which is no longer available, so I have been asked to try to resurrect this project but under Windows using gcc in NetBeans and cygwin.
The message : ./search: cannot execute binary file: Exec format error is most likely to do with the fact there is no executable file for search. When I try to build the C programs I get the following output:
C:\cygwin64\bin\make.exe -f Makefile
gcc -ansi -g -c cbuild.c
gcc -ansi -g -c complex.c
gcc -ansi -g -c mylib.c
gcc -ansi -g -c poly.c
gcc -ansi -g -c real.c
gcc -ansi -g -c zero.c
gcc -lgmp -lm -lrt -o cbuild cbuild.o complex.o mylib.o poly.o real.o zero.o
real.o: In function `rabs':
/cygdrive/c/../progs/real.c:9: undefined reference to `__imp___gmpf_abs'
/cygdrive/c/../progs/real.c:9:(.text+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp___gmpf_abs'
real.o: In function `radd':
I assume that R_X86_64_PC32 refers to the environment I am using. I am using a 64 bit version of Netbeans with gcc 5.4.0 in a 64 bit version of cygwin on Windows 10.
Can anyone advise what I must to to resolve this so that I can build the C programs?
The problem is this:
gcc -lgmp -lm -lrt -o cbuild cbuild.o complex.o mylib.o poly.o real.o zero.o
By default, the linker will link libraries and objects in the order specified on the command line, and, when linking a library, will only include symbols needed by things before it on the command line. Since -lgmp is first, there are (as yet) no outstanding symbols (except main), so nothing is included from the library. When later objects need the symbols from it, they won't see them.
Change the order to
gcc -o cbuild cbuild.o complex.o mylib.o poly.o real.o zero.o -lgmp -lm -lrt
and it should work. Alternately, use the -Wl,--as_needed linker option to get the linker to remember earlier libraries and relink them if more symbols from them are referenced by later object files (requires a recent version of the GNU linker -- I have no idea if it works with cygwin).
This kind of misordering is usually a symptom of a broken Makefile. The normal Makefile structure has a bunch of variables that are set to control the default rules that know how to compile source files and link object files. The two variables relevant for linking are LDFLAGS and LDLIBS, and the difference is that LDFLAGS comes before all the object files on the command line and LDLIBS comes after all the object files.
So in order to make things work, you need to ensure that all of the -l options and other libraries are in LDLIBS:
LDLIBS = -lgmp -lrt -lm
and NOT in LDFLAGS
For a simple "hello world" application, I used the following commands in order to create an .exe file using the GCC Compiler:
cpp hello.c > hello.i
(successful)
gcc -S hello.i
(successful)
as -o hello.o hello.s
(succesful)
When finally linking the files with the following command to obtain an .exe, I get an error:
C:\C_Experiments\test>ld -o test2.exe test2.o
test2.o:test2.c:(.text+ 0 x 9): undefined reference to __main
test2.o:test2.c:(.text+0 x 15): undefined reference to printf
ld: test2.o: bad reloc address 0x0 in section `.pdata'
You need to link with the runtime library which contains the startup function and all other standard functions.
Why not skip the preprocessor and assembler step, and go directly to object file? And also use gcc when linking which will add the needed extra libraries automatically? Or for simple single-source-file programs go directly to executable?
Either
$ gcc hello.c -c -o hello.o
$ gcc hello.o -o hello
Or
$ gcc hello.c -o hello