38

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?

David Holm
  • 17,522
  • 8
  • 47
  • 47
  • Have you considered simply adding your project to the MacPorts repo? That way all the correct flags are passed in from MacPorts. – Jeremy L Oct 14 '09 at 21:57

5 Answers5

36

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Christopher Bruns
  • 9,160
  • 7
  • 46
  • 61
  • 4
    For a slightly nicer syntax: list(APPEND CMAKE_LIBRARY_PATH /opt/local/lib) – David C. Bishop Dec 19 '12 at 05:41
  • You may want to also document `NO_DEFAULT_PATH` since one may want to exclusively use lib from the macports location but not search for the regular ones. – malat Feb 27 '15 at 09:56
  • 4
    This answer wasted a lot of my time for me - cmake seem to happily ignore this variable (or maybe it's used for completely different purposes). Just wanted to warn people, as the actual (correct) answer is the one with (confusingly) less upvotes: use `link_directories`. – Marco Massenzio Aug 12 '15 at 00:11
16

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)
David Holm
  • 17,522
  • 8
  • 47
  • 47
  • 4
    Do NOT hardcode /opt/local. This can be any directory the user installs MacPorts to. – Jeremy L Oct 14 '09 at 21:56
  • normally not a good idea. Much better to let your PATH do the finds of the libraries that you need to include and add the library names specifically. Then each UNIX platform is different. – Dave Feb 06 '10 at 01:25
  • 1
    I agree but this isn't the optimal solution anyway but it was the only working solution I could come up with. – David Holm Nov 10 '10 at 08:07
  • 1
    @Nerdling, Can you can provide a dynamic way to retrieve the appropriate MacPorts prefix without hard-coding it? – tresf Aug 16 '15 at 17:03
6

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.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • 1
    LD_LIBRARY_PATH ought only be used for debugging and testing. Notably, if your executable is setuid or setgid, it is ignored. Build information needs to be with the other build information in the project files. – Hack Saw Oct 09 '14 at 23:17
3

Per @Nerdling's "Do NOT hardcode" comment on the accepted solution, here's a proposal to detect the MacPorts prefix path.

MyModule.cmake

# Detect if the "port" command is valid on this system; if so, return full path
EXECUTE_PROCESS(COMMAND which port RESULT_VARIABLE DETECT_MACPORTS OUTPUT_VARIABLE MACPORTS_PREFIX ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)

IF (${DETECT_MACPORTS} EQUAL 0)
    # "/opt/local/bin/port" doesn't have libs, so we get the parent directory
    GET_FILENAME_COMPONENT(MACPORTS_PREFIX ${MACPORTS_PREFIX} DIRECTORY)

    # "/opt/local/bin" doesn't have libs, so we get the parent directory
    GET_FILENAME_COMPONENT(MACPORTS_PREFIX ${MACPORTS_PREFIX} DIRECTORY)

    # "/opt/local" is where MacPorts lives, add `/lib` suffix and link
    LINK_DIRECTORIES(${LINK DIRECTORIES} ${MACPORTS_PREFIX}/lib)

    MESSAGE("WINNING!: ${MACPORTS_PREFIX}/lib")
ENDIF()

# Recommendation, also add a "brew --prefix" custom command to detect a homebrew build environment
tresf
  • 7,103
  • 6
  • 40
  • 101
1

Install cmake and pkgconfig with MacPorts.

port install cmake pkgconfig

CMake build files that use pkgconfig to find libs will then use the pkgconfig installed by MacPorts, and it will of course have correct search paths for libraries installed by MacPorts.

That assumes CMake build files use the FindPkgConfig module. For example, I have a FindLibuv.cmake module in a project, which begins like this.

find_package (PkgConfig)
pkg_check_modules (PC_Libuv QUIET libuv)
user7610
  • 25,267
  • 15
  • 124
  • 150