Skip to content

Instantly share code, notes, and snippets.

@erikyuzwa
Created June 19, 2026 21:43
Show Gist options
  • Select an option

  • Save erikyuzwa/0ee39f36152eb1689ac6f9d5721a9d28 to your computer and use it in GitHub Desktop.

Select an option

Save erikyuzwa/0ee39f36152eb1689ac6f9d5721a9d28 to your computer and use it in GitHub Desktop.
CMakeLists Configuration for LIBCS50
cmake_minimum_required(VERSION 4.1)
project(hello_cs50_library C)
set(CMAKE_C_STANDARD 11)
set(MY_PROJECT hello_cs50_library)
# Include the command that downloads libraries
include(FetchContent)
# Grab libcs50 from GitHub
FetchContent_Declare(
libcs50
GIT_REPOSITORY https://github.com/cs50/libcs50.git
GIT_TAG main
)
# Download + add to build
FetchContent_MakeAvailable(libcs50)
add_executable(MY_PROJECT main.c)
# ---- Link + includes ----
# libcs50's repo doesn't provide a universal CMake "target" in all historical versions,
# so we handle it robustly:
#
# 1) If the repo defines a target named "cs50", use it.
# 2) Otherwise, build cs50 ourselves from the fetched sources.
if(TARGET cs50)
target_link_libraries(MY_PROJECT PRIVATE cs50)
elseif(TARGET libcs50)
target_link_libraries(MY_PROJECT PRIVATE libcs50)
else()
# Fallback: build the library from source in the fetched directory
FetchContent_GetProperties(libcs50)
if(NOT libcs50_POPULATED)
message(FATAL_ERROR "libcs50 was not populated by FetchContent.")
endif()
add_library(cs50 STATIC
${libcs50_SOURCE_DIR}/src/cs50.c
)
target_include_directories(cs50 PUBLIC
${libcs50_SOURCE_DIR}/src
)
target_link_libraries(MY_PROJECT PRIVATE cs50)
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment