如何指示CMake使用外部预建库

时间:2018-03-23 16:00:15

标签: cmake

我正在努力寻找一种方法来告诉CMake包含外部预建库......这让我疯了。

我不熟悉我是CMake的新手。我只是希望MinGW链接器将两个外部.lib文件添加到链接列表中......此时我正在拉我的头发。

这是我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.9)
project(opengltest C CXX)

set(CMAKE_CXX_STANDARD 11)

file(GLOB
        TestSRC
        "src/*.h"
        "src/*.cpp"
        "src/*.c"
        )

add_executable(opengltest ${TestSRC})

include_directories(
        3rdparty/glew/include
        3rdparty/glfw/include
)

link_directories(
        ${PROJECT_SOURCE_DIR}/3rdparty/glew/lib/Release/x64/
        ${PROJECT_SOURCE_DIR}/3rdparty/glfw/build/src/Debug/
)

target_link_libraries(${PROJECT_NAME} glew32s glfw3)

Linker说找不到glew32s和glfw3。

编辑:我想我找到了解决方案:

...
add_library(glew32s STATIC IMPORTED)
set_property(TARGET glew32s PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glew32s.lib)
add_library(glfw3 STATIC IMPORTED)
set_property(TARGET glfw3 PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glfw3.lib)

target_link_libraries(${PROJECT_NAME} glew32s glfw3)

1 个答案:

答案 0 :(得分:1)

我已将所有.lib文件移至一个文件夹,并使用add_library将其包含在内。

...
add_library(glew32s STATIC IMPORTED)
set_property(TARGET glew32s PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glew32s.lib)
add_library(glfw3 STATIC IMPORTED)
set_property(TARGET glfw3 PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glfw3.lib)

target_link_libraries(${PROJECT_NAME} glew32s glfw3)

之后,链接器能够找到lib文件。