CMake 生成的可执行文件出现问题

问题描述 投票:0回答:0

我在 Qt 中为我的测试项目制作了自定义 CMakeLists.txt 文件。该项目构建得很好,并创建了一个可执行文件。但是,我收到一条错误消息,提示“此应用程序无法在您的电脑上运行”。我找不到问题所在。我什至在最后添加了“WIN32_EXECUTABLE TRUE”属性,以确保其正常运行。

这是我的 CMakeLists.txt:

# Project properties
# =============================================================================

cmake_minimum_required(
    
    VERSION 3.5
)

if(WIN32)      # If operating system >> Windows
    project(
        
        Project_name
        LANGUAGES
        CXX
    )

    set(
        CMAKE_CXX_STANDARD 17
    )

    set(
        CMAKE_CXX_STANDARD_REQUIRED ON
    )

elseif(UNIX)    # If operating system >> Unix (Linux/Mac)
    project(
        
        MY_PROJECT
        LANGUAGES
        CXX
    )

    set(
        CMAKE_CXX_STANDARD 17
    )

    set(
        CMAKE_CXX_STANDARD_REQUIRED ON
    )

endif()

# =============================================================================
# Project properties





# Qt properties
# =============================================================================

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)   # Automatic UI  compilation
set(CMAKE_AUTOMOC ON)   # Automatic MOC compilation
set(CMAKE_AUTORCC ON)   # Automatic RCC compilation

set(
    CMAKE_PREFIX_PATH
    
    # Enivronment path to Qt
    $ENV{QT_COMP_DIR}
)

find_package(
    Qt6
    
    REQUIRED COMPONENTS

    # List of components
    Core
    Widgets
)

# =============================================================================
# Qt properties





# Project files
# =============================================================================


set(
    FORMS_DIR
    
    # Path to forms
    "${CMAKE_SOURCE_DIR}/forms"
)

set(
    INCLUDE_DIR
    
    # Path to includes
    "${CMAKE_SOURCE_DIR}/include"
)

set(
    SOURCE_DIR
    
    # Path to sources
    "${CMAKE_SOURCE_DIR}/src"
)

set(
    RESOURCE_DIR

    # Path to resources
    "${CMAKE_SOURCE_DIR}/resources"
)


# Add the forms directory to the AUTOUIC search paths
set(
    CMAKE_AUTOUIC_SEARCH_PATHS
    
    # Paths which it must look for
    ${CMAKE_AUTOUIC_SEARCH_PATHS}
    ${FORMS_DIR}
)


include_directories(

    ${FORMS_DIR}
)

include_directories(
    
    ${INCLUDE_DIR}
)

include_directories(
    
    ${SOURCE_DIR}
)


file(
    GLOB_RECURSE
    
    FILES
    
    # Searching for all files
    "${FORMS_DIR}/*.ui"
    "${INCLUDE_DIR}/*.h"
    "${SOURCE_DIR}/*.cpp"
    "${RESOURCE_DIR}/*.qrc"
)

# =============================================================================
# Project files





# Executable
# =============================================================================

if(WIN32) # Windows version
    
    if(Qt${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        
        qt_add_executable(

            ${PROJECT_NAME}

            MANUAL_FINALIZATION

            ${FILES}
        )

    else ()

        add_executable(

            ${PROJECT_NAME}

            ${FILES}
        )

    endif()

elseif(UNIX) # Unix version
    
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        
        qt_add_executable(

            ${PROJECT_NAME}

            MANUAL_FINALIZATION

            ${FILES}
        )

    else ()

        add_executable(

            ${PROJECT_NAME}

            ${FILES}
        )

    endif()

endif()


# Add the target includes for MY_PROJECT 
target_include_directories(

    ${PROJECT_NAME}
    
    PRIVATE
    
    ${FORMS_DIR}
)

target_include_directories(
    
    ${PROJECT_NAME}
    
    PRIVATE
    
    ${INCLUDE_DIR}
)

target_include_directories(
    
    ${PROJECT_NAME}

    PRIVATE

    ${SOURCE_DIR}
)

# =============================================================================
# Executable





# Libraries
# =============================================================================

target_link_libraries(
    
    ${PROJECT_NAME}
    
    # Libraries
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Widgets
)

# =============================================================================
# Libraries





# Additional properties
# =============================================================================

set_target_properties(
    
    ${PROJECT_NAME}
    PROPERTIES
    
    WIN32_EXECUTABLE TRUE
)

install(

    TARGETS
    ${PROJECT_NAME}

    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

if(Qt${QT_VERSION_MAJOR} EQUAL 6)

    qt_finalize_executable(
        
        ${PROJECT_NAME}
    )

endif()

# =============================================================================
# Additional properties

有人可以向我解释一下问题是什么吗?

qt cmake executable
© www.soinside.com 2019 - 2024. All rights reserved.