Search code examples
c++qtcmakeqscintilla

How to Add QScintilla Via CMake?


I've tried to use QScintilla in order to develop my own text editor. I have been using QMake for over 3 years, and I know the way of adding the library is

config += QScintilla2

But my question is, how to add (or, target link) the library in CMake?

I've tried using target_link_library(QScintilla) and it reports "No target named 'QScintilla' found" and something similar. How to resolve it?

By the way, the QScintilla installed in my PC is built with QMake. I don't know if it is compatible with CMake or not.


Solution

  • Unfortunately, you are correct that CMake and QMake are not inter-compatible. Instead, you should copy-and-paste the results of the building into any folder (in my case it's lib/qt5_qscintilla) under the directory containing the CMake file CMakeLists.txt, plus the Qsci folder for the header files provided by the Riverbank Computing.

    For instance, on Windows and Linux, the file is libqscintilla2_qt5.a; on macOS, the file is ended with extension .dylib (dynamic library, sorry I can't really tell you the file name now).

    Then, try this:

    cmake_minimum_required(VERSION 3.5)
    project(TestingScintilla VERSION 1.0 LANGUAGES CXX)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
    find_package(QT${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
    # ... (Exactly same for Gui and Widgets)
    set(QSCINTILLA_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/lib/qt5_qscintilla/include/Qsci")
    set(QSCINTILLA_LIBRARY "${CMAKE_CURRENT_LIST_DIR}/lib/qt5_qscintilla/libqscintilla2_qt5.a")
    # Directory may vary. Change them to where your files are located.
    include_directories(${QSCINTILLA_INCLUDE_DIR})
    
    # ...
    # ...
    # ...
    target_link_libraries(TestingScintilla PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
    target_link_libraries(TestingScintilla PRIVATE ${QSCINTILLA_LIBRARY})