【问题标题】:(CMake build) fatal error LNK1181: cannot open input file 'glew.lib'(CMake 构建)致命错误 LNK1181:无法打开输入文件 'glew.lib'
【发布时间】:2019-01-19 06:25:14
【问题描述】:

我正在开发一个游戏引擎,最近尝试创建一个 CMakeLists.txt 来构建项目。我设法创建了一个配置和生成的 cmakelist,但是当我尝试在 Visual Studio 中运行生成 .sln 时,出现以下错误:

fatal error LNK1181: cannot open input file 'glew.lib'

我的游戏引擎使用外部库 glew 和 glfw。这是我的 cmakelist 文件:

cmake_minimum_required(VERSION 3.7)
project(GameEngine)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/Engine/source)

set(ENGINE_SOURCES ${SOURCE_DIR}/core/Engine.cpp
           ${SOURCE_DIR}/graphics/gl_types/IndexBuffer.cpp
           ${SOURCE_DIR}/graphics/gl_types/VertexArray.cpp
           ${SOURCE_DIR}/graphics/gl_types/VertexBuffer.cpp
           ${SOURCE_DIR}/graphics/renderer/Camera.cpp
           ${SOURCE_DIR}/graphics/renderer/Shader.cpp
           ${SOURCE_DIR}/graphics/renderer/Texture.cpp
           ${SOURCE_DIR}/graphics/renderer/Window.cpp
           ${SOURCE_DIR}/vendor/stb_image/stb_image.cpp)

add_library(engine STATIC ${ENGINE_SOURCES})
target_include_directories(engine PUBLIC ${SOURCE_DIR})


set(glfw3_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib/cmake/glfw3)
find_package(glfw3 REQUIRED)

set(glew_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLEW)
find_library(glew glew32s "${glew_DIR}/lib/Release/Win32")

include_directories(${GLFW_INCLUDE_DIRS})
include_directories(${glew_DIR}/include)

target_link_libraries(
        engine
        glfw
        glew
)

set(GAME_DIR ${CMAKE_SOURCE_DIR}/Game/source)
set(GAME_SOURCES ${GAME_DIR}/main.cpp)

add_executable(engine_game ${GAME_SOURCES})

target_link_libraries(engine_game engine)```

【问题讨论】:

    标签: c++ cmake


    【解决方案1】:

    您正在使用设置变量的find_library。你必须这样使用它们:

    target_link_libraries(
            engine
            ${glfw_LIBRARIES}
            ${glew}
    )
    

    我假设glfw_LIBRARIESfind_package(glfw) 设置的变量。

    【讨论】:

    • 使用库名称作为变量,程序不再寻找 .lib 文件,但现在出现链接错误 fatal error LNK1120: 48 unresolved externals
    • 这意味着您缺少另一个库。也许这不是glfw 的正确名称。您必须检查文档才能看到正确的名称。还要检查配置 (CMakeCache.txt) 中的条目是否具有您期望的值。
    • 是的 glfw 没有正确链接,我需要为我的 glew 库定义一个预处理器定义 GLEW_STATIC。现在我的程序可以运行了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    相关资源
    最近更新 更多