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

12

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

我尝试过构建各种版本的libstdc++库,但都没有成功。似乎大多数新版本都需要构建整个gcc包,这是一个更加冗长的过程,尤其是如果我要编辑和对libstdc++中的几个文件进行实验。

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

2个回答

10

简化的逐步示例

从源代码编译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说明找到实际的构造函数: When should I use 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

我们需要执行以下操作之一:

否则会出现以下错误:

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库在单个主机上的使用

相关:


10

是的,您需要构建整个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目录(其中$TARGET类似于x86_64-pc-linux-gnu)中运行make来重新构建libstdc++。

默认情况下,Makefile没有适当的依赖关系,导致在更改头文件后重新构建对象,因此您可能需要执行make clean,然后再次执行make才能重新编译代码。


@automorphic对我来说运行良好,使用Make 4.3,但我怀疑这并没有什么区别。我每天都会执行几次,每周都会执行。哪里出了问题?也许你没有注意到我说过类似于x86_64-unknown-linux-gnu(确切的目标取决于您的主机和gcc版本)。 - Jonathan Wakely
谢谢 - 我肯定是错过了什么。我再次下载了源代码(这次是 GCC 11.02),但仍然在 libstdc++-v3 子目录中找不到 Makefile,至少在 ARM7(raspi4)上是这样。也许是因为构建没有完成?(某些时候找不到 bits/libc-header-start.h)。 - automorphic
1
@automorphic,makefile是由“configure”步骤创建的,您仍然需要运行该步骤。请参见我的答案中的链接,不要直接跳到最后一步并只执行我提到的最后一件事。那是关于重建它的,所以只有在您已经按照正常方式构建了它之后才相关。看看链接!我已经编辑了答案,使其更清晰明了。 - Jonathan Wakely
1
另外,bits/libc-header-start.h属于glibc而不是GCC。如果找不到它,你根本无法构建GCC。听起来你的问题只是首次构建GCC,而不是仅重新构建libstdc++部分。你需要解决这个问题,这不是一个libstdc++的问题。 - Jonathan Wakely
1
@automorphic 那个文件是 glibc 的一部分,没有它你无法编译任何东西。这不是 GCC 的问题。 - Jonathan Wakely
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接