Thread: preprocessor file inclusion?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    preprocessor file inclusion?

    Hi,

    It's been a while since I have coded anything in C, can't remember anything. Anyway, I was hoping to be able to include a file at compile time. This is for an embedded application and I don't have a file system. I was hoping I could do something simple like:

    Code:
    #define start_file
    #include "filename"
    #define end_file
    But my compiler keeps trying to interpret the file as a header file. I would like to use the start_file as a pointer to the beginning of the file content. I am really hoping this is a simple question with a simple answer. Thanks in advance for any help.

    Cheers,
    Dyl

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The C preprocessor is basically a big "copy and paste" machine, so what happens in this case is the same as if you took your source file and opened "filename" and copied the whole content, then pasted it into the source file before compiling the code. If "filename" doesn't contain C source code, it basically won't work to do this.

    What are you actually trying to achieve, what is the content of the file you want to "include"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    Ya, that's what I figured. I want to have a pointer to the content of that file, which is binary.

    So...

    char * = <content of file>

    At the preprocessor level, since I can't do that while running. I just want a static array of data.

    Dyl

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So the usual way to do that is to write a piece of code that turns your binary data into a char-array style initializer. So if your binary data is (in hex) 00112233, it would generate
    Code:
    0x00, 0x11, 0x22, 0x33
    and then you'd do something like:
    Code:
    char *binary_data = 
    {
    #include "myfile.data.h"
    }
    It's really just a 5-10 line program to convert the data.
    To make it nicely readable [if it's a large amount of data], you may want to add a line-break every 10-16 byte or so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    Right on Mat! Thanks.

    Cheers,
    Dyl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Source file inclusion
    By sh3rpa in forum C++ Programming
    Replies: 7
    Last Post: 10-02-2007, 11:10 AM
  2. MSVC #pragma once and inclusion guards
    By cboard_member in forum Tech Board
    Replies: 4
    Last Post: 12-23-2006, 08:17 AM
  3. inclusion guards
    By Iamregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2003, 07:46 AM
  4. class and header inclusion problem.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 10:12 PM
  5. Inclusion of header class
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 12-08-2001, 03:33 PM