Using Cmake with Qt Creator

前端 未结 5 974
余生分开走
余生分开走 2020-12-15 18:19

I would like to use Qt creator and Cmake together (please, don\'t ask me about my motivation, accept this as a given.)

I successfully set u

相关标签:
5条回答
  • 2020-12-15 18:20

    I'm adding an updated answer for newer versions of QtCreator (4.x, I don't know precisely which release but at least from 4.7). In the Tools > Options... menu, choose the Build & Run section and then the CMake tab. You will see the Adding Files settings, and you can set it to Copy file paths :

    This way, when you want to add a new file to your project, in the Project view, right click on the desired CMake Executable/Library's name and select Add New..., go through the Add dialog, and when you'll validate the dialog, QtCreator will open CMakeLists.txt in the Editor view. Finally, paste the content of the clipboard at the end of the corresponding source file list and save CMakeLists.txt. The CMake project will be parsed, and your new file will show up in the Project view.

    0 讨论(0)
  • 2020-12-15 18:23

    You can add files using glob expression in your CMakeLists.txt, like this:

    file(GLOB SRC . *.cpp)
    add_executable (your_exe_name ${SRC})
    

    Cmake will pick your new cpp files next time you run it and QtCreator will show them in the project browser.

    Update

    This solution may be useful but as noted in comments - this is not a good practice. Every time somebody add new source file and commit changes, you need to rerun cmake to build all the sources. Usually I just touch one of the CMakeLists.txt files if my build is broken after I pool recent changes from repository. After that make will run cmake automatically and I didn't need to run it by hands. Despite of that I think that explicit source lists in CMakeLists.txt is a good thing, they called thing CMake Lists for a reason.

    0 讨论(0)
  • 2020-12-15 18:26

    I solve this problem that I added new files in standard way (CTRL+N), then added needed files in CMakeLists. After that, right click on project in project tree view and choose option Run CMake. After this, files showed in project list tree. Only build was not enough.

    0 讨论(0)
  • 2020-12-15 18:31

    When you add new files in QtCreator using the "New File or Project..." dialog it only creates the files on disk, it doesn't automatically add the files to the CMakeLists.txt. You need to do this by hand by editing the CMakeLists.txt file.

    The next time you build the project, CMake will be re-run, and QtCreator will pick up the new files and show them in the project browser.

    0 讨论(0)
  • 2020-12-15 18:42

    I tested here and happened the same behavior because those options you were asking were really disabled.

    Use File -> "New File or Project..." or CTRL+N to add new files and after that add to CMakeLists.txt

    0 讨论(0)
提交回复
热议问题