2

My problem: I have a library project which I want to distribute in binary form. I want my clients to be able to integrate this library with their projects as comfortably as possible. Also, I would prefer if they did not have to deal with a complex installation process. I assume them to be able to use CMake at a beginners level (which means adding header and source files, find_package() and add_executable()).

In best case, I'd imagine the workflow with my package to look like this:

  • Download the package with headers and binaries only. No source, no CMake, make, make install!
  • Extract the package.
  • Move the content to special directory where other packages are. (This directory can be in environment variable or anything like that.)
  • find_package() works.

It should be like this in both Windows 7 and newest Ubuntu.

For me, I've been learning CMake for the last 2 days and I find the documentation extremely hard-to-understand (I never had such trouble with learning things like Qt, qtcreator, sdl, pulse audio, git, OpenGL, ...) and that's why I'm asking.

Resolution:

Use packaging system of cmake.

Notes:

  • Of course I found this solution before asking, but I made an error (used *Targets.cmake file for build tree in install tree) and thought it's not usable for my purposes.
  • I used the package system and it wasn't working well, see related problems.

Related problems:

EDIT:

  • Changed "find_library" references to "find_package" as I mistook these two during writing.
  • Added resolution and related problems sections.
Community
  • 1
  • 1

1 Answers1

3

Take a look at CMake packages.

This will allow pretty much exactly the user workflow that you described. The idea here is that when you build your project, CMake will write a configuration file for you to ship with the binaries. Your users can then load that config file in their CMake project (using find_package) and will be able to link to your binaries with a minimum of effort (usually a simple target_link_libraries call does the job).

This approach is to be preferred over the classic find_library approach, which potentially requires a lot more steps on the user's side to get things up and running.

As for your troubles regarding learning CMake: I feel your pain. CMake is not particularly easy to learn by nature. On top of that a lot of the documentation available on the web is severely outdated. I'd recommend starting with the updated CMake reference manual which has been restructured for the upcoming CMake 3.0 release. It's still a lot of content to dig through, so don't expect perfect results too quickly, but it's a good place to start.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • First, I have to apologize, that I used "find_library" while I meant "find_package" you're talking about. –  May 14 '14 at 12:16
  • (Sorry, I sent reply before finishing...) Then, thank you very much for correction of my post - obviously, I'm not native English speaker and made more errors than I should... So, now to your answer: thank you, I'll look into it - even thought I was Googling for hours, I didn't find the updated manual. Generally, I was trying to do exactly what you described except it requires the installation step including writing to registry. So I'll look to your link and see if that helps. –  May 14 '14 at 12:19
  • @Laethnes Good luck with the manual. Just keep in mind that this manual is already for CMake 3.0 which is not yet released. Most of this stuff (including the packaging) already works with CMake 2.8.12, but if you run into issues, be sure to double-check with [the old manual](http://www.cmake.org/cmake/help/v2.8.12/cmake.html) whether the feature you are trying to use is already in there. – ComicSansMS May 14 '14 at 12:35
  • Thanks and thanks for warning - I already found one problem with export(EXPORT ...) being new feature. So, with the manual I was able to get some results, but problem is, I cannot link a library (if you're interested, testing projects is in https://dl.dropboxusercontent.com/u/41237813/ruzne/CMakeTest.zip ) - I created XyzLib project which should be exported and Index (name is totally random) as executable using it. The error is "No rule to make target `xyz-NOTFOUND', needed by `Index.exe'. Stop." with no more information. I guess the library file is not found - how should I setup... –  May 14 '14 at 14:23
  • ...setup the library project so the XyzLibTargets.cmake contains the necessary information? I guess I could configure link_directories() in XyzLibConfig.cmake (as I already do with includes), but in that case using ..Targets.cmake would be completely pointless, wouldn't be... By the way, how I test it: XyzLib: cmake-gui, mingw32-make, cpack -C CPackConfig.cmake -G ZIP, extract generated file to directory which is in prefix paths, then on Index: cmake-gui, mingw32-make -> error. –  May 14 '14 at 14:33
  • (Btw. marking your answer as accepted as it's the way to solve my original problem assuming this new problem is solvable.) –  May 14 '14 at 14:35
  • @Laethnes Your setup seems fine at a hasty first look. Make sure that the `find_package` call in your Index project actually knows where to find the config file (`CMAKE_MODULE_PATH`). Also, double-check the generated package config files with a text editor, to ensure their contents are correct. If you cannot figure it out on your own, try narrowing down your setup to isolate the specific problem and posting a new question. Unfortunately, package setups are quite complex by nature, so this is often easier said than done. – ComicSansMS May 14 '14 at 14:45
  • Ok, thanks. Btw., I'm pretty sure find_package finds it - I put message(...) to config file and it was displayed, but I didn't put the path to CMAKE_MODULE_PATH (I just did, but nothing changes) but to CMAKE_PREFIX_PATH as I read somewhere that the former is ignored as environment variable. About the generated package file (*Targets.cmake) - yes, I did it and I didn't find location of the library, problem is I have no idea how the file should look like and what should I do to add missing data. Anyway, ok, I'll try to do that (again, in fact) and post the new question if needed. –  May 14 '14 at 14:53
  • @BjarkeFreund-Hansen I apologize for that. I gave up on CMake completely and did some cleaning. I still have it on my PC in archive, however I didn't backup that particular version so only thing I could provide is old, abandoned unclean work-in-progress in random state (probably full of garbage, maybe not even working). Are you interested in that instead? And just out of curiosity - why are you interested in that in general? I never had something with satisfactory results... –  Aug 23 '16 at 17:47