Linking log4cpp library to project with cmake

4.9k views Asked by At

I am trying to link log4cpp to my project. I use CMake and i can't figure out a way to do so.

Log4cpp is install on projectfolder/log4cpp/ (with bin/ include/ lib/ ... in it)

I use the following Findlog4cpp.cmake :

IF (LOG4CPP_FOUND)
    SET(LOG4CPP_FIND_QUIETLY TRUE)
ENDIF (LOG4CPP_FOUND)

FIND_PATH(LOG4CPP_INCLUDE_DIR log4cpp/FileAppender.hh
    "./log4cpp/include/"
)

FIND_LIBRARY(LOG4CPP_LIBRARIES
  NAMES liblog4cpp.so
  PATHS "./log4cpp/lib"
)

SET(LOG4CPP_FOUND 0)
IF(LOG4CPP_INCLUDE_DIR)
  IF(LOG4CPP_LIBRARIES)
    SET(LOG4CPP_FOUND 1 CACHE INTERNAL "log4cpp found")
    IF (NOT LOG4CPP_FIND_QUIETLY)
      MESSAGE(STATUS "Found Log4CPP")
    ENDIF (NOT LOG4CPP_FIND_QUIETLY)
  ENDIF(LOG4CPP_LIBRARIES)
ENDIF(LOG4CPP_INCLUDE_DIR)

MARK_AS_ADVANCED(
  LOG4CPP_INCLUDE_DIR
  LOG4CPP_LIBRARIES
) 

and in my CMakeLists.txt i call it :

...
FIND_PACKAGE(log4cpp REQUIRED)
INCLUDE_DIRECTORIES(${LOG4CPP_INCLUDE_DIR})
SET(LIBS ${LOG4CPP_LIBRARIES} ${LIBS})

MESSAGE("############################# ${LOG4CPP_LIBRARIES}")
MESSAGE("############################# ${LOG4CPP_INCLUDE_DIR}")
...

The output :

-- Found Log4CPP
############################# /SOMEPATH/projectfolder/log4cpp/lib/liblog4cpp.so
############################# /SOMEPATH/projectfolder/log4cpp/include
-- Configuring done
-- Generating done
-- Build files have been written to: /SOMEPATH/projectfolder/

Then when i run make i have the folowing errors :

/SOMEPATH/projectfolder/common/Common.h:24:31: error: log4cpp/Category.hh: No such file or directory
/SOMEPATH/projectfolder/common/Common.h:25:35: error: log4cpp/FileAppender.hh: No such file or directory
/SOMEPATH/projectfolder/common/Common.h:26:34: error: log4cpp/BasicLayout.hh: No such file or directory

i just included the headers in common.h ( #include "log4cpp/xxx.hh" )

I add that all the build and compile works fine (with cmake then make) without log4cpp

Any help would be greatly appreciated

1

There are 1 answers

1
Fraser On BEST ANSWER

If you're adding your common directory as a subdirectory with its own CMakeLists.txt, you need to call INCLUDE_DIRECTORIES before you call ADD_SUBDIRECTORY if you want the directories already included to be passed down.