Bare bones working example and documentation

master
Martin Felis 2021-01-31 21:23:42 +01:00
parent d9da225531
commit 900f215858
5 changed files with 180 additions and 2 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.*.swp
build*

37
CMakeLists.txt Executable file
View File

@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.16)
project(QtClangTest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
add_executable(${PROJECT_NAME} main.cpp)
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
if (WIN32)
set(DEBUG_SUFFIX)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif ()
if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif ()
foreach (QT_LIB ${REQUIRED_LIBS})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()

View File

@ -1,3 +1,56 @@
# cross_cmake_clang_qt # Cross Compiling a Qt5 App using Clang and CMake
Example that uses CMake, Clang, and Qt to cross compile a simple application for Windows with MSVC compatibility. Example that uses CMake, Clang, and Qt to cross compile a simple application for Windows with MSVC compatibility.
Parts of it based on: https://gist.github.com/HiImJulien/3eb47d7d874fe5483810bd77940e74c0
## Requirements
- Clang Version 10
- Visual Studio Build Tools: https://visualstudio.microsoft.com/downloads/
- Qt5: https://download.qt.io/official_releases/qt
## Instructions Linux
### Requirements:
- Qt5 development packages
- Clang 10
### Steps
- Copy MSVC build tools into the project directory:
- Copy `C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/<MSVC Version>` to `msvc_build_tools/MSVC`
- Copy Windows SDK into the project directory:
- From `C:/Program Files (x86)/Windows Kits/10` copy the folders
* `Include` to `msvc_build_tools/Kit`
* `Lib` to `msvc_build_tools/Kit`
- Both directories have sub directories with the Windows SDK Version as
name.
- Adjust `clang_windows_toolchain.cmake` to use the proper Windows SDK
Version in variable `WINSDK_VER`
- Copy the Qt5 msvc development files into the project directory:
- Copy `C:/Qt/Qt5.12.10/5.12.10/msvc2017_64` to `qt5_msvc2017_64`
- Configure project using:
`cmake -DCMAKE_TOOLCHAIN_FILE=clang_windows_toolchain.cmake <project dir>`
## Instructions Windows with JetBrains CLion
### Requirements:
- Download and Install Qt5 (MinGW libraries not needed)
- Clang 10 (https://releases.llvm.org/download.html)
- Install MinGW Make
- Install JetBrains CLion (https://jetbrains.com/clion)
### Steps
- Open project
- Adjust Toolchain
- Go to Toolchain Settings
- Remove existing Toolchains
- Add MinGW Toolchain
- Adjust CMake Profiles
- Add `-DCMAKE_TOOLCHAIN_FILE=clang_windows_toolchain.cmake` as CMake
parameters

76
clang_windows_toolchain.cmake Executable file
View File

@ -0,0 +1,76 @@
# Cross toolchain configuration for using clang-cl.
set(WINSDK_VER "10.0.19041.0")
set(QT5_VERSION "5.12.20")
if(WIN32)
set(CMAKE_C_COMPILER "C:/llvm/bin/clang-cl.exe")
set(CMAKE_CXX_COMPILER "C:/llvm/bin/clang-cl.exe")
set(CMAKE_LINKER "C:/llvm/bin/lld-link.exe")
set(CMAKE_PREFIX_PATH "C:/Qt/Qt${QT5_VERSION}/${QT5_VERSION}/msvc2017_64/lib/cmake/Qt5")
set(QT_INSTALL_PATH "C:/Qt/Qt${QT5_VERSION}/${QT5_VERSION}/msvc2017_64")
set(MSVC_BASE "C:/MSVCBuildTools/MSVC")
set(WINSDK_BASE "C:/MSVCBuildTools/Kit")
else()
set(CMAKE_C_COMPILER "/usr/bin/clang-cl-10")
set(CMAKE_CXX_COMPILER "/usr/bin/clang-cl-10")
set(CMAKE_LINKER "/usr/bin/lld-link-10")
set(Qt5_DIR "${PROJECT_SOURCE_DIR}/qt5_msvc2017_64/lib/cmake/Qt5")
set(QT_INSTALL_PATH "${PROJECT_SOURCE_DIR}/qt5_msvc2017_64")
set(MSVC_BASE "${PROJECT_SOURCE_DIR}/msvc_build_tools/MSVC")
set(WINSDK_BASE "${PROJECT_SOURCE_DIR}/msvc_build_tools/Kit")
endif()
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0)
set(CMAKE_SYSTEM_PROCESSOR AMD64)
set(MSVC_INCLUDE "${MSVC_BASE}/include")
set(MSVC_LIB "${MSVC_BASE}/lib")
set(WINSDK_INCLUDE "${WINSDK_BASE}/Include/${WINSDK_VER}")
set(WINSDK_LIB "${WINSDK_BASE}/Lib/${WINSDK_VER}")
set(COMPILE_FLAGS
-D_CRT_SECURE_NO_WARNINGS
-imsvc "'${MSVC_INCLUDE}'"
-imsvc "'${WINSDK_INCLUDE}/ucrt'"
-imsvc "'${WINSDK_INCLUDE}/shared'"
-imsvc "'${WINSDK_INCLUDE}/um'"
-imsvc "'${WINSDK_INCLUDE}/winrt'"
)
string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
set(LINK_FLAGS
/manifest:no
-libpath:"${MSVC_LIB}/x64"
-libpath:"${WINSDK_LIB}/ucrt/x64"
-libpath:"${WINSDK_LIB}/um/x64"
)
string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)

10
main.cpp Executable file
View File

@ -0,0 +1,10 @@
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QPushButton button("Hello world!", nullptr);
button.resize(200, 100);
button.show();
return QApplication::exec();
}