如何编辑和重新构建GCC libstdc++C++标准库源代码

How to edit and re-build the GCC libstdc++ C++ standard library source?

本文关键字:libstdc++C++ GCC 构建 标准 源代码 何编辑 编辑 新构建      更新时间:2023-10-16

我正在进行一些研究,并希望编辑libstdc++库中的一些源代码以进行实验。特别是,我对实验并行排序算法很感兴趣。有没有地方可以让我找到文档来轻松编辑和构建源代码?

我曾尝试构建各种版本的libstdc++库,但没有成功。似乎大多数新版本都需要构建整个gcc包,这是一个漫长得多的过程,尤其是如果我要在libstdc++中编辑和试用一些文件。

我也无法找到包含并行排序算法的源文件。我似乎只能找到定义函数的头文件,而不能找到源代码本身。如有任何建议或文件链接,我们将不胜感激。

是的,您必须构建整个GCC,但一旦完成,您只需要重新构建libstdc++部分。

建筑GCC描述见http://gcc.gnu.org/wiki/InstallingGCC

libstdc++源位于libstdc++-v3目录中。并行算法在libstdc++-v3/include/parallel中,它们是模板,因此所有代码都在标头中。少量非标题代码在libstdc++-v3/src/c++98/parallel-settings.cc

在正常配置和构建整个GCC之后,可以通过在$TARGET/libstdc++-v3目录中运行make来重建libstdc++(其中$TARGET类似于x86_64-pc-linux-gnu)。

默认情况下,生成文件没有适当的依赖关系,这会导致在标头更改后重新生成对象,因此您可能需要再次执行make clean,然后再执行make,以获取您的更改。

最小分步示例

从源代码编译GCC。精简命令:

sudo apt-get build-dep gcc
git clone git://gcc.gnu.org/git/gcc.git
cd gcc
git checkout gcc-6_4_0-release
./contrib/download_prerequisites
mkdir build
cd build
../configure --enable-languages=c,c++ --prefix="$(pwd)/install"
make -j`nproc`
make install

等待30分钟到两个小时。现在让我们使用这个测试程序a.cpp:

#include <cassert>
#include <queue>
int main() {
std::priority_queue<int> q;
q.emplace(2);
q.emplace(1);
q.emplace(3);
assert(q.top() == 3);
q.pop();
assert(q.top() == 2);
q.pop();
assert(q.top() == 1);
q.pop();
}

首先编译并运行它以确保初始编译有效:

gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out

现在让我们破解priority_queue构造函数。

首先,您可以很容易地使用GDB找到实际的构造函数,如下所述:我应该何时使用make_heap vs.Priority Queue?

所以我们用这个补丁破解了它:

diff --git a/libstdc++-v3/include/bits/stl_queue.h b/libstdc++-v3/include/bits/stl_queue.h
index 5d255e7300b..deec7bc4d99 100644
--- a/libstdc++-v3/include/bits/stl_queue.h
+++ b/libstdc++-v3/include/bits/stl_queue.h
@@ -61,6 +61,7 @@
#if __cplusplus >= 201103L
# include <bits/uses_allocator.h>
#endif
+#include <iostream>

namespace std _GLIBCXX_VISIBILITY(default)
{
@@ -444,7 +445,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
priority_queue(const _Compare& __x = _Compare(),
_Sequence&& __s = _Sequence())
: c(std::move(__s)), comp(__x)
-      { std::make_heap(c.begin(), c.end(), comp); }
+      {
+        std::cout << "hacked" << std::endl;
+        std::make_heap(c.begin(), c.end(), comp);
+      }

template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
explicit

然后重建并重新安装libstdc++以节省大量时间:

cd gcc/build/x86_64-pc-linux-gnu/libstdc++-v3
make -j`nproc`
make install

现在是下一个构建和运行:

gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out

输出:

hacked

在Ubuntu 16.04上测试。

Ubuntu 22.04,GCC 12.1

我们需要:

  • --disable-multilib添加到./configure
  • (未经测试)sudo apt install gcc-multilib g++-multilib,这些链接到支持32位可执行文件:如何在64位Ubuntu上编译32位应用程序

否则失败:

configure: WARNING: using in-tree isl, disabling version check          
*** This configuration is not supported in the following subdirectories:                                                                                                                                           
gnattools gotools target-libada target-libphobos target-zlib target-libbacktrace target-libgfortran target-libgo target-libffi target-libobjc target-liboffloadmic
(Any other directories should still work fine.) 
checking for default BUILD_CONFIG... bootstrap-debug
checking for --enable-vtable-verify... no
/usr/bin/ld: cannot find Scrt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc: No such file or directory
collect2: error: ld returned 1 exit status                                                               
configure: error: I suspect your system does not have 32-bit development libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bi
t-only compiler, rerun configure with --disable-multilib. 

glibc

作为奖励,如果你也对C:单个主机上的多个glibc库感兴趣

相关:

  • https://unix.stackexchange.com/questions/657370/how-to-compile-libstdc-with-specific-compiler-option
相关文章: