如何编辑和重新构建GCC libstdc++C++标准库源代码
How to edit and re-build the GCC libstdc++ C++ standard library source?
我正在进行一些研究,并希望编辑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
- g++ libstdc++.so.6:从 4.8.5 升级到 GCC 版本 7.3.0 后找不到版本 'CXXABI_1.3.9'
- GCC (libstdc++) 运行时如何在异常处于活动状态时决定终止 ()
- libstdc++的make_shared布局在gcc 4.x和gcc 6.x之间是否发生了变化?
- 在CentOS 7上安装GCC 7.4.0不会更新我的LIBSTDC 版本
- -static-libstdc++ 适用于 g++ 但不适用于纯 gcc?
- 如何修复lib libstdc++的"std::variant"(来自GCC 9.1)不适用于Clang 8?
- GLIBCXX(libstdc++.so.6)与gcc版本之间的关系
- 在同一系统上有多个GCC的情况下,使用哪个版本的libstdc++.so
- ./lib/gcc/x86_64-linux-gnu/4.6/libstdc++.a 的用法是什么
- 如何编辑和重新构建GCC libstdc++C++标准库源代码
- 如何在使用旧版本gcc的系统上动态链接到libc.so.6、libstdc++.so.6的本地副本
- libstdc++-v3中的GCC源代码修改未显示
- libstdc++是来自系统还是gcc或gcc-c++
- GCC 和 libstdc++ 向前兼容
- 为什么在 gcc/libstdc++ 的__enable_shared_from_this_helper中没有模板参数
- 与使用 GCC 3.4.6 (libstdc++.so.6.0.13) 生成的 gmock 库链接会产生"undefined reference to... @GLIBCXX_3.4.9"错误
- 我的“交换”过载应该被使用吗?这是一个libstdc++(GCC)错误吗
- 如何链接GCC中的libc和libstdc++的调试版本
- 在使用旧版本gcc / glibc / libstdc++的计算机上运行用gcc 4.7编译的c++ 11可执行文件的问
- 从源代码构建GCC 5.4;这样构建的可执行文件试图链接不兼容的libstdc++,但运行失败