I'm trying to build a project with the library libconfig - not linked. Errors of the form:

/var/deadBranch/nginx-instaban/main.cpp:27: undefined reference to `libconfig::Config::Config()' /var/deadBranch/nginx-instaban/main.cpp:33: undefined reference to `libconfig::Config::readFile(char const*)' 

CmakeLists.txt:

 cmake_minimum_required(VERSION 3.5) project(nginx-instaban) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lconfig++ -lconfig") set(SOURCE_FILES main.cpp strFuncs.h banMgr.h banMgr.cpp banMgr.h nginxConfigReader.cpp nginxConfigReader.h IpParser.h IpHashTableGeneric.cpp IpHashTableGeneric.h BanRecord.h IpParser.cpp BinaryOps.h CheckIpHashTable.cpp CheckIpHashTable.h Configuration.cpp Configuration.h) add_executable(nginx-instaban ${SOURCE_FILES}) 

Installed libconfig ++ - dev and libconfig ++ 8-dev for some reason.

  • *-dev packages are object and header files needed to build programs that use the library from the corresponding non- *-dev package. - ߊߚߤߘ

2 answers 2

make cmake -DCMAKE_VERBOSE_MAKEFILE = ON to see the current compilation line

most likely you did not link the library itself to

target_link_libraries(nginx-instaban config)

Here , for example, we can see .a and .so files:

 /usr/include/libconfig.h++ /usr/lib/x86_64-linux-gnu/libconfig++.a /usr/lib/x86_64-linux-gnu/libconfig++.so 

This means that this is not a header-only library, which means it should be linked.

    Use the dependency search engine provided by CMake. An example search configuration file is FindLibConfig.cmake .

    Example of use:

     cmake_minimum_required(VERSION 3.5) project(nginx-instaban) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread") set(SOURCE_FILES ...) add_executable(nginx-instaban ${SOURCE_FILES}) find_package(Config REQUIRED) find_package(Config++ REQUIRED) if (LIBCONFIG_FOUND) target_link_libraries(nginx-instaban ${LIBCONFIG_LIBRARIES}) target_include_directories(nginx-instaban ${LIBCONFIG_INCLUDE_DIR}) endif()