Linux下怎样编译通过使用C++17新特性的代码?

简介: 以下为示例:通过c++17的新特性实现对文件的读取。
  1. 需要有环境 [[Linux怎样更新Centos下Gcc版本支持C17?]]
  2. 代码需要在编译时指定 c++版本
  3. 如果使用多线程和锁,要加-pthread
  4. 使用文件系统类,需要额外的编译选项 -lstdc++fs
  5. 不需要额外指定动态库或者静态库地址,filesystem类包含在了libstdc++.so中
-rw-r--r-- 1 root root 4887654 Mar 27  2020 libstdc++.a
-rw-r--r-- 1 root root 1520198 Mar 27  2020 libstdc++fs.a
-rw-r--r-- 1 root root 1928418 Mar 27  2020 libstdc++_nonshared.a
-rw-r--r-- 1 root root     210 Mar 27  2020 libstdc++.so


以下为示例:通过c++17的新特性实现对文件的读取。


源码

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <filesystem>
#include <thread>
#include <mutex>
using namespace std;
namespace fs = std::filesystem;
class FileScanner
{
public:
    FileScanner(string path, bool recursive = true, bool parallel = false)
        : path_(path), recursive_(recursive), parallel_(parallel) {}
    map<string, string> search()
    {
        map<string, string> result;
        vector<thread> threads;
        mutex m;
        if (parallel_)
        {
            for (auto &p : fs::recursive_directory_iterator(path_))
            {
                if (!fs::is_directory(p))
                {
                    threads.emplace_back([&]()
                                         {
                        auto file_path = p.path().string();
                        auto file_name = p.path().filename().string();
                        ifstream file(file_path);
                        if (file) {
                            string content((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
                            lock_guard<mutex> lock(m);
                            result[file_name] = content;
                        } });
                }
            }
        }
        else if (recursive_)
        {
            for (auto &p : fs::recursive_directory_iterator(path_))
            {
                if (!fs::is_directory(p))
                {
                    auto file_path = p.path().string();
                    auto file_name = p.path().filename().string();
                    ifstream file(file_path);
                    if (file)
                    {
                        string content((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
                        result[file_name] = content;
                    }
                }
            }
        }
        else
        {
            for (auto &p : fs::directory_iterator(path_))
            {
                if (!fs::is_directory(p))
                {
                    auto file_path = p.path().string();
                    auto file_name = p.path().filename().string();
                    ifstream file(file_path);
                    if (file)
                    {
                        string content((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
                        result[file_name] = content;
                    }
                }
            }
        }
        if (parallel_)
        {
            for (auto &thread : threads)
            {
                thread.join();
            }
        }
        return result;
    }
private:
    string path_;
    bool recursive_;
    bool parallel_;
};
int main()
{
    FileScanner scanner("path/to/your/directory", true, true);
    map<string, string> result = scanner.search();
    for (auto &item : result)
    {
        cout << item.first << " : " << item.second << endl;
    }
    return 0;
}


编译方式:

g++ ReadFileDemo-4.cpp -std=c++17 -lstdc++fs -pthread -g -o Read_Test-4

目录
相关文章
|
3月前
|
存储 算法 安全
c++模板进阶操作——非类型模板参数、模板的特化以及模板的分离编译
在 C++ 中,仿函数(Functor)是指重载了函数调用运算符()的对象。仿函数可以像普通函数一样被调用,但它们实际上是对象,可以携带状态并具有更多功能。与普通函数相比,仿函数具有更强的灵活性和可扩展性。仿函数通常通过定义一个包含operator()的类来实现。public:// 重载函数调用运算符Add add;// 创建 Add 类的对象// 使用仿函数return 0;
116 0
|
8月前
|
自然语言处理 编译器 C语言
为什么C/C++编译腰要先完成汇编
C/C++ 编译过程中先生成汇编语言是历史、技术和实践的共同选择。历史上,汇编语言作为成熟的中间表示方式,简化了工具链;技术上,分阶段编译更高效,汇编便于调试和移植;实践中,保留汇编阶段降低了复杂度,增强了可移植性和优化能力。即使在现代编译器中,汇编仍作为重要桥梁,帮助开发者更好地理解和优化代码。
120 25
为什么C/C++编译腰要先完成汇编
|
7月前
|
消息中间件 Linux C++
c++ linux通过实现独立进程之间的通信和传递字符串 demo
的进程间通信机制,适用于父子进程之间的数据传输。希望本文能帮助您更好地理解和应用Linux管道,提升开发效率。 在实际开发中,除了管道,还可以根据具体需求选择消息队列、共享内存、套接字等其他进程间通信方
156 16
|
10月前
|
自然语言处理 编译器 Linux
告别头文件,编译效率提升 42%!C++ Modules 实战解析 | 干货推荐
本文中,阿里云智能集团开发工程师李泽政以 Alinux 为操作环境,讲解模块相比传统头文件有哪些优势,并通过若干个例子,学习如何组织一个 C++ 模块工程并使用模块封装第三方库或是改造现有的项目。
739 56
|
10月前
|
自然语言处理 编译器 Linux
|
11月前
|
存储 程序员 编译器
简述 C、C++程序编译的内存分配情况
在C和C++程序编译过程中,内存被划分为几个区域进行分配:代码区存储常量和执行指令;全局/静态变量区存放全局变量及静态变量;栈区管理函数参数、局部变量等;堆区则用于动态分配内存,由程序员控制释放,共同支撑着程序运行时的数据存储与处理需求。
501 22
|
10月前
|
Ubuntu Linux Shell
Linux 系统中的代码类型或脚本类型内容
在 Linux 系统中,代码类型多样,包括 Shell 脚本、配置文件、网络配置、命令行工具和 Cron 定时任务。这些代码类型广泛应用于系统管理、自动化操作、网络配置和定期任务,掌握它们能显著提高系统管理和开发的效率。
148 1
|
11月前
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
1877 3
|
11月前
|
Linux 编译器 C语言
Linux c/c++之多文档编译
这篇文章介绍了在Linux操作系统下使用gcc编译器进行C/C++多文件编译的方法和步骤。
136 0
Linux c/c++之多文档编译
|
11月前
|
Linux C语言 C++
vsCode远程执行c和c++代码并操控linux服务器完整教程
这篇文章提供了一个完整的教程,介绍如何在Visual Studio Code中配置和使用插件来远程执行C和C++代码,并操控Linux服务器,包括安装VSCode、安装插件、配置插件、配置编译工具、升级glibc和编写代码进行调试的步骤。
1997 0
vsCode远程执行c和c++代码并操控linux服务器完整教程