Stephane
Stephane

Reputation: 153

Compile Qt project on VSCode with CMake

My basic project compile, just inheriting a QMainWindow and showing it, but all the include appears in red, and i have the following error even if the project compile:

Impossible to open the file source "QtWidgets/qtwidgetsglobal.h" (dependancy of QMainWindow")

The VSCode intellisense is also not working.

I downloaded Qt-5.12 from the git repo. I compiled and then installed in /usr/local/Qt-5.12/.

My c_cpp_properties.json:

    "configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**",
            "/usr/local/Qt-5.12/lib",
            "/usr/local/Qt-5.12/lib/QtWidgets.framework/Headers",
            "/usr/local/Qt-5.12/lib/QtGui.framework/Headers",
            "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers",
            "/usr/local/Qt-5.12/lib/QtCore.framework/Headers",
            "/usr/local/Qt-5.12/./mkspecs/macx-clang"
        ],
        "defines": [],
        "macFrameworkPath": [
            "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64",
        "configurationProvider": "vector-of-bool.cmake-tools"
    }
],
"version": 4
}

here is my project tree:

enter image description here

my top level CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(irc_client VERSION 1.0)

set(CMAKE_PREFIX_PATH "/usr/local/Qt-5.12/")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(Qt5 COMPONENTS Core Widgets REQUIRED)

add_subdirectory(sources)

my sources/CMakeLists.txt:

add_executable(${PROJECT_NAME} main.cpp irc_client.cpp)

target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets)
target_include_directories(${PROJECT_NAME} PUBLIC 
${irc_client_SOURCE_DIR}/headers/)

How do i setup vscode with Qt and CMAKE to have intellisense working please ? What am i doing wrong here ? i can't find my exact issue, and i know QtCreator is better to code Qt apps, but still i want to try to figure out why this isn't working. Because it complains it can't find the includes but still it compiles...

thank you

Upvotes: 0

Views: 3729

Answers (1)

Stephane
Stephane

Reputation: 153

Ok seems i found a solution. Here is what my c_cpp_properties.json looks like now:

{
"configurations": [
    {
        "name": "Mac",
        "macFrameworkPath": [    
            "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk\
                 /System/Library/Frameworks",
            "/System/Library/Frameworks"
        ],
        "intelliSenseMode": "clang-x64",
        "compilerPath": "/usr/bin/clang++",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "compileCommands": "${workspaceFolder}/build/compile_commands.json",
        "browse": {
            "path": ["/usr/local/Qt-5.12/lib"],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db"
          }
    }
],
"version": 4
}

the browse part make the job i think, and i also added "compileCommands" part to point to my cmake generated compile commands file.

So far no more errors in the incude and intellisense working.

Hope it serves others.

Upvotes: 0

Related Questions