Skip to content
Nathan Moinvaziri edited this page Jan 19, 2024 · 3 revisions

Common Build Problems

How to Statically Link Runtime Library

CMake

macOS/Linux

cmake -S . -B build -D CMAKE_EXE_LINKER_FLAGS=-static -D CMAKE_SHARED_LINKER_FLAGS=-static

MSVC

Use CMake's CMAKE_MSVC_RUNTIME_LIBRARY option:

cmake -S . -B build -D CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded

Configure

export LDFLAGS=-static
./configure

How to Build into Your CMake project

If you are building from zlib sources with CMake:

project(myproject)

add_subdirectory(zlib zlib EXCLUDE_FROM_ALL)
# Use EXCLUDE_FROM_ALL to prevent all targets from being added to the solution

add_executable(myproject myproject.c)
target_link_libraries(myproject PRIVATE zlibstatic) # Or "zlib" depending on whether or not you want the shared/static library

If you are using system libraries:

project(myproject)

find_package(ZLIB REQUIRED)

add_executable(myproject myproject.c)
target_link_libraries(myproject PRIVATE ZLIB::ZLIB)