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

目录
相关文章
|
2月前
|
自然语言处理 编译器 C语言
为什么C/C++编译腰要先完成汇编
C/C++ 编译过程中先生成汇编语言是历史、技术和实践的共同选择。历史上,汇编语言作为成熟的中间表示方式,简化了工具链;技术上,分阶段编译更高效,汇编便于调试和移植;实践中,保留汇编阶段降低了复杂度,增强了可移植性和优化能力。即使在现代编译器中,汇编仍作为重要桥梁,帮助开发者更好地理解和优化代码。
63 25
为什么C/C++编译腰要先完成汇编
|
1月前
|
消息中间件 Linux C++
c++ linux通过实现独立进程之间的通信和传递字符串 demo
的进程间通信机制,适用于父子进程之间的数据传输。希望本文能帮助您更好地理解和应用Linux管道,提升开发效率。 在实际开发中,除了管道,还可以根据具体需求选择消息队列、共享内存、套接字等其他进程间通信方
69 16
|
4月前
|
自然语言处理 编译器 Linux
告别头文件,编译效率提升 42%!C++ Modules 实战解析 | 干货推荐
本文中,阿里云智能集团开发工程师李泽政以 Alinux 为操作环境,讲解模块相比传统头文件有哪些优势,并通过若干个例子,学习如何组织一个 C++ 模块工程并使用模块封装第三方库或是改造现有的项目。
439 56
|
5月前
|
编译器 程序员 定位技术
C++ 20新特性之Concepts
在C++ 20之前,我们在编写泛型代码时,模板参数的约束往往通过复杂的SFINAE(Substitution Failure Is Not An Error)策略或繁琐的Traits类来实现。这不仅难以阅读,也非常容易出错,导致很多程序员在提及泛型编程时,总是心有余悸、脊背发凉。 在没有引入Concepts之前,我们只能依靠经验和技巧来解读编译器给出的错误信息,很容易陷入“类型迷路”。这就好比在没有GPS导航的年代,我们依靠复杂的地图和模糊的方向指示去一个陌生的地点,很容易迷路。而Concepts的引入,就像是给C++的模板系统安装了一个GPS导航仪
188 59
|
4月前
|
自然语言处理 编译器 Linux
|
4月前
|
安全 编译器 C++
【C++11】新特性
`C++11`是2011年发布的`C++`重要版本,引入了约140个新特性和600个缺陷修复。其中,列表初始化(List Initialization)提供了一种更统一、更灵活和更安全的初始化方式,支持内置类型和满足特定条件的自定义类型。此外,`C++11`还引入了`auto`关键字用于自动类型推导,简化了复杂类型的声明,提高了代码的可读性和可维护性。`decltype`则用于根据表达式推导类型,增强了编译时类型检查的能力,特别适用于模板和泛型编程。
37 2
|
4月前
|
Ubuntu Linux Shell
Linux 系统中的代码类型或脚本类型内容
在 Linux 系统中,代码类型多样,包括 Shell 脚本、配置文件、网络配置、命令行工具和 Cron 定时任务。这些代码类型广泛应用于系统管理、自动化操作、网络配置和定期任务,掌握它们能显著提高系统管理和开发的效率。
|
5月前
|
存储 程序员 编译器
简述 C、C++程序编译的内存分配情况
在C和C++程序编译过程中,内存被划分为几个区域进行分配:代码区存储常量和执行指令;全局/静态变量区存放全局变量及静态变量;栈区管理函数参数、局部变量等;堆区则用于动态分配内存,由程序员控制释放,共同支撑着程序运行时的数据存储与处理需求。
314 22
|
5月前
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
1132 3
|
5月前
|
Linux 编译器 C语言
Linux c/c++之多文档编译
这篇文章介绍了在Linux操作系统下使用gcc编译器进行C/C++多文件编译的方法和步骤。
79 0
Linux c/c++之多文档编译

热门文章

最新文章