首页 > 解决方案 > 安装期间 CMake 链接 libQt6Widgets.so.6

问题描述

我想使用 CMake 安装我的 C++ 程序(使用 Qt)。我可以在构建目录中构建并运行它。但是 cmake 命令“安装”不会复制(或不使用)链接库。

我从一个模板项目(由 MIT 提供)开始(从头开始),其中 bash 脚本是:

export BashMatic="${HOME}/.bashmatic"

source "${BashMatic}/init.sh"

export ProjectRoot=$(pwd)
export BuildDir="${ProjectRoot}/build/run"
export BashLibRoot="${ProjectRoot}/bin/lib-bash"

randomGenerator.header() {
  h1.purple "Personal random project: A CMake Project Template with Tests"
  local OIFC=${IFC}
  IFS="|" read -r -a gcc_info <<< "$(gcc --version 2>&1 | tr '\n' '|')"
  export IFC=${OIFC}
  h1 "${bldylw}GCC" "${gcc_info[1]}" "${gcc_info[2]}" "${gcc_info[3]}" "${gcc_info[4]}"
  h1 "${bldylw}CMAKE:  ${bldblu}$(cmake --version | tr '\n' ' ')"
}

randomGenerator.setup() {
  hl.subtle "Creating Build Folder..."
  run "mkdir -p build/run"

  #[[ -f .idea/workspace.xml ]] || cp .idea/workspace.xml.example .idea/workspace.xml
}

randomGenerator.clean() {
  hl.subtle "Cleaning output folders..."
  run 'rm -rf bin/d* include/d* lib/*'
}

randomGenerator.build() {
  run "cd build/run"
  run "cmake ../.. "
  run.set-next show-output-on
  run "make -j 12"
  run "make install"
  run "cd ${ProjectRoot}"
}

randomGenerator.tests() {
  hr
  if [[ -f bin/randomGenerator_tests_catch ]]; then
    run.set-next show-output-on
    run "echo && bin/randomGenerator_tests_catch"
  else
    printf "${bldred}Can't find installed executable ${bldylw}bin/randomGenerator_tests_catch.${clr}\n"
    #exit 2
  fi
  hr
}

randomGenerator.examples() {
  [[ ! -f bin/Temp ]] && {
    error "You don't have the cmpiled binary yet".
    #exit 3
  }

  run.set-all show-output-on

  hr
  run "bin/Temp"
  hr

}

main() {
  randomGenerator.header
  randomGenerator.setup
  randomGenerator.build
  randomGenerator.tests
  randomGenerator.examples
}

(( $_s_ )) || main

我用 CMake 添加了一个 Qt6 模板,其中“main”-CMakeLists.txt 是:

cmake_minimum_required(VERSION 3.2)

#You can change this to Qt5 if you
#are not ready to migrate to Qt6.
set(QT_MAJOR_VERSION 6)

set(TARGET_NAME Temp)
project(${TARGET_NAME} VERSION 0.0.1 DESCRIPTION "Project Description")

#Make sure CMake will take care of moc for us
set(CMAKE_AUTOMOC ON)

#Set the CXX standard. Qt 6 I believe uses c++ 17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#See if the environment var is set
if(DEFINED ENV{Qt${QT_MAJOR_VERSION}_HOME})
    message(STATUS "Looking for Qt in: " $ENV{Qt${QT_MAJOR_VERSION}_HOME})
else()
    message(STATUS "Qt${QT_MAJOR_VERSION}_HOME environment variable not set. Checking default paths.")
endif()

#Qt6 requires setting the CMAKE_PREFIX_PATH whereas 5 does not.
if("${QT_MAJOR_VERSION}" STREQUAL "6")
    set(CMAKE_PREFIX_PATH $ENV{Qt${QT_MAJOR_VERSION}_HOME})
endif()

#find_package should find everything fine so long as the ENV Variable is set or, for linux systems,
#it is in the default install path.
find_package(Qt${QT_MAJOR_VERSION} COMPONENTS Widgets REQUIRED PATHS $ENV{Qt${QT_MAJOR_VERSION}_HOME})


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -O3")

set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR})

set(PROJECT_INSTALL_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(PROJECT_INSTALL_BIN_DIR ${PROJECT_SOURCE_DIR}/bin)
set(PROJECT_INSTALL_LIB_DIR ${PROJECT_SOURCE_DIR}/lib)

set(PROJECT_HEADERS_DIR ${PROJECT_SOURCE_DIR}/src/randomGenerator)

include_directories(${PROJECT_INSTALL_INCLUDE_DIR})
include_directories(${PROJECT_HEADERS_DIR})

add_subdirectory(src)
add_subdirectory(tests_catch)

在子目录“src”中,我有 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.16)

add_subdirectory(randomGenerator)

set(SOURCE_FILES 
    include/window.hpp
    src/window.cpp
    src/main.cpp)

add_executable(${TARGET_NAME} ${SOURCE_FILES})
    
if(MSVC)
    target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
else()
    target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
endif()

target_include_directories(${TARGET_NAME} PRIVATE ${Qt${QT_MAJOR_VERSION}_INCLUDE_DIRS})
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)

set_target_properties(${TARGET_NAME} PROPERTIES DEBUG_OUTPUT_NAME "${TARGET_NAME}d" RELEASE_OUTPUT_NAME ${TARGET_NAME})

#Add any extra libs to link also.
target_link_libraries(${TARGET_NAME} PRIVATE Qt${QT_MAJOR_VERSION}::Widgets)

install(TARGETS ${TARGET_NAME} DESTINATION ${PROJECT_INSTALL_BIN_DIR})

如果这样做,我可以在 build/run/src 目录中运行程序(名为)Temp

但“bin”中的“已安装”程序返回错误消息

bin/Temp:加载共享库时出错:libQt6Widgets.so.6:无法打开共享对象文件:没有这样的文件或目录

我想我必须用这条线改变一些东西

target_link_libraries(${TARGET_NAME} PRIVATE Qt${QT_MAJOR_VERSION}::Widgets)

但我不知道“什么”。

标签: c++linuxbashqtcmake

解决方案


推荐阅读