Search code examples
ccmakeinclude

how to include zstd in cmake?


im trying to include zstd into my project and currently it kind of is included but it's being linked to the main executable at the end of the cmakelist file and thus except for the main file nothing can acces zstd.h. i want to use zstd globally in project folder so that if i make a test file and try to access zstd.h it works. currently im using c. currently my cmakelist looks like this


set(CMAKE_C_STANDARD 23)

set(CMAKE_C_COMPILER gcc)

project(main
    VERSION 1.0.0
    LANGUAGES C)

    
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
    
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/vendor/cwalk)
# link_libraries(cwalk)

# add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/vendor/zstd/build/cmake)
# target_include_directories(libzstd_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/zstd/lib/)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/vendor/zstd/build/cmake zstd)
# target_include_directories(main PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/zstd/lib/)


# Add vendor-datastruct library
file(GLOB DATASTRUCT_SOURCES vendor/datastructs/*.c)
file(GLOB DATASTRUCT_HEADERS vendor/datastructs/*.h)
add_library(datastructs ${DATASTRUCT_SOURCES})
target_sources(datastructs INTERFACE ${DATASTRUCT_HEADERS})
target_include_directories(datastructs PUBLIC ${PROJECT_SOURCE_DIR}/vendor/datastructs)

# Add libraries
file(GLOB LIB_SOURCES lib/*.c)
file(GLOB LIB_HEADERS lib/*.h)
add_library(LIB ${LIB_SOURCES})
target_sources(LIB INTERFACE ${LIB_HEADERS})
target_include_directories(LIB PUBLIC ${PROJECT_SOURCE_DIR}/lib)
target_link_libraries(LIB cwalk datastructs)
link_libraries(LIB)

add_executable(main main.c)
target_include_directories(main PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/zstd/lib/)

if(WIN32)
    add_executable(test2 test/test2.c)
    add_executable(test3 test/test3.c)
    add_executable(test4 vendor/datastructs/tests.c)
elseif(UNIX)
    add_executable(test1 test/test1.c)
endif()

i cannot access zstd in any test files. please help me with zstd or improve on this cmakelist file as im using cmake for first time


Solution

  • this worked for me

    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/vendor/zstd/build/cmake zstd)
    target_include_directories(zstd PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/zstd/lib/)
    target_link_libraries(LIB libzstd_static)