CMake I 使用Boost库filesystem

目录

一、 关于Boost库

二、使用Boost库

1.使用的命令及参数

(1)BOOST_INCLUDEDIR/BOOST_INCLUDE_DIR

(2)BOOST_LIBRARYDIR/BOOST_LIBRARY_DIR

(3)target_include_directories/include_directories

2.源码

(1)CMakeLists.txt文件

(2).cpp文件

三、配置及构建


一、 关于Boost库

        新版Boost库在VS IDE中的使用可参考C++ | boost库入门,头文件和静态库并没有安装在windows标准位置,所以在使用Boost库时,需要手动设置头文件和lib文件目录。

二、使用Boost库

1.使用的命令及参数

(1)BOOST_INCLUDEDIR/BOOST_INCLUDE_DIR

set(BOOST_INCLUDEDIR  "D:/thirdParties/boost_1_74_0")

(2)BOOST_LIBRARYDIR/BOOST_LIBRARY_DIR

set(BOOST_LIBRARYDIR  "D:/thirdParties/boost_1_74_0/stage/lib")

(3)target_include_directories/include_directories

        给源文件添加头文件搜索路径:将指定目录添加到编译器的头文件搜索路径之下,指定的目录被解释成当前源码路径的相对路径。鉴于可能有不同目录相同名称的头文件,所以建议针对特定的target进行添加头文件的操作,即推荐用target_include_directories。

target_include_directories(testBoost ${Boost_INCLUDE_DIRS})
  • link_directories:指定库文件的路径
  • target_link_directories:将目标文件与库文件进行链接(推荐)
target_link_directories(testBoost ${Boost_LIBRARY_DIRS})

2.源码

(1)CMakeLists.txt文件

        注意:BOOST_ROOT, BOOST_INCLUDEDIR, BOOST_LIBRARYDIR在windows下需要设置,否则cmake找不到目录。

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(useBoost LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#set(Boost_USE_STATIC_RUNTIME ON) #可选
#set(Boost_USE_STATIC_LIBS ON)#可选
#set(BOOST_ROOT "D:/thirdParties/boost_1_74_0/boost")#可选
set(BOOST_INCLUDEDIR  "D:/thirdParties/boost_1_74_0")#Boost_INCLUDE_DIR
set(BOOST_LIBRARYDIR  "D:/thirdParties/boost_1_74_0/stage/lib")#Boost_LIBRARY_DIR
#set(BOOST_NO_SYSTEM_PATHS ON)
#set(Boost_DEBUG on)
#set(Boost_NO_BOOST_CMAKE ON)
find_package(BOOST REQUIRED  COMPONENTS  system filesystem)#BOOST大写
if (NOT BOOST_FOUND)
	message("未发现Boost")
endif()
include_directories(${Boost_INCLUDE_DIRS}) 
link_directories(${Boost_LIBRARY_DIRS})
add_executable(testBoost test.cpp)
message(STATUS "Boost_INCLUDE_DIRS=${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARY_DIRS=${Boost_LIBRARY_DIRS}")

message(STATUS "Boost_FILESYSTEM_LIBRARIES=${Boost_FILESYSTEM_LIBRARY}")#为空
message(STATUS "Boost_LIBRARIES=${BOOST_LIBRARIES}")#为空
#target_link_libraries(testBoost Boost::filesystem)#有问题
target_link_libraries(testBoost PUBLIC ${BOOST_LIBRARIES})

(2).cpp文件

#include 

#include 

using namespace std;
using namespace boost::filesystem;

const char *say_what(bool b) { return b ? "true" : "false"; }

int main(int argc, char *argv[]) {
  if (argc < 2) {
    cout
        << "Usage: path_info path-element [path-element...]\n"
           "Composes a path via operator/= from one or more path-element arguments\n"
           "Example: path_info foo/bar baz\n"
#ifdef BOOST_POSIX_API
           "         would report info about the composed path foo/bar/baz\n";
#else // BOOST_WINDOWS_API
           "         would report info about the composed path foo/bar\\baz\n";
#endif
    return 1;
  }

  path p;
  for (; argc > 1; --argc, ++argv)
    p /= argv[1]; // compose path p from the command line arguments

  cout << "\ncomposed path:\n";
  cout << "  operator<<()---------: " << p << "\n";
  cout << "  make_preferred()-----: " << p.make_preferred() << "\n";

  cout << "\nelements:\n";
  for (auto element : p)
    cout << "  " << element << '\n';

  cout << "\nobservers, native format:" << endl;
#ifdef BOOST_POSIX_API
  cout << "  native()-------------: " << p.native() << endl;
  cout << "  c_str()--------------: " << p.c_str() << endl;
#else // BOOST_WINDOWS_API
  wcout << L"  native()-------------: " << p.native() << endl;
  wcout << L"  c_str()--------------: " << p.c_str() << endl;
#endif
  cout << "  string()-------------: " << p.string() << endl;
  wcout << L"  wstring()------------: " << p.wstring() << endl;

  cout << "\nobservers, generic format:\n";
  cout << "  generic_string()-----: " << p.generic_string() << endl;
  wcout << L"  generic_wstring()----: " << p.generic_wstring() << endl;

  cout << "\ndecomposition:\n";
  cout << "  root_name()----------: " << p.root_name() << '\n';
  cout << "  root_directory()-----: " << p.root_directory() << '\n';
  cout << "  root_path()----------: " << p.root_path() << '\n';
  cout << "  relative_path()------: " << p.relative_path() << '\n';
  cout << "  parent_path()--------: " << p.parent_path() << '\n';
  cout << "  filename()-----------: " << p.filename() << '\n';
  cout << "  stem()---------------: " << p.stem() << '\n';
  cout << "  extension()----------: " << p.extension() << '\n';

  cout << "\nquery:\n";
  cout << "  empty()--------------: " << say_what(p.empty()) << '\n';
  cout << "  is_absolute()--------: " << say_what(p.is_absolute()) << '\n';
  cout << "  has_root_name()------: " << say_what(p.has_root_name()) << '\n';
  cout << "  has_root_directory()-: " << say_what(p.has_root_directory()) << '\n';
  cout << "  has_root_path()------: " << say_what(p.has_root_path()) << '\n';
  cout << "  has_relative_path()--: " << say_what(p.has_relative_path()) << '\n';
  cout << "  has_parent_path()----: " << say_what(p.has_parent_path()) << '\n';
  cout << "  has_filename()-------: " << say_what(p.has_filename()) << '\n';
  cout << "  has_stem()-----------: " << say_what(p.has_stem()) << '\n';
  cout << "  has_extension()------: " << say_what(p.has_extension()) << '\n';

  return 0;
}

三、配置及构建

CMake I 使用Boost库filesystem_第1张图片

        虽然提示说find_package函数中我"BOOST"写错了,应该为"Boost",但我改为"Boost"后会报错。(原因未知)

CMake I 使用Boost库filesystem_第2张图片

 

你可能感兴趣的:(CMake,Boost,cmake)