我正在进行一些研究,并希望编辑libstdc++库中的一些源代码以进行实验。 我特别感兴趣的是并行排序算法的实验。 是否有一个地方可以找到文档,以便轻松地编辑和构建源代码?
我尝试过构建各种版本的libstdc++库,但都没有成功。似乎大多数新版本都需要构建整个gcc包,这是一个更加冗长的过程,尤其是如果我要编辑和对libstdc++中的几个文件进行实验。
我还找不到包含并行排序算法的源文件。 我只能找到定义函数的头文件,而不是源代码本身。 如有任何建议或文档链接,将不胜感激。
我正在进行一些研究,并希望编辑libstdc++库中的一些源代码以进行实验。 我特别感兴趣的是并行排序算法的实验。 是否有一个地方可以找到文档,以便轻松地编辑和构建源代码?
我尝试过构建各种版本的libstdc++库,但都没有成功。似乎大多数新版本都需要构建整个gcc包,这是一个更加冗长的过程,尤其是如果我要编辑和对libstdc++中的几个文件进行实验。
我还找不到包含并行排序算法的源文件。 我只能找到定义函数的头文件,而不是源代码本身。 如有任何建议或文档链接,将不胜感激。
简化的逐步示例
从源代码编译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
构造函数。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
中添加--disable-multilib
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库在单个主机上的使用
相关:
是的,您需要构建整个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
才能重新编译代码。
x86_64-unknown-linux-gnu
(确切的目标取决于您的主机和gcc版本)。 - Jonathan Wakelybits/libc-header-start.h
属于glibc而不是GCC。如果找不到它,你根本无法构建GCC。听起来你的问题只是首次构建GCC,而不是仅重新构建libstdc++部分。你需要解决这个问题,这不是一个libstdc++的问题。 - Jonathan Wakely