Tag Archives: CMake

Cmake: How to Connect boost Library

Here is a simple guide on how to connect to boost library in Cmake.

 

How to Connect boost Library to Cmake

Situation 1: Not installed in the default path use:

set(BOOST_ROOT /usr/include/boost)////Set your own path
set(Boost_NO_SYSTEM_PATHS NO)////////Here is the system path for instructions. Generally, the default installation is in /usr/include/boost
find_package (Boost COMPONENTS regex system REQUIRED)

if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
add_executable(main main.cpp)
target_link_libraries (main ${Boost_LIBRARIES})
endif()

 

 

Situation 2: Install it in the default path and use it directly

 

find_package(Boost COMPONENTS regex system REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
add_executable(main main.cpp)
target_link_libraries (main ${Boost_LIBRARIES})
endif()

CMake: How to Complie Log4cplus

 

1. Download the official source code from
git clone –recurse https://github.com/log4cplus/log4cplus.git
log4cplus relies on catch and threadpool, if one of them fails, you can delete the corresponding file in the root of the source code and download it separately
As an example:
rm -rf threadpool
git clone https://github.com/log4cplus/ThreadPool.git threadpool
rm -rf catch
git clone https://github.com/philsquared/Catch.git catch
2. Compile the msvc repository
(1). Switch to the source root, and create a temporary folder for the cmake cache
mkdir .build_msvc
cd .build_msvc
(2). To compile the release version, execute the following command, where CMAKE_INSTALL_PREFIX specifies the library and file output path, in this case to the same level as the source code
# cmake -DCMAKE_INSTALL_PREFIX=… /… /thirdparties/log4cplus/msvc -DCMAKE_DEBUG_POSTFIX=d .
cmake -DCMAKE_INSTALL_PREFIX=. /… /thirdparties/log4cplus/msvc …
cmake –build . –config release -j8
cmake –install .
If the build is successful, there are three subdirectories in the directory, as follows.
— thirdparties/log4cplus/msvc
— bin
— include
— lib
(3). To compile the debug version, execute the following command
# cmake -DCMAKE_INSTALL_PREFIX=… /… /thirdparties/log4cplus/msvc -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_BUILD_TYPE=Debug …
# cmake -DCMAKE_INSTALL_PREFIX=… /… /thirdparties/log4cplus/msvc -DCMAKE_DEBUG_POSTFIX=d …
cmake –build . –config debug -j8
cmake –install . –config debug

3. Compile the mingw32 repository
(1). Add the mingw32/bin directory to the environment variable
PATH=D:/Qt/Qt5.15.2/Tools/mingw810_32/bin/;%PATH%
(2). Switch to the source root, create a temporary folder for the cmake cache
mkdir .mingw32
cd .mingw32
(3). Compile the release version, execute the following command, where CMAKE_INSTALL_PREFIX specifies the library and file output path
cmake.exe -G “MinGW Makefiles” “-DCMAKE_INSTALL_PREFIX:STRING=… /… /thirdparties/log4cplus/mingw32” “-DCMAKE_MAKE_PROGRAM=mingw32-make.exe” “-DCMAKE_C_COMPILER:STRING=gcc.exe” “-DCMAKE_CXX_COMPILER: STRING=g++.exe” …
cmake –build . -j8
cmake –install .
(4). To compile the debug version, execute the following command
cmake.exe -G “MinGW Makefiles” “-DCMAKE_BUILD_TYPE:STRING=Debug” “-DCMAKE_INSTALL_PREFIX:STRING=. /… /thirdparties/log4cplus/mingw32” “-DCMAKE_MAKE_PROGRAM=mingw32-make.exe” “-DCMAKE_C_COMPILER:STRING=gcc.exe” “-DCMAKE_CXX_COMPILER: STRING=g++.exe” …
cmake –build . –config debug -j8
cmake –install . –config debug

4. Using log4cplus in Qt
(1). You can add an environment variable directly from Qt creator to the output path specified during compilation, or you can copy the entire compiled folder to the project’s third-party library path
qt creator add env:
LOG4CPLUS_DIR with a value of . /… /thirdparties/log4cplus/msvc
Or add the path to the cmake file in CMakeLists.txt:
set(log4cplus_DIR … /thirdparties/msvc/lib/cmake/log4cplus)
(2). Add log4plus library
find_package(log4cplus REQUIRED)
target_link_libraries(TestLog4cplus log4cplus::log4cplusU)

cmake_minimum_required(VERSION 3.14)

project(TestLog4cplus LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (${MSVC})
    message("MSVC")
    set(log4cplus_DIR ../thirdparties/msvc/lib/cmake/log4cplus)
endif()

if (${MINGW})
    message("MINGW")
    if (not ${CMAKE_CL_64})
    else()
        set(log4cplus_DIR ../thirdparties/mingw32/lib/cmake/log4cplus)
    endif()
endif()

find_package(log4cplus REQUIRED)

add_executable(TestLog4cplus
    main.cpp
)

target_link_libraries(TestLog4cplus log4cplus::log4cplusU)
#include <map>
#include <string>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/log4cplus.h>


int main(int argc, char *argv[])
{
    (void)(argc);
    (void)(argv);

    using namespace log4cplus;
    const int LOOP_COUNT = 20;

    Initializer initializer;

    Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("test"));
    // logger.setLogLevel(INFO_LOG_LEVEL);
    logger.setLogLevel(ALL_LOG_LEVEL);

    tstring pattern = LOG4CPLUS_TEXT("%D{%Y-%m-%d %H:%M:%S.%q} %t %p %l %m%n");

    SharedAppenderPtr consoleAppender(new ConsoleAppender);
    consoleAppender->setName(LOG4CPLUS_TEXT("console"));
    // consoleAppender->setLayout(std::unique_ptr<Layout>(new SimpleLayout));
    consoleAppender->setLayout(std::unique_ptr<Layout>(new PatternLayout(pattern)));
    logger.addAppender(consoleAppender);

    // Create objects with a maximum of 200K and cache up to 5 log files (total is 6 files)
    SharedFileAppenderPtr fileAppender(new RollingFileAppender(LOG4CPLUS_TEXT("logs/Test.log"), 200 * 1024, 5, false, true));
    fileAppender->setName(LOG4CPLUS_TEXT("FirstFile"));
    // fileAppender->setLayout( std::unique_ptr<Layout>(new TTCCLayout()));
    fileAppender->setLayout(std::unique_ptr<Layout>(new PatternLayout(pattern)));
    // fileAppender->getloc();
    logger.addAppender(SharedAppenderPtr(fileAppender.get()));

    for (int i = 0; i < LOOP_COUNT; ++i) {
        LOG4CPLUS_DEBUG(logger, "Entering loop #" << i);
    }

    LOG4CPLUS_TRACE(logger, ("Hello trace"));
    LOG4CPLUS_DEBUG(logger, ("Hello debug"));
    LOG4CPLUS_INFO(logger, ("Hello info"));
    LOG4CPLUS_WARN(logger, ("Hello warn"));
    LOG4CPLUS_ERROR(logger, ("Hello error"));
    LOG4CPLUS_FATAL(logger, ("Hello fatal"));
    LOG4CPLUS_ASSERT(logger, ("Hello assert"));

    std::string str("Hello std::string info");
    LOG4CPLUS_INFO(logger, str.data());

    return 0;
}

Cmake solves the problem of phread Library in C + + 11: undefined reference to ` pthread_ create’

Method 1

PROJECT(HELLO)
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -std=c++11")
AUX_SOURCE_DIRECTORY(. SRC_LIST)
ADD_EXECUTABLE(hello ${SRC_LIST})

From the cmakelists.txt file of cmake, we can see that cmake has – STD = C + + 11, just like G + + uses C + + 11, so we can add – pthread to solve it

PROJECT(HELLO)
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -std=c++11 -pthread")
AUX_SOURCE_DIRECTORY(. SRC_LIST)
ADD_EXECUTABLE(hello ${SRC_LIST})

Method 2

Using package threads and link ${cmake_ THREAD_ LIBS_ INIT}

PROJECT(HELLO)
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -std=c++11")
FIND_PACKAGE(Threads)
AUX_SOURCE_DIRECTORY(. SRC_LIST)
ADD_EXECUTABLE(hello ${SRC_LIST})
TARGET_LINK_LIBRARIES(hello ${CMAKE_THREAD_LIBS_INIT})