4

I have a C source file and another (non-code) file in a project which contain the same hard-coded value. Ideally, the value would be specified in only one location and be copied to the other location automatically during the build process. It's not hard to think of hackish ways to accomplish this task, but I want to know if there is a common way of doing it.

What standard method or approach should I use to guarantee that the two values remain consistent? In other words, how should I maintain consistency in a non-hackish way?

  • Seems like they shouldn't be hard coded in the first place. – ChaosPandion Feb 15 '14 at 3:07
  • 1
    That's the point of the question. But of course, the value needs to be given SOMEWHERE. Computers don't read your mind. – user31765 Feb 15 '14 at 3:24
  • Is this something that is needed at runtime? Compile time? How is the non-code one updated/maintained? What format is that file in? How do you do builds now? – user40980 Feb 15 '14 at 3:30
  • It's not needed at runtime, or even at compile time. It is just a piece of data in some configuration file. The file is not in any particular standard format. The non-code one needs to be manually updated when there is a change. I do builds with make. – user31765 Feb 15 '14 at 3:35
  • Just read the non-code file and parse the value from that as your program starts up. If you need to be super flexible provide a command line option setting the location of the file. – Charles E. Grant Feb 15 '14 at 3:47
4

the value would be specified in only one location and be copied to the other location automatically during the build process

That's exactly the way to solve the problem - but you have to decide in which location you want to maintain the values, and which location is the one where only the copy "lives". You should also take some precaution that no one starts to change the values in the copy (for example, by adding a BIG remark on top of the list like these values are auto-generated - don't change them manually.

Here are some different ways I have solved similar requirements in the past on my own:

  • maintain the values in an Excel sheet and add a VBA macro to generate all destination files from there (like your C code and your non-code file)

  • maintain the values in a header file in code, include that header file in a generator tool which produces the non-code file and compile and run that generator within your build process

  • maintain the values directly in the non-code text file (assumed its machine-readable) and generate the C code as an includable header from there, using some script written in your favorite scripting language.

2

Avoid hard coded values in you programs - they will need to change at some stage so make them easy to change (even if they are compiled into an executable, you will at some stage need to change them, unless they are universal constants like Pi, or the speed of light). Also they will often change between environments, so make them easy to maintain by putting them in a file or database.

You have not specified what the other location is, but you have some options,

  1. use a parameter file - this could be an header file that the "other" process can read if, for performance reasons, needs to be pre-compiled into the executable, or read in during the initialization of the program.
  2. store the values in a database - this would allow you to encrypt the value if needs be (eg it is a connection string).
  3. Pass them to the program at run time as start up arguments. You could then write a wrapper script to read from the "other" source to run the program with these arguments.
1

Some high level languages are accustomed to set values in the code - values together with code are named scripts. But C doesn't belong to these languages. So, simply read values from the file.

Of course, there will be some hard set constants - that are identifying by names these values from file. For example, if you read XML or property files, where do you know from the tags/names? It is just your case.

But you should try to move the hard values to files, until this very values export is not too expensive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.