V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
Sunrisepeak
V2EX  ›  分享创造

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

  •  
  •   Sunrisepeak ·
    Sunrisepeak · 78 天前 · 2429 次点击
    这是一个创建于 78 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近 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 [email protected]
    

    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
    

    其他

    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1007 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 23:02 · PVG 07:02 · LAX 16:02 · JFK 19:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.