My C application relies on some files to copy over. Will the files be contained in the executable and not as stand-alone files? Would it have to be linked statically? I am using Eclipse CDT if it matters.
-
The question is not clear to me. Would [zipping][1] the exe and the file together and distributing the zip file be an option? [1]: http://en.wikipedia.org/wiki/ZIP_%28file_format%29 – Arun Oct 11 '10 at 01:29
-
No. I mean the text files are accessible only within the executable. And they are contained in the executable, not in the filesystem. – Mohit Deshpande Oct 11 '10 at 01:33
-
I see. Then, the option that appears first in mind is to create a `[static] const char * file_contents = ...` to hold that text. – Arun Oct 11 '10 at 01:38
3 Answers
There are several ways you can link file data into an executable. A platform like Windows allows you to link data into an executable as a "resource" and provides APIs to access those resources (this is how icons and other objects are bound into a Windows executable). This is probably the best way to do it if your platform supports it - support for it is built right into the IDEs.
At a lower level, you might be able to use the linker to directly link a file into an executable as an addressable object:
And finally, if you're dealing with a more primitive system like some embedded platforms, you can run your file through something that converts it into source for C array of bytes:

- 1
- 1

- 333,147
- 50
- 533
- 760
Unless you do something special, no, the files will not be included in your executable. Is there a reason you can't distribute the text files with your application?
If you want to bake the files into your executable, you can bake them in constant strings:
const char myTextFileData[] = "the text of the file goes here";
Of course, you'll have to preprocess your text files into C source files (remembering to properly escape quotes, backslashes, newlines, and other control characters). Alternatively you can use a tool such as objcopy
to convert the file data directly into an object file and then link that object file into your executable.

- 390,455
- 97
- 512
- 589
No. The executable contains only the output from the compiler and linker. Any ancillary files your program requires must be packaged separately.

- 4,277
- 3
- 27
- 35