Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I instruct CMake to look for libraries installed by MacPorts?

I'm trying to build some of our software, which was designed to run solely on Linux, on MacOS X. We are using CMake and I installed MacPorts so I could easily get CMake along with some of the third party libraries that we depend on.

Now the problem is that CMake doesn't appear to look for libraries from MacPorts by default so several of our targets are disabled as it fails to find the dependencies which are all in /opt/local.

How can I instruct CMake to also look for includes and libraries from MacPorts?

like image 869
David Holm Avatar asked Sep 28 '09 15:09

David Holm


3 Answers

Add /opt/local/lib, and any other likely install paths, to the set of paths searched by cmake in your CMakeLists.txt file:

set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /opt/local/lib) 

This appends /opt/local/lib to the set of paths in which cmake searches for libraries. This CMAKE_LIBRARY_PATH technique will affect all find_library commands after you set the variable.

For a more surgical, library-by-library approach, modify the individual find_library commands:

find_library(Foo foo     PATHS /opt/local/lib) 

Note that this does not hardcode /opt/local/lib as the only place to look for the library. Rather, it merely appends /opt/local/lib to the set of locations in which to search for the library. I often end up adding many such paths, covering the locations observed on all of the machines I know about. See the find_library documentation for more variations on this theme.

You might also wish to change CMAKE_INCLUDE_PATH, which affects the behavior of find_file() and find_path() commands.

like image 53
Christopher Bruns Avatar answered Sep 22 '22 09:09

Christopher Bruns


I added a toolchain file for "Darwin" which adds the necessary include and library paths. I was hoping for something a little more automatic but at least it solves the problem.

darwin.cmake:

SET(CMAKE_SYSTEM_NAME Darwin)  # Add MacPorts INCLUDE_DIRECTORIES(/opt/local/include) LINK_DIRECTORIES(/opt/local/lib) 
like image 44
David Holm Avatar answered Sep 26 '22 09:09

David Holm


CMake needs to respect the DYLD_LIBRARY_PATH environment variable, which is the equivalent of the LD_LIBRARY_PATH environment variable on Linux. Your DYLD_LIBRARY_PATH needs to have the proper path to find libraries installed by MacPorts.

like image 45
Hank Gay Avatar answered Sep 23 '22 09:09

Hank Gay