0

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.

GGinside
  • 63
  • 6

1 Answers1

0
  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 (e.g. 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 -- i.e. 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!

zaufi
  • 6,811
  • 26
  • 34
  • Hi Zaufi, thanks for helping out. 1. I know it might be bad practice to use GLOB, but i am distributing library, so no one gets to really build the project by them, they will be seeing my built binary and the header(interface) i provide them. This is my workflow, hopefully it gives a better idea of what confuses me. Also, I am asking whether to include headers in add_library, as you can see, i currently only searched for .cpp files, there are headers that are included in those .cpp but not explicitly included when building the lib. 2. I want others to just download my binary and my header. – GGinside Aug 16 '20 at 00:48
  • so far your answer sounds to me is using install is beneficial if i want to use cmake to distribute my library, which i am not going to. Or is there other benefits other than being able to use find_package()? – GGinside Aug 16 '20 at 00:50
  • Libraries can just expose the header files to me, and I could put their sources anywhere in my computer. When i write my makefile, i just need to specify my lib location. So cmake confuses by using install. – GGinside Aug 16 '20 at 00:52
  • You can use CPack to build a _distribution packge_ of your library and for that, you ought to use `install()`. Anyway, having `install()` is good even if you build your package _manually_ -- after build you can pre-install whatever you need into an _image directory_ and pack it some way or another (that is CPack's job in fact). – zaufi Aug 16 '20 at 08:45
  • How do you produce your distribution package? – zaufi Aug 16 '20 at 08:47
  • Hi, I am not distributing my package with cmake... user would download my binary and the header file i provided them, and they could put those anywhere they want.. – GGinside Aug 17 '20 at 05:55
  • Really? User ought to download more than one file? Am I understand you correctly? – zaufi Aug 17 '20 at 19:24
  • when i download json headers, i download them. Many libs work that way. Or is there a misunderstanding? I never use cmake to search for libs, i usually download them, install them to place i want them to be, including CUDA. My lib is just one header with one binary – GGinside Aug 17 '20 at 22:03