【问题标题】:Build error LNK1104 for CMake project using Irrlicht使用 Irrlicht 为 CMake 项目构建错误 LNK1104
【发布时间】:2020-05-22 13:59:59
【问题描述】:

我目前正在使用 Visual Studio 2019 开展一个学校项目,但我们遇到了一个问题:

LINK : fatal error LNK1104: cannot open file 'Irrlicht.lib'

该错误仅在我们已经编译 CMake 文件时出现,它是第一次工作。

我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(IndieCMAKE)

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_STANDARD 11)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

find_package(Irrlicht)

link_libraries(Irrlicht)

INCLUDE_DIRECTORIES(
        "/usr/include/irrlicht"
        "./include"
${PROJECT_SOURCE_DIR}/include
)
include_directories(inc)

add_executable(IndieCMAKE
 ./main.cpp)

这是我的FindIrrlicht.cmake 文件:

    IF (NOT Irrlicht_INCLUDE_DIRS OR NOT Irrlicht_LIBRARIES)
  FIND_PATH(Irrlicht_INCLUDE_DIRS
    NAMES
      irrlicht.h
    PATHS
      /usr/include/irrlicht/        # Default Fedora28 system include path
      /usr/local/include/irrlicht/  # Default Fedora28 local include path
      ${CMAKE_MODULE_PATH}/include/ # Expected to contain the path to this file for Windows10
      ${Irrlicht_DIR}/include/      # Irrlicht root directory (if provided)
  )

  IF (MSVC)     # Windows
    SET(CMAKE_FIND_LIBRARY_PREFIXES "")
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
  ELSE (MSVC)   # Linux
    SET(CMAKE_FIND_LIBRARY_PREFIXES "lib")
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
  ENDIF(MSVC)

  FIND_LIBRARY(Irrlicht_LIBRARIES
    NAMES
      Irrlicht
    PATHS
      /usr/lib64             # Default Fedora28 library path
      /usr/lib/                     # Some more Linux library path
      /usr/lib/x86_64-linux-gnu/    # Some more Linux library path
      /usr/local/lib/               # Some more Linux library path
      /usr/local/lib64/             # Some more Linux library path
      ${CMAKE_MODULE_PATH}/         # Expected to contain the path to this file for Windows10
      ${Irrlicht_DIR}/              # Irrlicht root directory (if provided)
  )
ENDIF (NOT Irrlicht_INCLUDE_DIRS OR NOT Irrlicht_LIBRARIES)

IF (Irrlicht_INCLUDE_DIRS AND Irrlicht_LIBRARIES)
  SET(Irrlicht_FOUND TRUE)
ELSE (Irrlicht_INCLUDE_DIRS AND Irrlicht_LIBRARIES)
  SET(Irrlicht_FOUND FALSE)
ENDIF (Irrlicht_INCLUDE_DIRS AND Irrlicht_LIBRARIES)

IF (Irrlicht_FIND_REQUIRED AND NOT Irrlicht_FOUND)
  MESSAGE(FATAL_ERROR
    "  Irrlicht not found.\n"
    "      Windows: Fill CMake variable CMAKE_MODULE_PATH to the provided directory.\n"
    "      Linux: Install Irrlicht using your package manager ($> sudo dnf install irrlicht-devel).\n"
  )
ENDIF (Irrlicht_FIND_REQUIRED AND NOT Irrlicht_FOUND)

这是我的 CMake GUI 变量:

【问题讨论】:

  • 欢迎来到 Stack Overflow!请参阅对this 问题的回复,了解如何正确地将库链接 到您的可执行文件。您的代码看起来与this 问题中发布的代码非常相似。请注意,您应该修改路径以匹配 您的 系统,而不是使用可能与 Windows 无关的 Unix 样式路径...
  • 请在您的问题帖子中提供 CMake 输出。 CMake 是否甚至成功找到了 Irrlicht(通过 find_package() 调用)?看来您可能需要按照thread 中的建议使用您自己的自定义 FindIrrlicht.cmake 文件。
  • 我有自己的 FindIrrlicht.cmake 可以第一次找到我的库
  • 我提供 cmake 输出

标签: c++ visual-studio cmake irrlicht


【解决方案1】:

如果您使用 CMake Find Module,例如 FindIrrlicht.cmake 文件,find_package(Irrlicht) 命令应该为您填充一些 Irrlicht_* CMake 变量。假设 find_package() 确实正确找到了包组件,您可以在 CMake 文件中使用这些 Irrlicht_* 变量来指定 CMake 应在何处查找 Irrlicht 包头和库。

类似下面的 CMake 文件应该可以工作:

cmake_minimum_required(VERSION 3.9)
project(IndieCMAKE)

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_STANDARD 11)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

# Use REQUIRED here, to ensure CMake finds the package before continuing.
find_package(Irrlicht REQUIRED)

# Don't use this command. It's old, and the syntax didn't work, in your case.
#link_libraries(Irrlicht)

# Specify your directories that contain headers, including the Irrlicht headers.
include_directories(
    ${Irrlicht_INCLUDE_DIRS}
)

# Define your executable target.
add_executable(IndieCMAKE
    main.cpp
)

# Link the Irrlicht library to your executable target.
target_link_libraries(IndieCMAKE PRIVATE ${Irrlicht_LIBRARIES})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多