2
votes

I know Qt Creator support to build single file from Menu "Build" -> “Build

File”,or right click one source file in project tree to build single file,

but when I load a Cmake Project, Menu “Build File” and “right click” are both

disappeared.

Does it mean build single file functionality is only available for qmake?

Does cmake project support build single file, if so, how to do it? if not

is there any workaround?

Thanks, Le

1
CMake does only create targets you explicitly specify, not for every file. Probably qmake is different, thus the support from Qt Creator.usr1234567
I've added a feature request on QtCreator's issue tracker.DoDo

1 Answers

0
votes

QT currently doesn't support this feature. However you can add the following hack that works quite well. (This hack will replace the clean project command with build command. but you can easily restore it when needed)

  1. Make sure sed is installed:

    sudo apt-get install sed

  2. Add the following bash script somewhere on your computer under the name "CompileSingle.sh"

    CppPath=$1

    BuildPath=$2

    Target=$(sed -E 's/.(/|^)([^/].)\w+/\2o/g' <<< $CppPath)

    MakePath=${BuildPath}/$(sed -E 's/./Src/(.)/[^/]*$/\1/g' <<< $CppPath)>

    pushd ${MakePath} > /dev/null

    make -B ${Target}

    if [ $? -eq 0 ]

    then

    echo "^^^^^^^^^^^^^^ Build Succesfully ^^^^^^^^^^^^^^^"

    else

    echo "!!!!!!!!!!!! Build Failed !!!!!!!!!!!!!!!!"

    fi

    popd > /dev/null

  3. Enable running the script:

    chmod +x CompileSingle.sh

  4. Go to qt creator and load your project

  5. Go to: Projects -> Build -> Clean Steps
  6. Disable the current step there by clicking the 0 button
  7. Select: Add Clean Step -> Custom Process Step
  8. Set the following values:

    Command: bash

    Arguments: CompileSingle.sh "%{CurrentDocument:FilePath}" "%{CurrentProject:BuildPath}"

    Working Directory: [Path to where you placed CompileSingle.sh]

  9. Go to Tools->Option->Environment->Keyboard

  10. Map: Project Explorer->Clean to a key

You should now be able to select a file and press the key you mapped the clean function to end it will compile it.