简体   繁体   中英

clarify on CMAKE library installation

The odd is that I can understand CMAKE documents, but I still can not figure out how to use it in a little more complicated scenario.

I want to install a SHARED LIB, to let someone else use it. I know I can install it with CMAKE install command, but my first question is that my code still works without installing the library. The library is built and put under cmake_build_debug dir.

All I did is:

FILE(GLOB SHAREAD_SRC
        ${CMAKE_CURRENT_SOURCE_DIR}/Interface/*.cpp
        )
set(MY_LIB mylib)
add_library(${MY_LIB} SHARED ${SHAREAD_SRC})
add_executable(run_src src/my_src.cpp ${HEADERS})
target_link_libraries(run_src ${MY_LIB})

I can now include the library's header in my source code and start to use it.

My question is,

  1. in add library command, should I also include my library's header files? Currently i only include the source files to build the library, since whenever I use the library, I know where physically the library headers are(since i made this library), and for those others who also want to use this lib, i would provide them the header and the built lib target, so whereever they want to put them, no problem.

  2. some answers talk about the install command saying that without the header files included in add_library, Why add header files into ADD_LIBRARY/ADD_EXECUTABLE command in CMake , otherwise you won't see headers in IDE-generated project. My headers are in this project, so I don't understand this issue. Why do I need to install this library? What is the purpose of install if the user downloaded my header file and have the built binary?

Thanks for helping. Appreciation in advance.

  1. Except for the mentioned reason that specified files gonna be "visible" in IDE there is one more: explicit better than implicit (yeah Pythonish statement) -- meaning that you give a hint to the reader of your CMakeLists.txt of what exact files your library consists of . And yes, using GLOB for sources is the known bad practice for many reasons -- IRL it's not really hard to maintain the list of sources explicitly and makes your build system less error-prone. In some circumstances, you can gain some more benefits of having headers mentioned explicitly (eg using install(TARGET... PUBLIC_HEADERS...) but this is the other subject:)

  2. You didn't specify your workflow (how do you build and distribute your project/library). However, the primary goal of install is to form the built image of your project -- ie what targets/files/directories gonna be installed and into what directory layout. It's needed to build other projects dependent on yours or produce packages (w/ CPack) to distribute or deploy 'em somewhere.

Also, to let others use your built library in CMake way please read how to make a package section in the manual . So, others can just do find_package(YourProject) and use it (link w/ it via target_link_libraries ) -- easy peasy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM