[英]How to edit and re-build the GCC libstdc++ C++ standard library source?
I am working on some research and would like to edit some of the source code in the libstdc++ library for experimentation. 我正在进行一些研究,并希望编辑libstdc ++库中的一些源代码进行实验。 I am, specifically, interested in experimenting with the parallel sorting algorithms.
具体而言,我对试验并行排序算法感兴趣。 Is there a place I can find documentation to easily edit and build the source code?
有没有一个地方我可以找到文档来轻松编辑和构建源代码?
I have tried, unsuccessfully, to build various versions of the libstdc++ library. 我试过构建各种版本的libstdc ++库,但没有成功。 It seems like most new versions require building the entire gcc package, which is a much more lengthy process, especially if I am going to be editing and experimenting with a few files in libstdc++.
似乎大多数新版本都需要构建整个gcc包,这是一个更漫长的过程,特别是如果我要编辑和试验libstdc ++中的一些文件。
I have also been unable to find the source files that contain the parallel sorting algorithms. 我也一直无法找到包含并行排序算法的源文件。 I can only seem to find the header files that define the functions, and not the source code itself.
我似乎只能找到定义函数的头文件,而不是源代码本身。 Any advice or links to documentation would be greatly appreciated.
任何建议或文档链接将不胜感激。
Yes, you have to build the whole of GCC, but once you've done that you only need to rebuild the libstdc++ part. 是的,你必须构建整个GCC,但是一旦你完成了,你只需要重建libstdc ++部分。
Building GCC is described at http://gcc.gnu.org/wiki/InstallingGCC 建立GCC的方法见http://gcc.gnu.org/wiki/InstallingGCC
The libstdc++ sources are in the libstdc++-v3
directory. libstdc ++源代码位于
libstdc++-v3
目录中。 The parallel algorithms are in libstdc++-v3/include/parallel
, they are templates so all the code is in headers. 并行算法在
libstdc++-v3/include/parallel
,它们是模板,所以所有代码都在头文件中。 The small amount of non-header code is in libstdc++-v3/src/c++98/parallel-settings.cc
少量的非标头代码在
libstdc++-v3/src/c++98/parallel-settings.cc
To rebuild libstdc++ from the top-level build dir go into the $TARGET/libstdc++-v3
directory (where $TARGET
is something like x86_64-unknown-linux-gnu
) and run make
. 要从顶级构建目录重建libstdc ++,请进入
$TARGET/libstdc++-v3
目录(其中$TARGET
类似于x86_64-unknown-linux-gnu
)并运行make
。
Minimal step-by-step example 最小的一步一步的例子
Compile GCC from source . 从源代码编译GCC 。 Condensed commands:
压缩命令:
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`
Wait from 30-minutes to two hours. 等待30分钟到两个小时。 Now let's use this test program
a.cpp
: 现在让我们使用这个测试程序
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();
}
First compile and run it to ensure that the initial compilation worked: 首先编译并运行它以确保初始编译工作:
gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out
Now let's hack up the priority_queue
constructor. 现在让我们破解
priority_queue
构造函数。
First, you can find the actual constructor easily with GDB as explained at: When should I use make_heap vs. Priority Queue? 首先,您可以使用GDB轻松找到实际的构造函数,如下所述: 我何时应该使用make_heap与Priority Queue?
So we hack it up with this patch: 所以我们用这个补丁搞定了:
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
Then rebuild and re-install just libstdc++ to save a lot of time: 然后重建并重新安装libstdc ++以节省大量时间:
cd gcc/build/x86_64-pc-linux-gnu/libstdc++-v3
make -j`nproc`
make install
and now the next build and run: 现在是下一个构建和运行:
gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out
outputs: 输出:
hacked
Tested on Ubuntu 16.04. 在Ubuntu 16.04上测试过。
glibc glibc的
As a bonus, if you are also interested in C: Multiple glibc libraries on a single host 作为奖励,如果您对C: 单个主机上的多个glibc库感兴趣
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.