[CMake] What does find_package(COMPONENTS) do? and

Andreas Pakulat apaku at gmx.de
Thu Oct 7 02:12:53 EDT 2010


On 06.10.10 22:47:15, Stephen Kelly wrote:
> Hi,
> 
> The documentation says
> 
> > A package-specific list of components may be listed after the REQUIRED 
> > option or after the COMPONENTS option if no REQUIRED option is given. 
> 
> http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package
> 
> But it doesn't say why you would want to do that. At first I thought it was 
> to specify that only the components specified would be used to fill cmake 
> variables. However, find_package(Qt COMPONENTS QtCore) also results in 
> ${QT_QTGUI_LIBRARY} being defined for example. So then I thought maybe the 
> semantic is that if one of the components can not be found, the find_package 
> fails. So I tried find_package(Qt REQUIRED QtCore QtDoesNotExist) which 
> works without error.
> 
> What am I missing?
> 
> The reason I ask is that I finally figured out why the tests in my library 
> needed to be linked to QtGui, even though it only uses QtCore.
> 
> The reason is that find_package(Qt) causes QT_GUI_LIB to be defined. Which 
> in turn causes QTEST_MAIN to be defined to expand to use QApplication 
> instead of QCoreApplication as it is if QT_GUI_LIB is not defined.
> 
> http://qt.gitorious.org/qt/qt/blobs/4.7/src/testlib/qtest.h
> 
> find_package(Qt REQUIRED QtCore QtScript) does not cause QT_GUI_LIB to be 
> defined. However, some of my targets do need QtGui, so I should specify it, 
> right? 
> 
> So if I do specify it I'll end up having QT_GUI_LIB defined when building my 
> unit tests. I could remove_definitions(-DQT_GUI_LIB), but apart from being a 
> BadHack(tm), it causes my unit tests which *do* require QtGui to fail at 
> runtime because they create QWidgets and by undeffing QT_GUI_LIB I build 
> them to use QCoreApplication instead of QApplication (Not allowed in Qt), so 
> the tests fail at runtime.
> 
> http://gitorious.org/grantlee/grantlee/blobs/0.1/tests/CMakeLists.txt
> 
> Is there a solution to all this? What is the point of COMPONENTS if it has 
> no effect on what I can include or link to? Is it possible to link some of 
> my tests to QtGui but not all of them and still have them all pass? Do I 
> need to just link my core tests to QtGui and use QApplication and quit my 
> complaining?
> 
> The only way I can see to satisfy all requirements is attached. Is that 
> acceptable or is there a better way?

You could just use 2 find_package calls in two different subdirs. One
for the case of core-libs+core-tests and the other has
gui-libs+gui-tests. Unless I misunderstood something...

Andreas

-- 
Excellent day for putting Slinkies on an escalator.


More information about the CMake mailing list