Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove tokens from a list in cmake?

I want to exclude some source files from building when not in Windows.
What is wrong in the following CMakeLists.txt cmake file?

aux_source_directory(. SRC_LIST)

# Remove Microsoft specific files
message(${SRC_LIST})

list(REMOVE_ITEM SRC_LIST stdafx.h stdafx.cpp)

message("------------------")
message(${SRC_LIST})

The contents of the messages before and after trying to remove the two files are exactly the same.

What is wrong?

like image 823
Pietro Avatar asked Aug 18 '14 13:08

Pietro


People also ask

How do I use a list in cmake?

NOTES: A list in cmake is a ; separated group of strings. To create a list the set command can be used. For example, set(var a b c d e) creates a list with a;b;c;d;e, and set(var “a b c d e”) creates a string or a list with one item in it.

What are CMakeLists txt?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.


1 Answers

You have to specify the exact name of the element you want to remove.

In your case, aux_source_directory prepends each entry with a ./, so the correct command has to be

list(REMOVE_ITEM SRC_LIST ./stdafx.h ./stdafx.cpp)

Also, please make sure you understand the implications of using manual calls to aux_source_directory for maintaining lists of source files:

It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added. Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source. When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.

Quoting the documentation for aux_source_directory.

like image 74
ComicSansMS Avatar answered Sep 18 '22 20:09

ComicSansMS