By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Including compiled C source file in array

P: n/a
Hi,

Is it possible to do something like this:

unsigned char arr[] = {
#include "cFile.c"
}

I need that C source file cFile.c to compile and its binary output to
include in array.

How we can achieve it? Any other trick to do this?

Thanks,
Sachin

Jun 27 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
sachin wrote:
Hi,

Is it possible to do something like this:

unsigned char arr[] = {
#include "cFile.c"
}

I need that C source file cFile.c to compile and its binary output to
include in array.

How we can achieve it? Any other trick to do this?
You can compile cFile.c and copy it's bytes, (as hexadecimal, decimal or
octal text. All binary editors can show this for you), to your array
declaration.

Alternatively there might be compiler specific extensions for this,
though I doubt it. You might also consider the possibility of loading
the binary file at runtime with fread.

Jun 27 '08 #2

P: n/a
In article <b6**********************************@u12g2000prd. googlegroups.com>,
sachin <sb******@rediffmail.comwrote:
>I need that C source file cFile.c to compile and its binary output to
include in array.
What do you expect to be able to do with it?

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #3

P: n/a
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <b6**********************************@u12g2000prd. googlegroups.com>,
sachin <sb******@rediffmail.comwrote:
>>I need that C source file cFile.c to compile and its binary output to
include in array.

What do you expect to be able to do with it?

-- Richard
I would expect he has something which produces code based on dynamic
variables and wants to have variable records e.g accept user tweaks,
regenerate record structures, recompile record handling SW, run record
handling SW.
Jun 27 '08 #4

P: n/a
In article <g2**********@registered.motzarella.org>,
Richard <rg****@gmail.comwrote:
>>>I need that C source file cFile.c to compile and its binary output to
include in array.
>What do you expect to be able to do with it?
>I would expect he has something which produces code based on dynamic
variables and wants to have variable records e.g accept user tweaks,
regenerate record structures, recompile record handling SW, run record
handling SW.
Possibly, but unless he tells us we can't give him much help. For
most uses he'd be wisest to to use system-dependent functions that
(for example) access the symbol table, and reading the compiled file
into an array may well not be the right way to do that.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #5

P: n/a
On Jun 5, 5:25 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <g28kua$ba...@registered.motzarella.org>,

Richard <rgr...@gmail.comwrote:
>>I need that C source file cFile.c to compile and its binary output to
include in array.
What do you expect to be able to do with it?
I would expect he has something which produces code based on dynamic
variables and wants to have variable records e.g accept user tweaks,
regenerate record structures, recompile record handling SW, run record
handling SW.

Possibly, but unless he tells us we can't give him much help. For
most uses he'd be wisest to to use system-dependent functions that
(for example) access the symbol table, and reading the compiled file
into an array may well not be the right way to do that.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Hi,

I have one C source file which after compiling I need to copy at
specific location.
Until system comes up I don't have access to that memory location. So
I want to keep
that code in executable form with the OS image. When system comes up
will copy that
executable code to designated space.

Thanks,
Sachin
Jun 27 '08 #6

P: n/a
In article <g2***********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
>In article <b6**********************************@u12g2000prd. googlegroups.com>,
sachin <sb******@rediffmail.comwrote:
>>I need that C source file cFile.c to compile and its binary output to
include in array.

What do you expect to be able to do with it?
Why, to smash the stack, of course.

It's perfectly obvious that all of these queries about needing to
include (seemingly) random sequences of bytes in code are about
"cracking/hacking/freeking/whatever-you-want-to-call-it" - let's not get
into that argument...

Not necessarily for evil purposes, mind you. It's worth the exercise
just to learn what all the fuss is about.

Jun 27 '08 #7

P: n/a
sachin <sb******@rediffmail.comwrote:
On Jun 5, 5:25 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <g28kua$ba...@registered.motzarella.org>,

Richard <rgr...@gmail.comwrote:
>>>I need that C source file cFile.c to compile and its binary output to
>>>include in array.
>What do you expect to be able to do with it?
>I would expect he has something which produces code based on dynamic
>variables and wants to have variable records e.g accept user tweaks,
>regenerate record structures, recompile record handling SW, run record
>handling SW.
Possibly, but unless he tells us we can't give him much help. For
most uses he'd be wisest to to use system-dependent functions that
(for example) access the symbol table, and reading the compiled file
into an array may well not be the right way to do that.

I have one C source file which after compiling I need to copy at
specific location.
Until system comes up I don't have access to that memory location. So
I want to keep
that code in executable form with the OS image. When system comes up
will copy that
executable code to designated space.
You can't simply include a binary file (the C source will be
of no use anyway). Just write a little utility that opens the
file you want to insert as data at that place, reads the data
from the file in as unsigned chars and writes them out in a
form like this:

unsigned char array[ ] = {
0x01, 0x3A, 0xA6, 0x12, 0x17, 0xC3, 0x7D, 0x5F,
.....
};

Save that to a file (e.g. data.h). Then include the data.h
file at exactly the place where you want the array to be
defined. Lets hope that the binary file isn't too long to
be stored in an array.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #8

P: n/a
On 5 Jun 2008 at 12:44, sachin wrote:
I have one C source file which after compiling I need to copy at
specific location.
If you're writing a virus, the man's way is to write the shellcode by
hand in assembly, not use a compiled C file.

Jun 27 '08 #9

P: n/a
Antoninus Twink wrote, On 05/06/08 17:18:
On 5 Jun 2008 at 12:44, sachin wrote:
>I have one C source file which after compiling I need to copy at
specific location.

If you're writing a virus, the man's way is to write the shellcode by
hand in assembly, not use a compiled C file.
On one DSP I used to use there was a perfectly good and "standard"
reason for wanting to achieve what the OP wants to achieve. However, the
implementation provided special tricks in the linker to allow you to
achieve it which did not involved doing what the OP asked about.

The OP needs to ask about the real problem rather than what he thinks
the solution is in a group dedicated to his implementation.
--
Flash Gordon
Jun 27 '08 #10

P: n/a
Dan

"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:c0************@news.flash-gordon.me.uk...
Antoninus Twink wrote, On 05/06/08 17:18:
>On 5 Jun 2008 at 12:44, sachin wrote:
>>I have one C source file which after compiling I need to copy at
specific location.

If you're writing a virus, the man's way is to write the shellcode by
hand in assembly, not use a compiled C file.

On one DSP I used to use there was a perfectly good and "standard" reason
for wanting to achieve what the OP wants to achieve. However, the
implementation provided special tricks in the linker to allow you to
achieve it which did not involved doing what the OP asked about.
Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D
Jun 27 '08 #11

P: n/a
On Jun 6, 2:49 pm, "Dan" <vo...@sometwher.worldwrote:
"Flash Gordon" <s...@flash-gordon.me.ukwrote in message

news:c0************@news.flash-gordon.me.uk...
Antoninus Twink wrote, On 05/06/08 17:18:
On 5 Jun 2008 at 12:44, sachin wrote:
I have one C source file which after compiling I need to copy at
specific location.
If you're writing a virus, the man's way is to write the shellcode by
hand in assembly, not use a compiled C file.
On one DSP I used to use there was a perfectly good and "standard" reason
for wanting to achieve what the OP wants to achieve. However, the
implementation provided special tricks in the linker to allow you to
achieve it which did not involved doing what the OP asked about.

Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D
How __PRAGMA_FILE works. How to use it in our source code?
Can you please explain this. I don't know much about pragma.
Jun 27 '08 #12

P: n/a
sachin <sb******@rediffmail.comwrote:
On Jun 6, 2:49 pm, "Dan" <vo...@sometwher.worldwrote:
"Flash Gordon" <s...@flash-gordon.me.ukwrote in message
Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D
How __PRAGMA_FILE works. How to use it in our source code?
Can you please explain this. I don't know much about pragma.
It's something specific to the compiler Flash's using on his
system, it's nothing every compiler has to support. Obviously,
it converts the contents of a file at compile time into an
expression that can be used to initialize a char array. But
unless you know you have the exact same compiler as Flash you
will have to study your compiler documentation. If you're very
lucky you may have something similar, perhaps going by a dif-
ferent name.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #13

P: n/a
Jens Thoms Toerring wrote, On 09/06/08 16:00:
sachin <sb******@rediffmail.comwrote:
>On Jun 6, 2:49 pm, "Dan" <vo...@sometwher.worldwrote:
>>"Flash Gordon" <s...@flash-gordon.me.ukwrote in message
Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D
>How __PRAGMA_FILE works. How to use it in our source code?
Can you please explain this. I don't know much about pragma.

It's something specific to the compiler Flash's using on his
system, it's nothing every compiler has to support. Obviously,
No, it is specific to the compiler Dan used. The implementation I used
worked in an entirely different way to achieve the same effect.
it converts the contents of a file at compile time into an
expression that can be used to initialize a char array. But
unless you know you have the exact same compiler as Flash you
will have to study your compiler documentation. If you're very
lucky you may have something similar, perhaps going by a dif-
ferent name.
Or it may be completely different but allowing the same effect to be
achieved. Anyone needing to do this needs to ask on a group dedicated to
their specific implementation.
--
Flash Gordon
Jun 27 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.