0

I have a config.h generated by CMake, defining the current build's setup, like this:

#ifndef CONFIG_H__
#define CONFIG_H__

#define CMAKE_BUILD_TYPE "Debug"
#define MY_GITHASH "@123456@"
#define MY_COMPILATION_DATE "2018-10-16"
#define MY_VERSION_MAJOR 1
#define MY_VERSION_MINOR 0

#endif /* CONFIG_H__ */

How can I put this into the binary so it can be printed on demand? I know that I can combine all defines into a new define and print this. I am looking for a more straight-forward solution that just prints the entire header file - ideally without having the location of the config.h header file.

mattmilten
  • 6,242
  • 3
  • 35
  • 65
  • 3
    Unrelated to your problem, but please don't use symbols or names with two leading underscores (like e.g. `__CONFIG_H__`), those are reserved in all scopes. See [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for details. – Some programmer dude Oct 16 '18 at 07:59
  • @Someprogrammerdude Is the source of the identfier in question under user control? Or is it something solely controlled by the cmake generator? (I am with you on avoiding them, either way.) – Yunnosch Oct 16 '18 at 08:32
  • 1
    @Yunnosch Templates for configuration header files using CMake are user-created. CMake will not add header include guards. – Some programmer dude Oct 16 '18 at 08:36
  • Thank you for this hint! – mattmilten Oct 16 '18 at 09:45
  • Am I right that the question is about embedding of the file's content into the executable? If so, then there are several questions on this topic on Stack Overflow. E.g. https://stackoverflow.com/questions/4158900/embedding-resources-in-executable-using-gcc, https://stackoverflow.com/questions/7288279/how-to-embed-a-file-into-an-executable – Tsyvarev Oct 16 '18 at 13:18

1 Answers1

0
  1. Not ideally (config.h must be in known for gasplace)
  2. GCC only

test.c:

extern const char config_h_begin;
extern const char config_h_end;

asm ("\t.section .rodata\n"
    "config_h_begin: \n"
    "\t .incbin \"config.h\"\n"
    "config_h_end:\n"
);

const char *begin = &config_h_begin;
const char *end = &config_h_end;
$objdump -s test.o
test.o:     file format elf64-x86-64

Contents of section .rodata:

0000 2369666e 64656620 5f5f434f 4e464947  #ifndef __CONFIG
0010 5f485f5f 0a236465 66696e65 205f5f43  _H__.#define __C
0020 4f4e4649 475f485f 5f0a0a23 636d616b  ONFIG_H__..#cmak
0030 65646566 696e6520 434d414b 455f4255  edefine CMAKE_BU
0040 494c445f 54595045 20224465 62756722  ILD_TYPE "Debug"
0050 0a236465 66696e65 204d595f 47495448  .#define MY_GITH
0060 41534820 22403132 33343536 40220a23  ASH "@123456@".#
0070 64656669 6e65204d 595f434f 4d50494c  define MY_COMPIL
0080 4154494f 4e5f4441 54452022 32303138  ATION_DATE "2018
0090 2d31302d 3136220a 23646566 696e6520  -10-16".#define 
00a0 4d595f56 45525349 4f4e5f4d 414a4f52  MY_VERSION_MAJOR
00b0 20310a23 64656669 6e65204d 595f5645   1.#define MY_VE
00c0 5253494f 4e5f4d49 4e4f5220 300a0a23  RSION_MINOR 0..#
00d0 656e6469 66202f2a 205f5f43 4f4e4649  endif /* __CONFI
00e0 475f485f 5f202a2f 0a0a               G_H__ */..      
ReAl
  • 1,231
  • 1
  • 8
  • 19
  • Thanks, but I am looking for a platform independent solution. – mattmilten Oct 16 '18 at 13:11
  • As platform independent one can use something like [bin2c](https://github.com/gwilymk/bin2c) — convert _any_ file to C array. Just add appropriate lines into yours makefile. – ReAl Oct 16 '18 at 13:19