一、项目代码
xlog.h
//xlog.h
#ifndef XLOG_H
#define XLOG_H
class XLog
{
public:
XLog();
};
#endif
xlog.cpp
#include "xlog.h"
#include <iostream>
using namespace std;
XLog::XLog()
{
cout<<"Create XLog"<<endl;
}
二、CMake编译静/动态库
1、CMake编译静态库【STATIC】
#CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(xlog)
add_library(xlog STATIC xlog.cpp xlog.h)
在xlog目录下执行:
cmake -S . -B build
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build
再执行:
cmake --build build
静态库打印如下:
[ 50%] Building CXX object CMakeFiles/xlog.dir/xlog.cpp.o
[100%] Linking CXX static library libxlog.a
[100%] Built target xlog
2、CMake编译动态库【SHARED】
#CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(xlog)
add_library(xlog SHARED xlog.cpp xlog.h)
在xlog目录下执行:
cmake -S . -B build
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build
再执行:
cmake --build build
动态库打印如下:
[ 50%] Building CXX object CMakeFiles/xlog.dir/xlog.cpp.o
[100%] Linking CXX shared library libxlog.so
[100%] Built target xlog