Skip to content

Instantly share code, notes, and snippets.

@davisford
Created September 26, 2012 17:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davisford/3789287 to your computer and use it in GitHub Desktop.
Save davisford/3789287 to your computer and use it in GitHub Desktop.
Issue with CMake cross-compile and find_library()
# add ZeroMQ library
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
if (${CMAKE_CROSSCOMPILING})
# build with Arm / Pi-compiled libraries
find_library(ZEROMQ_LIB zmq ${PROJECT_SOURCE_DIR}/../lib/linux/pi)
find_library(CZMQ_LIB czmq ${PROJECT_SOURCE_DIR}/../lib/linux/pi)
else()
# build with 32-bit linux libraries
find_library(ZEROMQ_LIB zmq ${PROJECT_SOURCE_DIR}/../lib/linux/x86)
find_library(CZMQ_LIB czmq ${PROJECT_SOURCE_DIR}/../lib/linux/x86)
endif()
else()
# building for Mac OS X
find_library(ZEROMQ_LIB zmq ${PROJECT_SOURCE_DIR}/../lib/mac)
find_library(CZMQ_LIB czmq ${PROJECT_SOURCE_DIR}/../lib/mac)
endif()

Show listing of pre-compiled libs I'm trying to link against.

** listing directory with pre-compiled libs for Raspberry Pi / Arm target **

$ls -al lib/linux/pi/
total 24936
drwxr-xr-x  13 davis  staff      442 Sep 24 17:50 .
drwxr-xr-x   4 davis  staff      136 Sep 24 17:50 ..
-rw-r--r--   1 davis  staff   803866 Sep 24 17:50 libCppUTest.a
-rw-r--r--   1 davis  staff   288552 Sep 24 17:50 libczmq.a
-rwxr-xr-x   1 davis  staff      954 Sep 24 17:50 libczmq.la
-rwxr-xr-x   1 davis  staff   201072 Sep 24 17:50 libczmq.so
-rwxr-xr-x   1 davis  staff   201072 Sep 24 17:50 libczmq.so.1
-rwxr-xr-x   1 davis  staff   201072 Sep 24 17:50 libczmq.so.1.0.0
-rw-r--r--   1 davis  staff  4235478 Sep 24 17:50 libzmq.a
-rwxr-xr-x   1 davis  staff      942 Sep 24 17:50 libzmq.la
-rwxr-xr-x   1 davis  staff  2268130 Sep 24 17:50 libzmq.so
-rwxr-xr-x   1 davis  staff  2268130 Sep 24 17:50 libzmq.so.3
-rwxr-xr-x   1 davis  staff  2268130 Sep 24 17:50 libzmq.so.3.0.0

** listing directory with pre-compiled libs for standard Linux / x86 **

$ ls -al lib/linux/x86/

total 25960
drwxr-xr-x  18 davis  staff      612 Sep 24 17:50 .
drwxr-xr-x   4 davis  staff      136 Sep 24 17:50 ..
-rw-r--r--   1 davis  staff   746964 Sep 24 17:50 libCppUTest.a
-rw-r--r--   1 davis  staff   303330 Sep 24 17:50 libczmq.a
-rwxr-xr-x   1 davis  staff     1000 Sep 24 17:50 libczmq.la
-rwxr-xr-x   1 davis  staff   208665 Sep 24 17:50 libczmq.so
-rwxr-xr-x   1 davis  staff   208665 Sep 24 17:50 libczmq.so.1
-rwxr-xr-x   1 davis  staff   208665 Sep 24 17:50 libczmq.so.1.0.0
-rw-r--r--   1 davis  staff   314414 Sep 24 17:50 libjzmq.a
-rwxr-xr-x   1 davis  staff      996 Sep 24 17:50 libjzmq.la
-rwxr-xr-x   1 davis  staff   188577 Sep 24 17:50 libjzmq.so
-rwxr-xr-x   1 davis  staff   188577 Sep 24 17:50 libjzmq.so.0
-rwxr-xr-x   1 davis  staff   188577 Sep 24 17:50 libjzmq.so.0.0.0
-rw-r--r--   1 davis  staff  4028054 Sep 24 17:50 libzmq.a
-rwxr-xr-x   1 davis  staff      947 Sep 24 17:50 libzmq.la
-rwxr-xr-x   1 davis  staff  2220121 Sep 24 17:50 libzmq.so
-rwxr-xr-x   1 davis  staff  2220121 Sep 24 17:50 libzmq.so.3
-rwxr-xr-x   1 davis  staff  2220121 Sep 24 17:50 libzmq.so.3.0.0

Refer to CMakeLists.txt below that shows how I'm trying to use find_library() to resolve the references to these pre-built libraries inside my own project dir structure.

Also, refer to Toolchain-raspberrypi-arm.cmake which is my cross-compile toolchain file.

If I run cmake .. on Linux/x86 or Mac OS X, everything is fine. It finds and links with the correct libraries, as expected.

However, if I try to cross-compile, if fails to "find" the libraries like it does on the other platforms -- why?:

$ cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-raspberrypi-arm.cmake ..
Using Raspberry Pi Tools found in /home/davis/git/raspberrypi/tools
CMAKE_CROSSCOMPILING => TRUE
CMAKE_SYSTEM_NAME => Linux, PROJECT_SOURCE_DIR => /home/davis/git/ebike-firmware
CMAKE_CROSSCOMPILING is true...doing find_library for ZeroMQ...
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CZMQ_LIB
    linked by target "Runtime" in directory /home/davis/git/ebike-firmware/src
ZEROMQ_LIB
    linked by target "Runtime" in directory /home/davis/git/ebike-firmware/src

-- Configuring incomplete, errors occurred!
# this one is important
SET (CMAKE_SYSTEM_NAME Linux)
# this one not so much
SET (CMAKE_SYSTEM_VERSION 1)
# Check for Raspberry Pi Tools and bail out if they don't have it
if(DEFINED ENV{PI_TOOLS_HOME})
message("Using Raspberry Pi Tools found in $ENV{PI_TOOLS_HOME}")
else()
message("PI_TOOLS_HOME is not set; You must tell CMake where to find Raspberry Pi Tools (cross-compiler)")
return()
endif()
SET (PiToolsDir arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi)
# specify the cross compiler
SET (CMAKE_C_COMPILER $ENV{PI_TOOLS_HOME}/${PiToolsDir}/bin/arm-bcm2708hardfp-linux-gnueabi-gcc)
SET (CMAKE_CXX_COMPILER $ENV{PI_TOOLS_HOME}/${PiToolsDir}/bin/arm-bcm2708hardfp-linux-gnueabi-g++)
# where is the target environment
SET (CMAKE_FIND_ROOT_PATH $ENV{PI_TOOLS_HOME}/${PiToolsDir} ${PROJECT_SOURCE_DIR}/../lib/linux/pi)
SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ${PROJECT_SOURCE_DIR}/../lib/linux/pi)
# search for programs in the build host directories
SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
@pboettch
Copy link

For those finding this gist, the problem is the SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY). ONLY tells cmake to not look inside non-sysroot-dirs for libraries - even if you provided explicitly a path to find_library().

What needs to be done is SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) - then it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment