Search code examples
linuxcmakestdiobsd

how to link <bsd/stdio.h> in cmake to use funopen()


I am using Ubuntu, and I want to use funopen(), but this function is in BSD system only. So I install libbsd

sudo apt-get install libbsd-dev

after that, I can include and I see the funopen() in this header file. But cannot compile. I try use this in CMakeLists.txt, but no use:

find_package(bsd)

I think the param is not bsd, how can I do it?

[UPDATED] do as @Alu suggestion, still not work:

cmake_minimum_required(VERSION 3.3)
project(myProject)

set(SOURCE_FILES main.c)
set(DIR_TO_LIB_HEADERS, /usr/include/bsd)
set(DIR_TO_BSD_LIB_FILE, /usr/lib/x86_64-linux-gnu/libbsd.so)
include_directories(${DIR_TO_LIB_HEADERS})
add_executable(myProject ${SOURCE_FILES})
target_link_libraries(myProject ${DIR_TO_BSD_LIB_FILE})

with error:

undefined reference to `funopen'

Solution

  • 32 bit OS:

    set(DIR_TO_BSD_LIB_FILE /usr/lib/i386-linux-gnu/libbsd.so)
    target_link_libraries(${PROJECT_NAME} ${DIR_TO_BSD_LIB_FILE})
    

    64 bit OS:

    set(DIR_TO_BSD_LIB_FILE /usr/lib/x86_64-linux-gnu/libbsd.so)
    target_link_libraries(${PROJECT_NAME} ${DIR_TO_BSD_LIB_FILE})