How to find specific/local files via CMake

2019-03-21 17:21发布

I have a problem with a locally installed library. In my project there is the xmlrpc++0.7-library:

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

I want CMake to fallback using the local xmlrpc++0.7 directory if not found otherwise. Two problems, the first one, find_path() or find_library() does not work with local dir. I used a workaround testing if variables processed by find_xxx() are empty or not. If empty I set them manually. The cmake generates the Makefile without errors now. But if I want to compile the project via make, the c++ compiler returns "error: XmlRpc.h: file not found". The file XmlRpc.h lies in myproject/xmlrpc++0.7/src and if I compile all them manually it works fine.

Here is my CMakeLists.txt. I am very happy if anyone could me point to the right solution to use cmake under conditions described above.

--- 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)

标签: include cmake
2条回答
Ridiculous、
2楼-- · 2019-03-21 17:50

the problem is solved. Here is my new 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)
查看更多
beautiful°
3楼-- · 2019-03-21 18:11

find_library does not care about local/global paths as you call them. It searches the paths where you specify it to look for. Verify that the paths you are searching for are correct. In your question you mention xmlrpc++0.7 but in your cmakelists.txt you look for xmlrpc0.7++ which would explain why it is not found. Also, as you say, the compiler fails on not finding xmlrpc.h but you use xmlrpcserver.h to find the path.

查看更多
登录 后发表回答