Search code examples
windowscmakeline-breaksmultiplatform

Line termination in source for multiplatform C++ project and CMakeLists.txt


After training and working almost exclusively under gnu-linux, I came across a multiplatform project I'll have to work on.

I was quite disappointed to find that in the repo there is a mix of LF and CRLF line feeds. I noticed that most CRLF were introduced by two committers and ran dos2unix on the involved files.

However, I also noticed that all CMakeLists.txt files were CRLF-terminated even if they came from commits by the main developer (who had committed all other sources with LF line endings), so for the moment I left them alone.

I'm not very familiar with windows or multiplatform development, nor with cmake and I was wondering: is there any convention? Or, is there any cmake-specific tool that generates CRLF even on gnu-linux?

If it may matter, the repo is at https://github.com/glipari/rtlib2.0


Solution

  • There are CMake rules:

    Newlines may be encoded as either \n or \r\n but will be converted to \n as input files are read.

    https://cmake.org/cmake/help/v3.3/manual/cmake-language.7.html#encoding

    All project I know use \n and ask their developers to configure their editors accordingly. Similar to trailing whitespace and the use of tabs.

    If you are using Git you can ask your fellow developers to use a Git commit hook that checks for line breaks. If they are not following your convention, the commit is rejected (unless the developer disables it).