I want to compile libstdc++ with the -fno-omit-frame-pointer
option for profiling purposes, if possible. I was able to build libstdc++ by following https://gcc.gnu.org/install/index.html, but how can I set the option? The CXXFLAGS
approach did not work.
2 Answers
Hints https://www.linuxfromscratch.org/lfs/view/stable/chapter06/gcc-pass2.html
A test-build ....
tar xvf gcc-10.3.0.tar.xz
mkdir BUILD__libstdc++103
cd BUILD__libstdc++103/
../gcc-10.3.0/libstdc++-v3/configure \
CXXFLAGS="-g -O2 -D_GNU_SOURCE -fno-omit-frame-pointer" \
--prefix=/home/knudfl/BUILD__libstdc++103/usr \
--disable-multilib --disable-libstdcxx-pch
make
make install
Seems to be OK : The text -fno-omit-frame-pointer
is visible in the terminal make
output.
Minimal step-by-step example
Compile GCC from source. 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. Now let's use this test program 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.
First, you can find the actual constructor easily with GDB as explained at: https://stackoverflow.com/questions/11266360/when-should-i-use-make-heap-vs-priority-queue/51945521#51945521
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:
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.
glibc
As a bonus, if you are also interested in C: https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/52454603#52454603
Related: