[xim+]: 最新 gcc15.1.0 发布, 一键从源码构建 -- c++23 import std 启动 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
Sunrisepeak
V2EX    分享创造

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

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

    最近 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     Solana     5666 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 20ms UTC 02:07 PVG 10:07 LAX 18:07 JFK 21:07
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86