跳转至内容
  • 版块
  • 最新
  • 标签
  • 热门
  • Online Tools
  • 用户
  • 群组
折叠
品牌标识

D2Learn Forums

  1. 主页
  2. Open source | 开源
  3. xlings
  4. [xim+]: 最新gcc15.1.0发布, 一键从源码构建 -- c++23 import std启动

[xim+]: 最新gcc15.1.0发布, 一键从源码构建 -- c++23 import std启动

已定时 已固定 已锁定 已移动 xlings
xlingsximgccc++23从源码构建
2 帖子 1 发布者 166 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • SPeakS 离线
    SPeakS 离线
    SPeak d2learn-dev
    编写于 最后由 SPeak 编辑
    #1

    最近gnu发布了最新版本的gcc15.1.0支持了很多c++23的特性, 其中最引人注意的就是支持了模块化标准库. 这个特性能让开发这不需要一个一个的include <xxx>或import <xxx>来使用std中的一些函数或功能, 而只需要import std;即可。例如下面的代码:

    import std;
    
    auto main() -> int {
        std::println("Hello, World!");
        return 0;
    }
    

    我相信很多朋友第一次看到这样的代码时, 可能会不由的发出一些惊叹: 这还是我认识的C++吗?
    没有关系, 下面就来介绍一下如何配置gcc15.1.0的环境并成功编译运行上面的代码

    1.一键从源码构建安装gcc15.1.0

    通过xlings工具可以在不影响系统环境的情况下, 从源码构建gcc并安装

    安装xlings工具

    curl -fsSL https://d2learn.org/xlings-install.sh | bash
    

    注: 更多详情见 -> xlings仓库

    安装gcc15.1.0

    运行安装命令并确认后, 会自动获取gcc源码、处理依赖和编译安装

    xlings install gcc@15.1.0
    

    553b6f02-999b-4319-8154-eb18f64cff5b-image.png

    查看版本进行确认

    gcc --version
    

    2.生成模块化std并构建一个HelloWorld程序

    创建helloworld.cpp文件

    import std;
    
    auto main() -> int {
        std::println("Hello, World!");
        return 0;
    }
    

    构建std模块缓存

    g++ -std=c++23 -fmodules -O2 -c -fmodule-only -fsearch-include-path bits/std.cc
    

    编译helloworld程序

    g++ -std=c++23 -fmodules -O2 helloworld.cpp -o helloworld
    

    运行helloworld程序&验证

    ./helloworld
    

    3.xpkg包文件 - 构建细节

    function __gcc_url(version) return format("https://ftp.gnu.org/gnu/gcc/gcc-%s/gcc-%s.tar.xz", version, version) end
    
    package = {
        -- base info
        name = "gcc",
        description = "GCC, the GNU Compiler Collection",
    
        authors = "GNU",
        license = "GPL",
        repo = "https://github.com/gcc-mirror/gcc",
        docs = "https://gcc.gnu.org/wiki",
    
        -- xim pkg info
        type = "package",
        archs = {"x86_64"},
        status = "stable", -- dev, stable, deprecated
        categories = {"compiler", "gnu", "language"},
        keywords = {"compiler", "gnu", "gcc", "language", "c", "c++"},
    
        -- xvm: xlings version management
        xvm_enable = true,
    
        xpm = {
            linux = {
                deps = { "make", "gcc" },
                ["latest"] = { ref = "15.1.0" },
                ["15.1.0"] = { url = __gcc_url("15.1.0") },
                ["14.2.0"] = { url = __gcc_url("14.2.0") },
                ["13.3.0"] = { url = __gcc_url("13.3.0") },
                ["12.4.0"] = { url = __gcc_url("12.4.0") },
                ["11.5.0"] = { url = __gcc_url("11.5.0") },
            },
        },
    }
    
    import("xim.libxpkg.system")
    import("xim.libxpkg.pkginfo")
    import("xim.libxpkg.log")
    import("xim.libxpkg.xvm")
    
    function install()
        local builddir = path.join(pkginfo.install_dir(), "xim_build")
        local objdir = path.join(pkginfo.install_dir(), "xim_build", "objdir")
        local prerequisites_dir = path.join(path.directory(pkginfo.install_dir()), "comm-prerequisites")
    
        log.info("0.clean build cache...")
        if not os.isdir(prerequisites_dir) then os.mkdir(prerequisites_dir) end
        for _, dir in ipairs(os.dirs(path.join(prerequisites_dir, "**"))) do
            -- if dir is empty, remove it
            if os.emptydir(dir) then
                os.tryrm(dir)
            end
        end
    
        os.tryrm(builddir)
        os.mkdir(builddir)
        system.exec(string.format("tar xvf gcc-%s.tar.xz -C %s", pkginfo.version(), builddir))
        os.cd(path.join(builddir, "gcc-" .. pkginfo.version()))
    
        log.info("1.download prerequisites...")
        -- readfile - contrib/download_prerequisites
        local filecontent = io.readfile("contrib/download_prerequisites")
        filecontent = filecontent:replace("--no-verbose", " ", { plain = true })
        io.writefile("contrib/download_prerequisites", filecontent)
        system.exec("contrib/download_prerequisites --directory=" .. prerequisites_dir)
    
        log.info("2.build config...")
        os.mkdir(objdir)
        os.cd(objdir)
        system.exec(string.format([[%s/gcc-%s/configure
            --prefix=%s --enable-languages=c,c++ --disable-multilib
        ]], builddir, pkginfo.version(), pkginfo.install_dir()))
    
        log.info("3.build gcc...")
        system.exec("time make -j32", { retry = 2 })
    
        log.info("4.install gcc...")
        system.exec("make install")
        return true
    end
    
    function config()
        local gcc_bindir = path.join(pkginfo.install_dir(), "bin")
        local ld_lib_path = string.format("%s:%s", path.join(pkginfo.install_dir(), "lib64"), os.getenv("LD_LIBRARY_PATH") or "")
        
        local config = {
            bindir = gcc_bindir,
            envs = {
                ["LD_LIBRARY_PATH"] = ld_lib_path,
            }
        }
    
        xvm.add("gcc", config)
        xvm.add("g++", config)
    
        return true
    end
    
    function uninstall()
        xvm.remove("gcc")
        xvm.remove("g++")
        return true
    end
    

    其他

    • gcc主页
    • xlings工具
    • gcc的xpkg文件
    • 原文地址
    • 知乎问题
    1 条回复 最后回复
    0
    • SPeakS 离线
      SPeakS 离线
      SPeak d2learn-dev
      编写于 最后由 编辑
      #2

      对应的视频演示: https://www.bilibili.com/video/BV1qvGRzcErF

      1 条回复 最后回复
      0

      • 登录

      • 没有帐号? 注册

      • 登录或注册以进行搜索。
      d2learn forums Powered by NodeBB
      • 第一个帖子
        最后一个帖子
      0
      • 版块
      • 最新
      • 标签
      • 热门
      • Online Tools
      • 用户
      • 群组