Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-08-2002, 12:27 AM   #1
LQ Newbie
Registered: Sep 2001
Posts: 10

Rep: Reputation: 0
Smile creating a flat binary file with gcc

Consider the simplest program which I would like to compile as a simple binary file without headers:

#include <stdio.h>

int main()
printf("Test file\n");
return 0;

The reason that I would like to compile it as a binary is so that I can experiment with LILO and mkbootdisk (or other equivalents), place the file on a floppy, and watch the prgress of the "boot" process.

When I compile and link using:

gcc -o test.bin input.c

everything works fine and just running the "program" ( ./test.bin ) produces the desired results. However when I try the following:

gcc -c input.c

ld input.o -o test.bin -oformat binary

I get an error that states that printf() is an undefined function. This is confusing. gcc -c should take it through preprocessing and compiling up to the point of linking. Why wasn't the include statement preprocessed? Or... what is the answer...where is my thinking incorrect?
How do I correct this? Also, while you are you have any suggestions for getting the file to the correct position on a floppy (and to a correct position in memory when loaded) and direct LILO to also load the correct loader commands to the MBR?
Old 01-08-2002, 05:50 AM   #2
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
That's because printf is part of libc which is usually a shared library since lots of programs use it. And I don't think you want to include a static version of libc in your binary file. Which on my system is almost 1 MB.

Anyways you might want to read some stuff on this site:

I think that's kind of the direction you want to go. Look at the asmutils they seem kinda nice.
Old 01-08-2002, 12:33 PM   #3
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590
Which on my system is almost 1 MB.
gcc -O2 -static -o crap crap.c (aprox 1.6Mb)
strip crap (aprox 360Kb)
upx -9 crap (leaves 153Kb)

just my 2 cents,
upx btw is a freeware,no-performance-blocking exe compressor


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating binary RPMS statically! Sunilsbjoshi Linux - Software 2 10-12-2005 01:10 AM
need binary gcc phoenix_wolf Linux - Software 1 12-02-2004 04:16 AM
Making VSAM file from a flat file in AIX Raghothaman AIX 2 08-25-2004 05:55 PM
gcc and creating a debug file xviddivxoggmp3 Programming 5 03-16-2004 04:38 PM
Flat File QC Scripting mychl Programming 5 02-20-2004 12:53 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration