首页 > 解决方案 > 使用 cmake 和 Visual Studio 2019 生成器创建共享库时缺少 .lib 文件

问题描述

我创建了一个个人 C++ 库,并尝试在其他程序中使用它。要创建和编译我的库,我使用命令

cmake -G "Visual Studio 16 2019" -A x64 ..
cmake --build . --config Release --target INSTALL

我的编译和安装没有问题,但是在 Target-release.cmake 安装看起来像这样:

#----------------------------------------------------------------
# Generated CMake target import file for configuration "Release".
#----------------------------------------------------------------

# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)

# Import target "containers" for configuration "Release"
set_property(TARGET containers APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(containers PROPERTIES
  IMPORTED_IMPLIB_RELEASE "C:/Program Files (x86)/containers/lib/containers.lib"
  IMPORTED_LOCATION_RELEASE "C:/Program Files (x86)/containers/bin/containers.dll"
  )

list(APPEND _IMPORT_CHECK_TARGETS containers )
list(APPEND _IMPORT_CHECK_FILES_FOR_containers "C:/Program Files (x86)/containers/lib/containers.lib" "C:/Program Files (x86)/containers/bin/containers.dll" )

# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)

据我所知,没有任何异常。C:/Program Files (x86)/containers/lib/containers.lib我的问题是:我在编译和安装过程中没有生成文件。所以每次我尝试使用 find 包时,都会出错。(它只适用于 Visual Studio 生成器,它适用于 mingw)。这是我的 CMakeLists.txt :

cmake_minimum_required(VERSION 3.1)

set(VERSION 1.0.0)

project (containers
  VERSION ${VERSION}
  DESCRIPTION "Library adding some features to containers of the stl."
  LANGUAGES CXX)

option(BUILD_TESTING "Build test programs" OFF)
include(CTest)

# Set the possible values of build type for cmake-gui
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build.")
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")

message( STATUS "Sources  path : ${PROJECT_SOURCE_DIR}")
message( STATUS "Binary   path : ${PROJECT_BINARY_DIR}")
message( STATUS "install  path : ${CMAKE_INSTALL_PREFIX}")
message( STATUS "Version       : ${PROJECT_VERSION}")
message( STATUS "Version       : ${PROJECT_VERSION}")
message( STATUS "Compiler      : ${CMAKE_CXX_COMPILER_ID}")

set(SOURCES ${PROJECT_SOURCE_DIR}/src/containers.cpp)
set(HEADERS ${PROJECT_SOURCE_DIR}/include/containers/containers_check.h
  ${PROJECT_SOURCE_DIR}/include/containers/containers.h
  ${PROJECT_SOURCE_DIR}/include/containers/append.h
  ${PROJECT_SOURCE_DIR}/include/containers/contains.h
  ${PROJECT_SOURCE_DIR}/include/containers/remove.h
  ${PROJECT_SOURCE_DIR}/include/containers/print.h
  )

add_library(containers SHARED ${SOURCES} ${HEADERS})
#add_library(containers INTERFACE)

target_compile_features(containers PRIVATE cxx_std_17)
set_target_properties(containers
  PROPERTIES
  MSVC_RUNTIME_LIBRARY "MultiThreadedDLL"
  CXX_STANDARD_REQUIRED YES
  CXX_EXTENSIONS OFF)

target_include_directories(containers
  PUBLIC
  $<INSTALL_INTERFACE:include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)

target_compile_options(containers PRIVATE
  # g++
  #$<$<CXX_COMPILER_ID:GNU>:$<BUILD_INTERFACE:-Wall>>
  #$<$<CXX_COMPILER_ID:GNU>:$<BUILD_INTERFACE:-Wextra>>
  #$<$<CXX_COMPILER_ID:GNU>:$<BUILD_INTERFACE:-pedantic>>
  #$<$<CXX_COMPILER_ID:GNU>:$<BUILD_INTERFACE:-Werror>>
  #$<$<CXX_COMPILER_ID:GNU>:-Wno-reorder>
  ## Clang
  #$<$<CXX_COMPILER_ID:Clang>:$<BUILD_INTERFACE:-Wall>>
  ##TODO
  ## MSVC
  #$<$<CXX_COMPILER_ID:MSVC>:$<BUILD_INTERFACE:/W4>>
  #$<$<CXX_COMPILER_ID:MSVC>:$<BUILD_INTERFACE:/WX>>
  )

install(TARGETS containers
  EXPORT containersTarget
  ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/ COMPONENT Development
  LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/ COMPONENT Library NAMELINK_COMPONENT Development
  RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/ COMPONENT Runtimes
  )

install(FILES ${HEADERS}
  DESTINATION ${CMAKE_INSTALL_PREFIX}/include/containers/
  COMPONENT headers)


include(CMakePackageConfigHelpers)

# For moteur_de_calculConfig.cmake
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include CACHE PATH "install path for include files")
set(LIBRARY_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib CACHE PATH "install path for libraries")

#------------------------------------------------------------------------------
# Configure <export_config_name>ConfigVersion.cmake common to build and install tree
set(config_version_file ${PROJECT_BINARY_DIR}/containersConfigVersion.cmake)
write_basic_package_version_file(
  ${config_version_file}
  VERSION "${CMAKE_PROJECT_VERSION}"
  COMPATIBILITY ExactVersion
  )

#------------------------------------------------------------------------------
# Export '<export_config_name>Target.cmake' for a build tree
export(TARGETS
  containers
  FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Target.cmake"
  )

#------------------------------------------------------------------------------
# Configure '<export_config_name>Config.cmake' for a build tree
set(build_config ${CMAKE_BINARY_DIR}/containersConfig.cmake)
configure_package_config_file(
  "containersConfig.cmake.in"
  ${build_config}
  INSTALL_DESTINATION "${PROJECT_BINARY_DIR}"
  PATH_VARS INCLUDE_INSTALL_DIR LIBRARY_INSTALL_DIR VERSION
  )

#------------------------------------------------------------------------------
# Export '<export_config_name>Target.cmake' for an install tree
install(EXPORT
  containersTarget
  DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/${PROJECT_NAME}"
  )

#------------------------------------------------------------------------------
# Configure '<export_config_name>Config.cmake' for a install tree
set(install_config ${PROJECT_BINARY_DIR}/CMakeFiles/containersConfig.cmake)
configure_package_config_file(
  containersConfig.cmake.in
  ${install_config}
  INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/${PROJECT_NAME}"
  PATH_VARS INCLUDE_INSTALL_DIR LIBRARY_INSTALL_DIR VERSION
  )

# Install config files
install(
  FILES ${config_version_file} ${install_config}
  DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/${PROJECT_NAME}"
  )

# test
if(BUILD_TESTING)
  add_subdirectory(test)
endif()

# uninstall target
# use : make uninstall
if(NOT TARGET uninstall)
  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)

  add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

我的 cmake 版本:

cmake version 3.18.0

我的 CMakeLists.txt 中缺少什么来生成 container.lib 文件?

标签: windowsvisual-studiocmake

解决方案


推荐阅读