How to create a shared library with cmake?

前端 未结 4 951
死守一世寂寞
死守一世寂寞 2020-11-28 17:10

I have written a library that I used to compile using a self-written Makefile, but now I want to switch to cmake. The tree looks like this (I removed all the irrelevant file

4条回答
  •  悲哀的现实
    2020-11-28 17:45

    I'm trying to learn how to do this myself, and it seems you can install the library like this:

    cmake_minimum_required(VERSION 2.4.0)
    
    project(mycustomlib)
    
    # Find source files
    file(GLOB SOURCES src/*.cpp)
    
    # Include header files
    include_directories(include)
    
    # Create shared library
    add_library(${PROJECT_NAME} SHARED ${SOURCES})
    
    # Install library
    install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
    
    # Install library headers
    file(GLOB HEADERS include/*.h)
    install(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME})
    

提交回复
热议问题