LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 01-07-2002, 11:27 PM   #1
wsimmons
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 here...do 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, 04:50 AM   #2
Mik
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:
http://linuxassembly.org

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

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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
 
  


Reply



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 12:10 AM
need binary gcc phoenix_wolf Linux - Software 1 12-02-2004 03:16 AM
Making VSAM file from a flat file in AIX Raghothaman AIX 2 08-25-2004 04:55 PM
gcc and creating a debug file xviddivxoggmp3 Programming 5 03-16-2004 03:38 PM
Flat File QC Scripting mychl Programming 5 02-20-2004 11:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 AM.

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