
最近 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 的环境并成功编译运行上面的代码
通过xlings 工具可以在不影响系统环境的情况下, 从源码构建 gcc 并安装
安装 xlings 工具
curl -fsSL https://d2learn.org/xlings-install.sh | bash 注: 更多详情见 -> xlings 仓库
安装 gcc15.1.0
运行安装命令并确认后, 会自动获取 gcc 源码、处理依赖和编译安装
xlings install [email protected]
查看版本进行确认
gcc --version 创建 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 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