# configure\_file breaks multiline comments

**URL:** https://discourse.cmake.org/t/configure-file-breaks-multiline-comments/3723
**Category:** Development
**Created:** [July 10, 2021, 4:41pm UTC](https://discourse.cmake.org/t/configure-file-breaks-multiline-comments/3723 "2021-07-10T16:41:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Albrecht-S](https://discourse.cmake.org/user_avatar/discourse.cmake.org/albrecht-s/32/1407_2.png) [@Albrecht-S](https://discourse.cmake.org/u/Albrecht-S)
#### Post date: [July 10, 2021, 4:41pm UTC](https://discourse.cmake.org/t/configure-file-breaks-multiline-comments/3723/1 "2021-07-10T16:41:03Z")

</div>

CMake version: 3.20.5 (and older)

I’m not sure if my observation is intended behavior or a bug. I noticed that configure\_file()

- does not check for C (multiline) comments
- removes all text in a line before #cmakedefine…
- expands #cmakedefine… inside C/C++ comments

Example: ‘#cmakedefine HAVE\_JPEG 1’ inside a multiline C comment triggers a compilation error if HAVE\_JPEG is false. After expansion the terminating ‘\*/’ ends the existing multiline comment which leads to a compilation error in the following line(s).

This was a real case; I was trying to document CMake syntax in the input file.

All examples in the docs show ‘#’ at the beginning of the line. There’s no clue what will happen if it is inside a comment block and whether it is expanded if ‘#’ is not the first character or maybe after optional whitespace.

Please see more details in the attached complete project in text form:  
[configure\_file\_breaks\_comments.txt](https://discourse.cmake.org/uploads/short-url/pqwZtankKiSbSGswUO1QpIHcpuv.txt) (1.7 KB)

Questions:  
(1) Is this behavior intended?  
(2) If yes (or no) could (should) this be documented?

Thanks for reading and for any clues.

PS: I mitigated this in my project by replacing ‘#’ with ‘[#]’, hence there’s no problem for me anymore. Please consider this as an attempt to report a bug or trigger improvement of the documentation.

---

<div class="post-metadata">

### Author: ![Albrecht-S](https://discourse.cmake.org/user_avatar/discourse.cmake.org/albrecht-s/32/1407_2.png) [@Albrecht-S](https://discourse.cmake.org/u/Albrecht-S)
#### Post date: [July 10, 2021, 6:01pm UTC](https://discourse.cmake.org/t/configure-file-breaks-multiline-comments/3723/2 "2021-07-10T18:01:07Z")

</div>

BTW: This code:  
` // NOT USED: #cmakedefine HAVE_ZLIB 1`  
gets expanded to:  
` // NOT USED: #define HAVE_ZLIB 1`  
(if HAVE\_ZLIB is true, obviously)

which I think is at least inconsistent because the leading ‘`// comment`’ is NOT removed - and removing it would in all cases lead to unexpected behavior.

My suggestion would be to **not expand** `#cmakedefine` at all if it’s not the first token (after optional whitespace) in a source line.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 13, 2021, 1:53pm UTC](https://discourse.cmake.org/t/configure-file-breaks-multiline-comments/3723/3 "2021-07-13T13:53:38Z")

</div>

Please file [an issue](https://gitlab.kitware.com/cmake/cmake/-/issues). I don’t know that fixing it is all that doable since it would (probably) need to be a policy that probably isn’t all that easy to implement. I think mentioning this behavior as a note in the documentation is certainly the place to start. Any new policy which fixes it can then update that documentation to mention the policy.

---

<div class="post-metadata">

### Author: ![Albrecht-S](https://discourse.cmake.org/user_avatar/discourse.cmake.org/albrecht-s/32/1407_2.png) [@Albrecht-S](https://discourse.cmake.org/u/Albrecht-S)
#### Post date: [July 13, 2021, 2:00pm UTC](https://discourse.cmake.org/t/configure-file-breaks-multiline-comments/3723/4 "2021-07-13T14:00:34Z")

</div>

Thanks for reading and for your reply.

I’ll file an issue as requested.

Done: [https://gitlab.kitware.com/cmake/cmake/-/issues/22417](https://gitlab.kitware.com/cmake/cmake/-/issues/22417)
