26

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 up Qt creator to use cmake "Cmake": see this, this and this documents regarding how I did that.

I successfully create hello world project, but I can't create files in project, only add existing files to project tree and after that adding it to cmake list. Standard operation of Qt creator "Add New..." doesn't work and I can't find why.

Is there anybody who uses Qt creator and "Cmake" together? Is the combination actually possible?

Note: I'm using Qt creator v2.4.1.

5 Answers 5

14

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Keep in mind though, that it's considered a bad practice for CMake to use globing for collecting source files.
I'm using file(GLOB_RECURSE SRC *.cpp)
CMake is quite smart in running itself from a makefile. I've never had to manually run CMake and I use globs constantly...
What CMake version do you use?
10

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.

7 Comments

May be I don't have been enough cleared I search for way to create files in directory. Right button on folder ->add item and etc.This don't work for me when i using cmake
@themean - I took your question to be: Is it possible to add files to a CMake based project in QtCreator? And if so, how do you do it? If I've misunderstood this please edit your question to clarify what you're trying to ask.
I've clarified my answer a bit does this help?
Okay it is work but no point for me. I aways can duplicate some .cpp and rename him(faster way). I want to work with qt creator like normal ide I searching for plugin but the only think i found is this
May be soon i will can found solution of the problme
|
5

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.

Comments

3

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

Comments

2

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 :

enter image description here

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.

2 Comments

I don't see this option in QT Creator 4.5.2.
I don't precisely know when this option has been added. I've been using it for a couple of years now. When I wrote the answer the latest version of QT Creator was 4.7, I will edit my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.