How to make c program as executable in windows 7 - c

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

Related

cannot execute binary files on MAC terminal

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

How to link a simple program in Windows with Mingw GCC

I have a simple "Hello World!" c program, named hello.c on my desktop:
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
I run the following commands.
I pre-process it with : cpp hello.c > hello.i
I compile it with : gcc -S hello.i
I assemble it with : as -o hello.o hello.s
All good so far. But, i'm unable to link it. I've tried, among other commands, these:
ld -o hello.exe hello.o
ld -o hello.exe hello.o -lgcc
ld -o hello.exe hello.o -nostdlib -lgcc
Nothing works. The link errors i get in every single case are :
hello.o:hello.c:(.text+0x9): undefined reference to `__main'
hello.o:hello.c:(.text+0x15): undefined reference to `puts'
How can i link this assembled program hello.o in order to finally produce the executable program hello.exe? What am i missing? [Using Windows 8.1, Mingw version 0.6.2.] Thanks in advance.
Even if your answers to clarification questions are not particularly useful:
Try something like
ld hello.o -lmsvcrt -entry=_main -subsystem=console -o hello.exe
If you want to see the linker command line the standard gcc uses, invoke gcc like so:
gcc test.c -o test -Wl,-v
The last lines output is what you should be using...
If you want to compile something rather than experimenting with tools, don't link it using the linker directly. Use gcc, which will call the linker for you with the right options:
Compile step:
gcc -c hello.c
Link step:
gcc -o hello.exe hello.o
Or all in one:
gcc -o hello.exe hello.c
You can also use a simple Makefile, then run make:
all: hello.exe

How to compile an executable using clang and safe-stack flag

I am trying to compile a simple hello-world executable using clang-3.7 (also tried 3.8 (dev)) with -fsanitize=safe-stack flag. As explained here (http://clang.llvm.org/docs/SafeStack.html), I need to pass this flag to compiler and linker.
"To enable SafeStack, just pass -fsanitize=safe-stack flag to both compile and link command lines."
I tried the following command to compile an executable:
clang-3.7 -fsanitize=safe-stack -o a.out -Wl,-fsanitize=safe-stack test.c
But the linker tells me, that i need to compile it as a shared library (-shared), if I pass the -f flag to the linker.
/usr/bin/ld: -f may not be used without -shared
How can I compile an executable using the -fsanitize=safe-stack flag?
By "pass it to both the compile and link command lines" the documentation means to pass it both when you're compiling, and when you're linking. It does not mean to use -Wl, which passes it straight through to the linker - -f means something entirely unrelated to the linker.
In this case,
clang-3.7 -fsanitize=safe-stack -o a.out test.c
is sufficient. If you were using separate command executions to compile and link, you would need to pass it on both:
clang-3.7 -fsanitize=safe-stack -c -o test.o test.c
clang-3.7 -fsanitize=safe-stack -o a.out test.o

ELF File generation commands and options

I was reading about ELF files on the net and am stuck in understanding a standard command to generate an ELF file.
Till now I have been running my code using > gcc test.c -o test.o .Thats it!!
One article says:
gcc -c test.c // will generate ELF file test.o
Now -o option is to tell the compiler to create an executable ( which is not ELF)
Another article says:
gcc -c test.c -o test.o // will generate ELF test.o -> here's where I am confused.
-o should always generate Executable.
The option -c tells GCC to generate an object file. This object file is only the compiled code from the source file test.c, not a complete program. To generate a complete program you need to link the object file. Or not use the -c option.
The -o option tells GCC what to name the output file, no matter what kind of output file it is.
So, to generate an executable file from a single source file, the simplest command is
$ gcc test.c
The above command will create an executable named a.out in the current directory. To name the output file something else you use the -o option:
$ gcc test.c -o myprogram
The above commands names the executable program myprogram.
To use the intermediate step with object files you use the -c option, and then use a separate step to link the program, like
$ gcc -c test.c
$ gcc test.o -o myprogram
The above two commands is the same as the single command gcc test.c -o myprogram.

More GCC link time issues: undefined reference to main

I'm writing software for a Cortex-A8 processor and I have to write some ARM assembly code to access specific registers. I'm making use of the GNU compilers and related toolchains; these tools are installed on the processor board (Freescale i.MX515) with Ubuntu. I make a connection to it from my host PC (Windows) using WinSCP and the PuTTY terminal.
As usual I started with a simple C project having main.c and functions.s. I compile the main.c using GCC, assemble the functions.s using as and link the generated object files using once again GCC, but I get strange errors during this process.
An important finding -
Meanwhile, I found out that my assembly code may have some issues because when I individually assemble it using the command as -o functions.o functions.s and try running the generated functions.o using ./functions.o command, the Bash shell is failing to recognize this file as an executable (on pressing tab functions.o is not getting selected/PuTTY is not highlighting the file).
Can anyone suggest what's happening here? Are there any specific options I have to send, to GCC during the linking process? The errors I see are strange and beyond my understanding, I don't understand to what the GCC is referring.
I'm pasting here the contents of main.c, functions.s, the Makefile and the list of errors.
Help, please!!!
**Latest errors included after the makefile was edited as suggested here **
ubuntu#ubuntu-desktop:~/Documents/Project/Others/helloworld$ make
gcc -c -mcpu=cortex-a8 main.c
as -mcpu=cortex-a8 -o functions.o functions.s
gcc -o hello main.o functions.o
functions.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [hello] Error 1
main.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
functions.s
* Main program */
.equ STACK_TOP, 0x20000800
.text
.global _start
.syntax unified
_start:
.word STACK_TOP, start
.type start, function
start:
movs r0, #10
movs r1, #0
.end
Makefile
all: hello
hello: main.o functions.o
gcc hello -o main.o functions.o
-- hello was included here after suggested here by guys at Stack Overflow, but the problem still persists; I still get the same errors.
main.o: main.c
gcc -c -mcpu=cortex-a8 main.c
functions.o: functions.s
as -mcpu=cortex-a8 -o functions.o functions.s
Errors
ubuntu#ubuntu-desktop:~/Documents/Project/Others/helloworld$ make
gcc -c -mcpu=cortex-a8 main.c
as -mcpu=cortex-a8 -o functions.o functions.s
gcc -o main.o functions.o
functions.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o: In function `_start':
init.c:(.text+0x30): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [hello] Error 1
In the makefile:
hello: main.o functions.o
gcc -o main.o functions.o
should be:
hello: main.o functions.o
gcc -o hello main.o functions.o
As it stands, you are linking functions.o, but not main.o, and producing an output executable called main.o, which is overwriting your existing main.o.
Shouldn't
hello: main.o functions.o
gcc -o main.o functions.o
be
hello: main.o functions.o
gcc -o hello main.o functions.o
As Bigbohne suggests, gcc is trying to link in the standard runtime library. Try adding the -nostdlib option to your gcc call:
gcc -nostdlib -o hello main.o functions.o
I think that has something to do with the Runtime library the gcc is linking at the end.
And in this library there already is a "_start".
I think you have to compile without "std library". but than you wont have printf,getchar and all the other useful stuff.
Normally execution of a program compiled with gcc starts in the function called _start (in a startup file provided by gcc), which calls main.
If you are supplying your own _start, you should pass -nostdlib to gcc when linking. This disables all default startup files and all default libraries. You should therefore also explicitly provide any libraries you need such as libc and libgcc.
Your _start however does not call main, so it is unclear why you bother to provide main and the .c file at all.
gcc -o main.o functions.o is a mistake, it should be gcc -o main main.o functions.o, but this will not fix the problem described above.

Resources