Removing files from a set

2019-02-17 00:23发布

问题:

I have a directory with files that either belong to a set that makes up a Qt project, and other files that do not. That is, files A.cxx, ADriver.cxx and A.ui all belong to a set that needs to be compiled with Qt options. I then have a file B.cxx that is non-qt. Then C.cxx, CDriver, and C.ui are another Qt set. There are tens of these, so I want to use globs rather than write each add_executable manually. I was thinking of doing something like

for(all ui files) 
  create an executable from the ui and its matching .cxx and *Driver.cxx"
end

Then all cxx files that "remain" (not used in the above loop) are non-Qt, and need to be compiled by themselves. My question is how to "subtract" files from a "set". That is, to use the method described above I'd have to have a set of all cxx files, and remove the ones that get used in the .ui file loop. Is this possible? Is there a better way to do something like this?

回答1:

First, gather all files with a glob:

file(GLOB ALL_SRCS *)

Then select ui files and create Qt targets for them, substracting them from the ALL_SRCS list at the same time:

file(GLOB UIS *.ui)

foreach(ui ${UIS})
get_filename_component(f ${ui} NAME_WE)

# Do Qt stuff
qt4_wrap_ui( ${f}uis ${ui} )
qt4_wrap_cpp( ${f}srcs ${f}.cpp ${f}Driver.cpp )
add_executable( ${f} ${f}uis ${f}srcs )

list(REMOVE_ITEM ALL_SRCS ${ui} ${f}.cpp ${f}Driver.cpp)
endforeach()

After this you'll have all non-qt sources in ALL_SRCS.



标签: cmake