CMAKE - How to properly copy static library's header file into /usr/include?

前端 未结 4 1466
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 20:26

I\'m getting into CMAKE usage with C and actually I\'m creating two very small static libraries.

My goal is:

  1. The libraries are compiled and linked into
相关标签:
4条回答
  • 2020-12-12 21:00

    I don't think your solution is the correct one. /usr/include should be reserved for your vendor to put files in.

    The proper thing to do IMO is to install the header in /usr/local/include and then instruct the user to export CPATH="/usr/local/include:${CPATH}".

    It seems /usr/local/lib was search automatically but if you wish to use another dir export LIBRARY_PATH="/usr/local/lib:${LIBRARY_PATH}" works similar for the .a binary (but may or may not work good for shared libraries depending on your os).

    Optionally, but more cumbersome is to add -I /usr/local/include and -L /usr/local/lib while compiling.

    This is a somewhat subjective answer, but it's been working well for me.

    0 讨论(0)
  • 2020-12-12 21:04

    In a much better way, will copy all files that match the pattern and will preserve the directory structure.

    INSTALL (
        DIRECTORY ${CMAKE_SOURCE_DIR}/include/
        DESTINATION include
        FILES_MATCHING PATTERN "*.h*")
    
    0 讨论(0)
  • 2020-12-12 21:10

    In addition to the accepted answer, if you are creating a lot of libraries and the set_property syntax throws you off. You could wrap it in a very simple macro, such as:

    # File: target_public_headers.cmake
    macro(target_public_headers TARGET)
      set_target_properties(${TARGET} PROPERTIES PUBLIC_HEADER "${ARGN}")
    endmacro()
    

    Then you can use it like:

    project(myproject)
    
    include(target_public_headers)
    
    add_library(mylib some.c another.c)
    target_public_headers(mylib some.h another.h) # <<<<<
    
    # If you're exporting this library then you need to tell
    # CMake how to include the "installed" version of the headers.
    target_include_directories(mylib
      PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
      PUBLIC $<INSTALL_INTERFACE:some/includepath>
    )
    
    INSTALL(TARGETS mylib 
            LIBRARY DESTINATION some/libpath
            PUBLIC_HEADER DESTINATION some/includepath
    )
    
    0 讨论(0)
  • 2020-12-12 21:19

    A better way for newest cmake version is to use target's PUBLIC_HEADER properties.

    project(myproject)
    
    add_library(mylib some.c another.c)
    set_target_properties(mylib PROPERTIES PUBLIC_HEADER "some.h;another.h")
    INSTALL(TARGETS mylib 
            LIBRARY DESTINATION some/libpath
            PUBLIC_HEADER DESTINATION some/includepath
    )
    

    Some ref:

    PUBLIC_HEADER

    CMake install command

    0 讨论(0)
提交回复
热议问题