0

I know that we can configure a file using cmake as discussed in Can CMake generate configure file? for example.

My problem is that I want to update just one line of a file. For instance, assume that this line contains app version:

my_header_version.h.in:

#include "${CMAKE_SOURCE_DIR}/src/definitions.h"

CMakeLists.txt:

...
configure_file(my_header_version.h.in my_header_version.h)
...

my_header_version.h:

#include "/home/myusr/dev/src/definitions.h"

The problem is that if I change my_header_version.h into:

#include <some_lib.h>
#include "/home/myusr/dev/src/definitions.h"

and after that I run cmake .. command, then the line #include <some_lib.h> gets lost. What should I do if I want to change my_header_version.h directly without changing my_header_version.h.in anytime?!

Willy Cole
  • 134
  • 9
  • 1
    "What should I do if I want to change `my_header_version.h` directly without changing `my_header_version.h.in` anytime?!" - I know no such ready-to-use functionality in CMake. `configure_file` is intended to generate the **whole file**. If you want to "configure" only a *part* of the file, then move this part into a **separate header**, which could be generated with `configure_file`. This generated header could be included into the original one with standard `#include` directive. – Tsyvarev Oct 20 '19 at 12:32
  • @Tsyvarev you're right in this case and excuse me for my misunderstanding description; I updated the question – Willy Cole Oct 20 '19 at 12:37
  • @Tsyvarev as u can c, my_header_version.h contains all include files; so may be it does not make sense to separate those `#include` statements – Willy Cole Oct 20 '19 at 12:39
  • 2
    Oh, you want to modify the **generated** file **directly**. While it is possible to implement this semantic, it would be not such simple. Common approach is not to modify the *generated* file, but modify its *template* instead. – Tsyvarev Oct 20 '19 at 12:52
  • @Tsyvarev I don't want to change generated file; I just don't want to change .h.in file every time because it is not a part of source files directly – Willy Cole Oct 20 '19 at 13:09
  • @Tsyvarev maybe separating template structures from non-template ones is a good approach as u said in your first comment! not sure! – Willy Cole Oct 20 '19 at 13:11

0 Answers0