如何通过CMake查找特定/本地文件

时间:2011-01-11 14:49:47

标签: include cmake

我在本地安装的库时遇到问题。在我的项目中有xmlrpc ++ 0.7库:

myproject/
 +-- xmlrpc++0.7/
      +-- src/

如果没有找到,我希望CMake使用本地xmlrpc ++ 0.7目录进行回退。两个问题,第一个,find_path()或find_library()不适用于本地目录。我使用了一种变通方法来测试find_xxx()处理的变量是否为空。如果为空,我手动设置它们。 cmake现在生成Makefile而没有错误。但是如果我想通过make编译项目,那么c ++编译器会返回“错误:XmlRpc.h:找不到文件”。文件XmlRpc.h位于myproject / xmlrpc ++ 0.7 / src中,如果我手动编译它们就可以正常工作。

这是我的CMakeLists.txt。如果有人能指出在上述条件下使用cmake的正确解决方案,我感到非常高兴。

--- CMakeLists.txt ---

project(webservice_tesseract)
cmake_minimum_required(VERSION 2.6)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# find tesseract
find_path(TESSERACT_INCLUDE_DIR tesseract/tesseractmain.h
        /opt/local/include
 /usr/local/include
 /usr/include
 )
find_library(TESSERACT_LIBRARY_DIR 
 NAMES tesseract_main
 PATHS 
 /opt/local/lib/
 /usr/local/lib/
 /usr/lib
 )
message(STATUS "looked for tesseract library.")
message(STATUS "Include file detected: [${TESSERACT_INCLUDE_DIR}].")
message(STATUS "Lib file detected: [${TESSERACT_LIBRARY_DIR}].")
add_library(tesseract STATIC IMPORTED)
set_property(TARGET tesseract PROPERTY IMPORTED_LOCATION 
 ${TESSERACT_LIBRARY_DIR}/libtesseractmain.a
 )

#find xmlrpc++
message(STATUS "cmake home dir: [${CMAKE_HOME_DIRECTORY}].")
set(LOCAL_XMLRPCPLUSPLUS ${CMAKE_HOME_DIRECTORY}/xmlrpc0.7++/)
message(STATUS "xmlrpc++ local dir: [${LOCAL_XMLRPCPLUSPLUS}].")
find_path(XMLRPCPLUSPLUS_INCLUDE_DIR XmlRpcServer.h
 ${LOCAL_XMLRPCPLUSPLUS}src
 /opt/local/include
 /usr/local/include
 /usr/include
 )
find_library(XMLRPCPLUSPLUS_LIBRARY_DIR 
 NAMES XmlRpc
 PATHS 
 ${LOCAL_XMLRPCPLUSPLUS}
 /opt/local/lib/
 /usr/local/lib/
 /usr/lib/
 )
# next lines are an ugly workaround because cmake find_xxx() does not find local stuff
if (XMLRPCPLUSPLUS_INCLUDE_DIR)
else (XMLRPCPLUSPLUS_INCLUDE_DIR)
 set(XMLRPCPLUSPLUS_INCLUDE_DIR ${LOCAL_XMLRPCPLUSPLUS}src)
endif (XMLRPCPLUSPLUS_INCLUDE_DIR)
if (XMLRPCPLUSPLUS_LIBRARY_DIR)
else (XMLRPCPLUSPLUS_LIBRARY_DIR)
 set(XMLRPCPLUSPLUS_LIBRARY_DIR ${LOCAL_XMLRPCPLUSPLUS})
endif (XMLRPCPLUSPLUS_LIBRARY_DIR)
message(STATUS "looked for xmlrpc++ library.")
message(STATUS "Include file detected: [${XMLRPCPLUSPLUS_INCLUDE_DIR}].")
message(STATUS "Lib file detected: [${XMLRPCPLUSPLUS_LIBRARY_DIR}].")
add_library(xmlrpc STATIC IMPORTED)
set_property(TARGET xmlrpc PROPERTY IMPORTED_LOCATION 
 ${XMLRPCPLUSPLUS_LIBRARY_DIR}/libXmlRpc.a
 )
#### link together
include_directories(${XMLRPCPLUSPLUS_INCLUDE_DIR} ${TESSERACT_INCLUDE_DIR})
link_directories(${XMLRPCPLUSPLUS_LIBRARY_DIR} ${TESSERACT_LIBRARY_DIR})
add_library(simpleocr STATIC simple_ocr.cpp)
add_executable(webservice_tesseract webservice.cpp)
target_link_libraries(webservice_tesseract xmlrpc tesseract simpleocr)

2 个答案:

答案 0 :(得分:5)

问题解决了。这是我的新CMakeLists.txt:

project(webservice_tesseract)
cmake_minimum_required(VERSION 2.6)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# find tesseract
find_path(TESSERACT_INCLUDE_DIR tesseract/tesseractmain.h
    /opt/local/include
    /usr/local/include
    /usr/include
    )
find_library(TESSERACT_LIBRARY 
    NAMES tesseract_main
    PATHS 
    /opt/local/lib/
    /usr/local/lib/
    /usr/lib
    )
message(STATUS "looked for tesseract library.")
message(STATUS "Include file detected: [${TESSERACT_INCLUDE_DIR}].")
message(STATUS "Lib file detected: [${TESSERACT_LIBRARY}].")
add_library(tesseract STATIC IMPORTED)
set_property(TARGET tesseract PROPERTY IMPORTED_LOCATION 
    ${TESSERACT_LIBRARY}
    )
#find xmlrpc++
message(STATUS "cmake home dir: [${CMAKE_HOME_DIRECTORY}].")
set(LOCAL_XMLRPCPLUSPLUS ${CMAKE_HOME_DIRECTORY}/xmlrpc++0.7/)
message(STATUS "xmlrpc++ local dir: [${LOCAL_XMLRPCPLUSPLUS}].")
find_path(XMLRPCPLUSPLUS_INCLUDE_DIR XmlRpc.h
    ${LOCAL_XMLRPCPLUSPLUS}src
    /opt/local/include
    /usr/local/include
    /usr/include
    )
find_library(XMLRPCPLUSPLUS_LIBRARY 
    NAMES XmlRpc
    PATHS 
    ${LOCAL_XMLRPCPLUSPLUS}
    /opt/local/lib/
    /usr/local/lib/
    /usr/lib/
    )
message(STATUS "looked for xmlrpc++ library.")
message(STATUS "Include file detected: [${XMLRPCPLUSPLUS_INCLUDE_DIR}].")
message(STATUS "Lib file detected: [${XMLRPCPLUSPLUS_LIBRARY}].")
add_library(xmlrpc STATIC IMPORTED)
set_property(TARGET xmlrpc PROPERTY IMPORTED_LOCATION 
    ${XMLRPCPLUSPLUS_LIBRARY}
    )
#### link together
include_directories(${XMLRPCPLUSPLUS_INCLUDE_DIR} ${TESSERACT_INCLUDE_DIR})
link_directories(${XMLRPCPLUSPLUS_LIBRARY} ${TESSERACT_LIBRARY})
add_library(simpleocr STATIC simple_ocr.cpp)
add_executable(webservice_tesseract webservice.cpp)
target_link_libraries(webservice_tesseract xmlrpc tesseract simpleocr)

答案 1 :(得分:1)

find_library在调用它们时并不关心本地/全局路径。它会搜索您指定要查找的路径。验证您要搜索的路径是否正确。在您的问题中,您提到 xmlrpc ++ 0.7 ,但在您的cmakelists.txt中,您需要查找 xmlrpc0.7 ++ 这可以解释为什么没有找到它。另外,正如您所说,编译器无法找到 xmlrpc.h ,但您使用 xmlrpcserver.h 来查找路径。