How to Tell Cmake I Want My Project to Link Libraries Statically

How do I tell cmake I want my project to link libraries statically?

You build static OpenCV libraries by just setting the BUILD_SHARED_LIBS flag to false in CMake. Then all you need to do to build your own application with those static libraries is to add a dependency on OpenCV in your CMakeLists.txt:

FIND_PACKAGE (OpenCV REQUIRED)
...
TARGET_LINK_LIBRARIES (your-application ${OpenCV_LIBS})

and CMake will take care of everything.

How do I tell CMake to link in a static library in the source directory?

CMake favours passing the full path to link libraries, so assuming libbingitup.a is in ${CMAKE_SOURCE_DIR}, doing the following should succeed:

add_executable(main main.cpp)
target_link_libraries(main ${CMAKE_SOURCE_DIR}/libbingitup.a)

How to link static libraries in cmake

Here is some github example.

Here is some SO question you are duplicating.

Based on that this should look like this:

cmake_minimum_required(VERSION 3.15)
project(testo)

set(CMAKE_CXX_STANDARD 17)

find_package(GLEW REQUIRED)

add_executable(testo main.cpp)
target_link_libraries(testo PUBLIC ${GLEW_LIBRARIES})
target_include_directories(testo PUBLIC ${GLEW_INCLUDE_DIRS})

How to add prebuilt static library in project using CMake?

You're probably asking about how to link your project to the pre-built static library. If so, you can do like this by calling target_link_libraries.

Assume your project called myProj and the pre-built library myLib.lib, you can do like this:

target_link_libraries(myProj myLib)


Related Topics



Leave a reply



Submit