首页 > 解决方案 > Windows下Linux下cmake与QT交叉编译

问题描述

我想在 Windows 下为 Linux 交叉编译一个 cmake 项目。该项目使用QTBase。我做了什么:从官方存储库下载了 QTBase 的源代码。用 i686-mingw 进一步编译。在 cmake 中(我在下面引用)。我包含了推荐的 mingw 文件,它覆盖了编译器的路径(我在下面引用)。应用程序编译成功,但 wine 报告缺少一些库(也在下面)。在 Windows 中,这个编译后的应用程序也无法运行,原因是缺少库。如何正确描述 cmake?

# the name of the target operating system
SET(CMAKE_SYSTEM_NAME Windows)

# Choose an appropriate compiler prefix

# for classical mingw32
# see http://www.mingw.org/
#set(COMPILER_PREFIX "i586-mingw32msvc")

# for 32 or 64 bits mingw-w64
# see http://mingw-w64.sourceforge.net/
set(COMPILER_PREFIX "i686-w64-mingw32")
#set(COMPILER_PREFIX "x86_64-w64-mingw32")

# which compilers to use for C and C++
find_program(CMAKE_RC_COMPILER NAMES ${COMPILER_PREFIX}-windres)
#SET(CMAKE_RC_COMPILER ${COMPILER_PREFIX}-windres)
find_program(CMAKE_C_COMPILER NAMES ${COMPILER_PREFIX}-gcc)
#SET(CMAKE_C_COMPILER ${COMPILER_PREFIX}-gcc)
find_program(CMAKE_CXX_COMPILER NAMES ${COMPILER_PREFIX}-g++)
#SET(CMAKE_CXX_COMPILER ${COMPILER_PREFIX}-g++)


# here is the target environment located
SET(USER_ROOT_PATH /home/erk/erk-win32-dev)
SET(CMAKE_FIND_ROOT_PATH  /usr/${COMPILER_PREFIX} ${USER_ROOT_PATH})

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

CMake 项目:

cmake_minimum_required(VERSION 3.16.3)
include(../Toolchain-cross-mingw32-linux.cmake)
project(naiveTextEditor LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_PREFIX_PATH /home/dym/CLionProjects/QTShared)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(${PROJECT_NAME}
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        resources.qrc)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Widgets)
set_target_properties(${PROJECT_NAME}
        PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Core> $<TARGET_FILE_DIR:${PROJECT_NAME}>
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Gui> $<TARGET_FILE_DIR:${PROJECT_NAME}>
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Widgets> $<TARGET_FILE_DIR:${PROJECT_NAME}>
        )
#set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-static-libgcc -static-libstdc++")

酒输出:

0009:err:module:import_dll Library libwinpthread-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library Qt5Core.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\naiveTextEditor.exe") not found
0009:err:module:import_dll Library libwinpthread-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library Qt5Core.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Gui.dll") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Gui.dll") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Gui.dll") not found
0009:err:module:import_dll Library Qt5Gui.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\naiveTextEditor.exe") not found
0009:err:module:import_dll Library libwinpthread-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library Qt5Core.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Widgets.dll") not found
0009:err:module:import_dll Library libwinpthread-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Core.dll") not found
0009:err:module:import_dll Library Qt5Core.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Gui.dll") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Gui.dll") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Gui.dll") not found
0009:err:module:import_dll Library Qt5Gui.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Widgets.dll") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Widgets.dll") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\Qt5Widgets.dll") not found
0009:err:module:import_dll Library Qt5Widgets.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\naiveTextEditor.exe") not found
0009:err:module:import_dll Library libgcc_s_sjlj-1.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\naiveTextEditor.exe") not found
0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\naiveTextEditor.exe") not found
0009:err:module:LdrInitializeThunk Importing dlls for L"Z:\\home\\dym\\CLionProjects\\naiveTextEditor\\bin\\naiveTextEditor.exe" failed, status c0000135

我的笔记:似乎编写了链接器路径,以便应用程序在其文件夹中查找所有 dll。我可以指定在系统文件夹中查找系统 dll 吗?

标签: c++linuxqtcmakecross-compiling

解决方案


推荐阅读