23

I am porting some code over to windows and my cmake checks for the package Libavahi using

find_package(Libavahi)

I have the headers, dll, etc. but I'm not sure where to place these such that cmake will find them.

Where can I put these files to be found by cmake? They're in a folder called usr.

I see that the module path is specified using:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

but I'm wondering if there is a default location that will be searched as well

JuJoDi
  • 14,627
  • 23
  • 80
  • 126
  • You may want to check answer to ["find libavahi with cmake"](http://stackoverflow.com/a/18630794/2419207), which contains links to relevant parts of cmake documentation. – iljau Jan 23 '14 at 20:06

2 Answers2

29

The CMake manual fully specifies the rather complicated search order for the different find_* commands. Unfortunately, since Windows lacks a default directory structure à la /usr/local/lib, it is hard to come up with reasonable defaults here.

One of the most reliable ways of managing directories is through environment variable hints. You simply add an $ENV{MY_VAR} to the HINTS section of the find command and then document that environment variable in your project's readme. Most users that are capable of compiling a C++ program know how to use environment variables, and it is way more convenient than having to give the path on the command line every time (although it never hurts to leave that as an additional option).

For find_package CMake offers a special mechanism on Windows called the package registry. CMake maintains a list of package information in the Windows registry under HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\. Packages build from source can register there using the export command. Other projects build later on the same machine will then be able to find that package without additional configuration. This is quite powerful if you need to build a lot of interdependent projects from source on the same machine.

Update: Starting with version 3.12, CMake now implicitly considers the <PackageName>_Root environment variable a HINT for every find_package call.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • +1 for the $ENV{VAR} tip. It is exactly what I use in my projects and works very well (even on Unix). – André Jan 24 '14 at 09:56
  • I've specified the files' directory with the HINTS param. On Linux the NAMES param for libavahi was `libavahi-client.so`. I tried `libavahi-client.dll.a`, located (using HINTS) in `C:/Documents\ and\ Settings/IEUser/My\ Documents/Downloads/avahi-client/usr/lib/` but still no luck. Any recommendation? – JuJoDi Jan 24 '14 at 14:07
  • export approach is better that that global C:\Program Files (x86)\CMake\share\cmake-3.3\Modules directory! – Oliver Zendel Jan 17 '17 at 21:06
  • 12
    In case anyone is a cmake syntax tard like me... Here is an example line(Mind that OpenCV is my package, and I have created OPENCV_DIR in environment variables and it points to the open cv build directory on my system (from source)) : find_package( OpenCV REQUIRED HINTS $ENV{OPENCV_DIR}) – Sneaky Polar Bear Jul 14 '18 at 20:05
2

In the newer versions of cmake, you can use the --debug-find option to list the directories that cmake is searching through. Somethin like:

cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TOOLS=ON --debug-find .
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78