How to build c++20 using modules with autotools?

问题描述 投票:0回答:1

这是项目:

使用关键字import的源程序:hello.cpp

import <iostream>;
#include "config.h"

int main() {
    std::cout << "Hello World, " << "Version: " << VERSION << std::endl;
}

configure.ac文件:

AC_PREREQ([2.71])
AC_INIT([hello], [0.1.7], [[email protected]])
AC_CONFIG_SRCDIR([hello.cc])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([-Wno-portability])

AC_PROG_CXX

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Makefile.am:

AM_CXXFLAGS=-std=c++23 -fmodules-ts

bin_PROGRAMS=hello
hello_SOURCES=hello.cc

%.c++m:
    $(CXX) -std=c++23 -fmodules-ts -xc++-system-header $(notdir $(basename $@))

观察最后一句“%.c++m”;这不是标准,迫使我使用 AM_INIT_AUTOMAKE([-Wno-portability])。如果删除“%”以符合标准,我会收到以下消息:

 cd . && /bin/sh /home/ventura/c/hello/missing automake-1.16 --gnu Makefile
 cd . && /bin/sh ./config.status Makefile depfiles
config.status: creating Makefile
config.status: executing depfiles commands
make  all-am
make[1]: Entering directory '/home/ventura/c/hello'
make[1]: *** No rule to make target '/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/iostream.c++m', needed by 'hello.o'.  Stop.
make[1]: Leaving directory '/home/ventura/c/hello'
make: *** [Makefile:277: all] Error 2

如何在 GNU autotools 构建系统中集成 -std=c++20 模块???

现在我使用旧的“%”但是,其他问题由此产生,iostream 模块总是编译忽略 gmc.cache/.../iostream.gmc 时间戳。

c++ c++20 autotools automake c++-modules
1个回答
0
投票

试试这个

更新您的 configure.ac 文件以包含 AC_LANG_CXX 宏,这将在 Autoconf 配置脚本中启用 C++ 语言支持:

AC_PREREQ([2.71])
AC_INIT([hello], [0.1.7], [[email protected]])
AC_CONFIG_SRCDIR([hello.cc])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([-Wno-portability])

AC_PROG_CXX
AC_LANG_CXX

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

更新您的 Makefile.am 文件以使用 AM_CXXFLAGS 变量来包含 C++20 语言支持的 -std=c++20 选项:

AM_CXXFLAGS=-std=c++20 -fmodules-ts

bin_PROGRAMS=hello
hello_SOURCES=hello.cc

%.c++m:
    $(CXX) -std=c++20 -fmodules-ts -xc++-system-header $(notdir $(basename $@))

请注意,您应该使用 -std=c++20 而不是 -std=c++23,因为 C++23 尚未最终确定并且并非所有编译器都支持它。

更新源文件 hello.cpp 以使用 import 语句包含 iostream 模块:

import <iostream>;
#include "config.h"

int main() {
    std::cout << "Hello World, " << "Version: " << VERSION << std::endl;
}

试试看使用 GNU autotools 构建系统的 C++20 模块支持是否有效。请注意,iostream 模块应该由编译器自动缓存,因此您不需要在每次构建项目时都重新编译它。

© www.soinside.com 2019 - 2024. All rights reserved.