2
votes

I am using QtCreator with CMake. In my CMakeLists.txt files I use GLOB_RECURSE QOBJECT_SOURCES "*.cpp", just to avoid editing the file when adding a new source. That time of course CMake does not changes its mind about the available files it should compile. I expected that if I use Build/Clean, it will clean caches and when I re-compile the project, my new source files will be found. However, QtCreator neglects the newly added file. I need to clean the build directory manually, and run cmake ..; make. So, what exactly Build/Clean makes and what is the difference versus Build/Rebuild? Is there anything in QtCreator, I can make my new file known to the compiler?

2

2 Answers

1
votes

When one push "Build > Clear CMake Configuration", the file CMakeCache.txt and the directory CMakeFiles from the Build directory are deleted :

https://github.com/qtproject/qt-creator/blob/master/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp

    m_clearCMakeCacheAction [...] "Clear CMake Configuration"
    [...]
    connect(m_clearCMakeCacheAction [...] clearCMakeCache(SessionManager::startupProject());

https://github.com/qtproject/qt-creator/blob/master/src/plugins/cmakeprojectmanager/builddirmanager.cpp

void BuildDirManager::clearCache()
{
    auto cmakeCache = Utils::FileName(workDirectory()).appendPath(QLatin1String("CMakeCache.txt"));
    auto cmakeFiles = Utils::FileName(workDirectory()).appendPath(QLatin1String("CMakeFiles"));

    const bool mustCleanUp = cmakeCache.exists() || cmakeFiles.exists();
    if (!mustCleanUp)
        return;

    Utils::FileUtils::removeRecursively(cmakeCache);
    Utils::FileUtils::removeRecursively(cmakeFiles);

    forceReparse();
}


What version of QtCreator and CMake are you using ?

I suspect the behavior you described is previous to QtCreator 4.1 (Or at least I remember something similar happened me before I jumped to version 4.1, available at QtCreator snapshots ).

Just in case, you should try latest and see if that solve your problem, because I am using QtCreator 4.2 and CMake 3.7 (and previously QtCreator 4.1 and Cmake 3.5), and I do not see what you describe.

I setup files like you (Although CMake developers do not recommend it). I mean that in CMakeList.txt I mostly add a GLOB ( or GLOB_RECURSE if I want scan recursively the pointed directories).

That way, after one create a new file in those directories, in Qt Creator just it's needed to push "Build > Run CMake", or at editor modify CMakeList.txt with a blank space plus a CTRL+S (if one have activated 'Autorun CMake' at "Options > Build & Run > CMake"). Both actions update the listed files tree with the new files, ready for a Build and Run (CTRL+R).

From my humble point of view, I think it's the more comfortable way when one is constantly creating and renaming files in a new project ( In my case I add *.h and *.hpp also, for be able to see those files listed at project ).

0
votes

That is the problem with CMake's file(GLOB ...): It is only evaluated when CMake is run and not when make is run.

QtCreator's Build depends on make's ability to detect changes and will invoke CMake when it detects changes to the CMakeLists.txt files.

The most robust - and recommended - way is to explicitly name and list all source and header files manually.