StjepanV
StjepanV

Reputation: 167

How to instruct CMake to use external pre-built libraries

I'm struggling how to find a way to say to CMake to include external prebuilt libraries... It's driving me insane.

It's understatement that I'm new to CMake. I just want that MinGW linker add two external .lib files to link list... I'm pulling my hair at this point.

This is my CMakeLists.txt file:

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 says it cannot find glew32s and glfw3.

EDIT: I think I've found the solution:

...
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)

Upvotes: 1

Views: 2258

Answers (1)

StjepanV
StjepanV

Reputation: 167

I've moved all my .lib files to one folder and used add_library to include them.

...
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)

After that linker was able to find lib files.

Upvotes: 2

Related Questions