CMake: list(REMOVE_ITEM) does not handle escaped \#

I work with a dev that uses emacs-- as a result, they sometimes generate swapfiles (call one #cmaketest.cpp) which contain the pound character within a directory marked for

file(GLOB_RECURSE MY_SOURCES my/dir/*.cpp). 

In order to prevent these files from being compiled, I would like to be able to:


This CMakeFile successfully compiles, but fails on Make, and inspection of the generated Makefile shows references to #cmaketest.cpp, which it should not.

This is reproducible with the MWE below:

cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)

add_library(cmaketest ${TEST_SOURCES})
target_include_directories(cmaketest PRIVATE ${PROJECT_SOURCE_DIR})

And a file in the PROJECT_SOURCE_DIR named


Am I losing my mind? Am I just failing to escape this character properly, do I fundamentally misunderstand how list(REMOVE_ITEM) works? Is the target_include_directories command the problem? Thanks!

1 answer

  • answered 2018-03-14 04:54 Dr. Watson

    Yes, I was losing my mind. FILTER is for removing regex-matched list items, not REMOVE_ITEM.