昭通市网站建设_网站建设公司_门户网站_seo优化
2026/1/10 15:49:05 网站建设 项目流程

在Mac OS 15.5系统中,最近想用C++23,试了试gcc15.2,发现出现无法链接模块功能:

首先下一个最简单的Helloworld.cpp:

import std;

int main()
{
std::println("Hello,world!");
return 0;
}

使用gcc15.2编译:

g++-15 -std=c++23 -fmodules helloworld.cpp

出现链接不到模块的错误:

Undefined symbols for architecture arm64:
"initializer for module std", referenced from:
__static_initialization_and_destruction_0() in ccEgD1TM.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status

参考了网上一位大神的文章介绍:https://download.csdn.net/blog/column/11938572/149222276

使用这个命令重新编译:g++ -std=c++23 -fmodules -fsearch-include-path bits/std.cc helloworld.cpp

bit/std.cc是std模块所在位置,我们需要找出gcc15.2在Mac OS中安装位置,使用这个命令可以找到:/* by yours.tools - online tools website : yours.tools/zh/post.html */ g++-15 -print-search-dirs

gcc15.2安装位置:

install: /opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/

programs: =/opt/homebrew/Cellar/gcc/15.2.0/bin/../libexec/gcc/aarch64-apple-darwin24/15/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../libexec/gcc/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/../../../../../../aarch64-apple-darwin24/bin/aarch64-apple-darwin24/15/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/../../../../../../aarch64-apple-darwin24/bin/

libraries: =/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/../../../../../../aarch64-apple-darwin24/lib/aarch64-apple-darwin24/15/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/../../../../../../aarch64-apple-darwin24/lib/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/../../../aarch64-apple-darwin24/15/:/opt/homebrew/Cellar/gcc/15.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/15/../../../

可以看到std.cc模块位置是:/opt/homebrew/Cellar/gcc/15.2.0/include/c++/15/bits/std.cc

然后替换一下刚才的编译命令中std.cc的位置即可:

g++-15 -std=c++23 -fmodules -fsearch-include-path /opt/homebrew/Cellar/gcc/15.2.0/include/c++/15/bits/std.cc helloworld.cpp

./a.out
Hello,world!

大功告成,希望C++23成熟以后,不用自己手动指定模块位置吧。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询