centos

CentOS下C++多线程怎么配置

小樊
46
2025-08-10 14:44:30
栏目: 编程语言

  1. 安装开发工具和库
    使用yum安装GCC编译器、glibc开发库和pthread库:

    sudo yum groupinstall "Development Tools"  # 安装编译工具链
    sudo yum install glibc-devel pthread-devel  # 安装多线程相关库
    
  2. 编写多线程程序
    创建C++文件(如test_threads.cpp),包含<thread>头文件,示例代码:

    #include <iostream>
    #include <thread>
    void hello() { std::cout << "Hello from thread!" << std::endl; }
    int main() {
        std::thread t(hello);
        t.join();
        return 0;
    }
    
  3. 编译程序
    使用g++编译时添加-std=c++11(启用C++11标准,支持多线程)和-pthread(链接线程库):

    g++ -std=c++11 -pthread test_threads.cpp -o test_threads
    
  4. 运行程序
    执行生成的可执行文件:

    ./test_threads
    

注意:CentOS 8已停止支持,建议升级到CentOS Stream或Ubuntu等长期支持系统。

0
看了该问题的人还看了