[CMake] problem w/ "find_package"_ makefile does not always compile

Nizar Khalifa Sallem nksallem at laas.fr
Mon Jan 3 03:57:12 EST 2011


At Sun, 2 Jan 2011 22:15:56 +0100,
ny wrote:
> 
> ** Apologies for double-posting my earlier message got truncated **
> 
> greetings!
> 
> I spent enough time trying to make the software work,
> so I'd be happy to get some feedback::
> I built research code written in (templated) c++, size is approximately 
> ~15K lines. I am on a mac and do not use Cocoa(/xcode), straight from 
> command line. Note that I choose to generate a Unix makefile, not an 
> xcode project and I am on snow leopard 10.6.5 w/ g++ 4.2.1.
> The pain was that there were no makefile coming along w/ the project, 
> so I had to translate the *.vcproj file from scratch to CMakelists and 
> then convert it to makefile. My directories structure (directories marked 
> w/ DIR_ else are files, indentation denotes deeper level):: 
> 
> DIR_4DMPU_exampleTest
>       
>       4DMPU_example.cpp
>       DIR_ann_1.1.2 
>       DIR_cmake
>       CMakeLists.txt
>       FindANN.cmake
>           Modules
>               FindANN.cmake
> 
>       DIR_include
>       
>       DIR_mc4d_tables
>           num_tet_table.cpp
>           tet_table.cpp
>           vert_table.cpp   
> 
> DIR_4DMPU_exampleTestBin
> 
> In the cmake GUI I add:
> ANN_LIBRARY                  /Users/nikos/Downloads/4DMPU_exampleTest/ann_1.1.2/include
> ANN_INCLUDE_DIR         /Users/nikos/Downloads/4DMPU_exampleTest/ann_1.1.2/lib/libANN.a
> 
> My CMakeLists.txt is::
> 
> cmake_minimum_required(VERSION 2.8)
> project( 4DMPU_example )
> 
> SET (CMAKE_BUILD_TYPE DEBUG)
> 
> set(SOURCE_FILES
> 4DMPU_example.cpp
> mc4d_tables/num_tet_table.cpp
> mc4d_tables/tet_table.cpp
> mc4d_tables/vert_table.cpp
> )
> 
> set(INCLUDE_FILES
> include/4dtables.h
> include/ann_helper.h
>                    ...                     # more headers
>         include/Volume.h ann_1.1.2/include
>         include/VolumeIO.h
> )
> 
> include_directories( 
>   ${PROJECT_SOURCE_DIR}/ann_1.1.2/include
>   ${PROJECT_SOURCE_DIR}/ann_1.1.2/lib
>   include
> )
> 
> INCLUDE(FindANN.cmake)
> IF(ANN_FOUND)
>   INCLUDE_DIRECTORIES( ${ANN_INCLUDE_DIR} )
> ENDIF(ANN_FOUND)
> 
> set(LIBS ${LIBS} ${ANN_LIBRARY})
> 
> add_executable( 4DMPU_example ${SOURCE_FILES} )
> target_link_libraries( 4DMPU_example ${LIBS} )
> 
> and the content of the FindANN.cmake::
> 
> FIND_LIBRARY(ANN_LIBRARY lANN
> ${PROJECT_SOURCE_DIR}/ann_1.1.2/lib
>   )
> FIND_PATH( ANN_INCLUDE_DIR ANN/ANN.h ANN/ANNperf.h ANN/ANNx.h 
>   ${PROJECT_SOURCE_DIR}/ann_1.1.2/include
>        )
> 
> IF(ANN_LIBRARY)
>   IF(ANN_INCLUDE_DIR)
> SET(ANN_FOUND TRUE)
>   ENDIF(ANN_INCLUDE_DIR)
> ENDIF(ANN_LIBRARY)
> 
> The project compiles fine this way.
> 
> Now, the problem is that if I follow the advice below
> 
> http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries#Using_external_libraries_in_your_project
> 
> and add the line
> 
> set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
> 
> and, moreover, substitute
> 
> include_directories( 
>   ${PROJECT_SOURCE_DIR}/ann_1.1.2/include
>   ${PROJECT_SOURCE_DIR}/ann_1.1.2/lib
>   include
> )
> 
> with
> 
> find_package(ANN REQUIRED)
> include_directories(${ANN_INCLUDE_DIR}
> include
> )
> 
> into CMakeLists.txt I get a linking problem. 
> I do not reproduce the message -> the point is that the ANN library is not linked
> w/ my object files. Can anyone tell me what I am doing wrong in the 2nd case? 
> 
> Note also that in the 2nd case I explicitly add::
> 
> ANN_DIR            /Users/nikos/Downloads/4DMPU_exampleTest/cmake/Modules 
>  
> in the cmake GUI.  
> 
> Thanks for any pointers
> N
> 
> 
Hi, the problem is that you don't have a CMakeLists for your ANN
library so 2 choices:
1. write a CMakeLists for ANN and then just add_subdirectory(ANN) and
use the ANN_library target name to link with.
2. assume that ANN is a part of your project which you have done so
far but the wrong way.
you need to do the following:
i- remove find_package(ANN REQUIRED) since as far as I know from ANN
it doesn't ship with a .pc or FindANN.cmake file so you can't use find
find_package(ANN REQUIRED)
ii- set ANN_INCLUDE_DIR and ANN_LIBS_DIR and ANN_LIBRARIES to be user
assigned values which you fill with cmake
.. -DANN_INCLUDE_DIR=/path/to/ANN/install etc.

Now I have submitted a while ago a CMakeLists.txt to the ANN library
project and they didn't include it so if you want you can have a ANN
subdirectory in your prject, place the CMakeLists.txt there and do as
1.

I attach the file
-------------- next part --------------
cmake_minimum_required(VERSION 2.6)

project(ANN CXX)
set(major 1)
set(minor 1)
set(revision 2)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ANN_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ANN_SOURCE_DIR}/bin)

# the ANN library
include_directories(include)
include_directories(src)
file(GLOB sources src/*.cpp)
add_library(ANN SHARED ${sources})
set_target_properties(ANN PROPERTIES VERSION ${major}.${minor}.${revision}
	                                   SOVERSION ${major}
																		 COMPILE_FLAGS "-O3")

# ANN test utility
include_directories(test)
file(GLOB test_sources test/*.cpp)
add_executable(ann_test ${test_sources})
target_link_libraries(ann_test ANN)

# ANN sample utility
add_executable(ann_sample sample/ann_sample.cpp)
target_link_libraries(ann_sample ANN)

# ann2fig utility
add_executable(ann2fig ann2fig/ann2fig.cpp)
target_link_libraries(ann2fig ANN)

# install
install(TARGETS ANN ann_test ann_sample ann2fig 
        RUNTIME DESTINATION bin
	      LIBRARY DESTINATION lib)
install(DIRECTORY sample doc DESTINATION share/ANN)
-------------- next part --------------


Cheers,
--
Nizar



More information about the CMake mailing list