阅读更多
1 GNU
1.1 libbacktrace
1 | git clone https://github.com/ianlancetaylor/libbacktrace.git |
1 |
|
1 | gcc -o main main.cpp -lstdc++ -std=gnu++17 -lbacktrace -g |
1 | /data25/chenfeng.he/cpp/test/main.cpp:31: 0x4014d3 print_stack_trace() |
1.2 libunwind
1 | git clone https://github.com/libunwind/libunwind.git |
1 |
|
1 | # -DUNW_LOCAL_ONLY is mandatory, otherwise some link error may occur, like: |
1 | 0x401437: recursive(unsigned short) (+0x1a) |
1.2.1 How to automatically generate a stacktrace when my program crashes
How to automatically generate a stacktrace when my program crashes
2 boost
2.1 Installation
2.1.1 Package Manager
1 | yum install -y boost-devel |
2.1.2 From Source
1 | wget https://boostorg.jfrog.io/artifactory/main/release/1.84.0/source/boost_1_84_0.tar.gz |
2.2 Usage
1 | mkdir boost_demo |
2.3 Print Stack
Boost.Stacktrace provides several options for printing stack traces, depending on the underlying technology used to capture the stack information:
BOOST_STACKTRACE_USE_BACKTRACE
: uses thebacktrace
function from the GNU C Library, which is available on most UNIX-like systems including Linux.BOOST_STACKTRACE_USE_ADDR2LINE
: uses theaddr2line
utility from GNU binutils to convert addresses into file names and line numbers, providing more detailed information.BOOST_STACKTRACE_USE_NOOP
: doesn’t capture the stack trace at all. This can be used when you want to disable stack tracing completely.BOOST_STACKTRACE_USE_WINDBG
: utilizes the Windows Debug Help Library when compiling for Windows.
1 |
|
2.3.1 With addr2line
This approach works fine with gcc-10.3.0
, but can’t work with higher versions like gcc-11.3.0
, gcc-12.3.0
. Don’t know why so far.
Compile:
1 | # -ldl: link libdl |
Output:
1 | Boost version: 1.84.0 |
2.3.2 With libbacktrace
Compile:
1 | # -ldl: link libdl |
Output:
1 | Boost version: 1.84.0 |
2.4 Reference
- The Boost C++ Libraries BoostBook Documentation Subset
- How to print current call stack
- print call stack in C or C++
3 fmt
安装fmt
:
1 | git clone https://github.com/fmtlib/fmt.git |
在cmake
中添加fmt
依赖:
1 | find_package(fmt) |
示例:
1 |
|
gcc -o main main.cpp -lstdc++ -std=gnu++17 -lfmt
4 Facebook
4.1 folly
Prerequisites (These dependencies won’t be automatically installed by cachelib’s script):
boost
jemalloc
For Ubuntu 18.04, you may need the following dependencies:
1 | sudo apt install -y libdouble-conversion-dev libevent-dev liblz4-dev libdwarf-dev libsnappy-dev liblzma-dev libbz2-dev libunwind-dev libsodium-dev libnuma-dev libzstd-dev |
4.2 CacheLib
5 Google
5.1 gflag
安装gflag:
1 | git clone https://github.com/gflags/gflags.git |
在cmake
中添加gflags
依赖:
1 | find_package(gflags REQUIRED) |
示例:
1 |
|
gcc -o main main.cpp -lstdc++ -std=gnu++17 -lgflags -lpthread
./main --test_bool true --test_int32 100 --test_double 6.666 --test_str hello
5.2 glog
安装glog:
1 | git clone https://github.com/google/glog.git |
在cmake
中添加glog
依赖:
1 | find_package(GLOG) |
5.2.1 Print Stack
[Enhancement] wrap libc’s __cxa_throw to print stack trace when throw exceptions
1 |
|
编译:
glog
需要使用静态库版本,因为动态库版本选择隐藏DumpStackTraceToString
这个符号(readelf -s --wide /usr/lib64/libglog.so.0.7.0 | rg DumpStackTraceToString
)
1 | gcc -o main main.cpp -Wl,-wrap=__cxa_throw -lstdc++ -std=gnu++17 -Wl,-Bstatic -lglog -lgflags -Wl,-Bdynamic -lunwind -lpthread |
5.3 gtest
安装gtest:
1 | git clone https://github.com/google/googletest.git |
在cmake
中添加gtest
依赖:
1 | find_package(GTest REQUIRED) |
完整示例
1 | cat > CMakeLists.txt << 'EOF' |
5.3.1 Macros
-
TEST(test_case_name, test_name)
: Defines a test case.1
2
3TEST(TestCaseName, TestName) {
// Test logic here
} -
TEST_F(test_fixture, test_name)
: Defines a test case using a test fixture.1
2
3
4
5
6
7
8
9
10
11
12
13
14class MyTestFixture : public ::testing::Test {
protected:
void SetUp() override {
// Common setup logic for test cases
}
void TearDown() override {
// Common cleanup logic for test cases
}
};
TEST_F(MyTestFixture, TestName) {
// Test logic using the fixture environment
} -
EXPECT_EQ(expected, actual)
: Expects that two values are equal. -
ASSERT_EQ(expected, actual)
: Asserts that two values are equal. -
EXPECT_NE(val1, val2)
: Expects that two values are not equal. -
ASSERT_NE(val1, val2)
: Asserts that two values are not equal. -
EXPECT_LT(val1, val2)
: Expects that val1 is less than val2. -
ASSERT_LT(val1, val2)
: Asserts that val1 is less than val2. -
EXPECT_LE(val1, val2)
: Expects that val1 is less than or equal to val2. -
ASSERT_LE(val1, val2)
: Asserts that val1 is less than or equal to val2. -
EXPECT_GT(val1, val2)
: Expects that val1 is greater than val2. -
ASSERT_GT(val1, val2)
: Asserts that val1 is greater than val2. -
EXPECT_GE(val1, val2)
: Expects that val1 is greater than or equal to val2. -
ASSERT_GE(val1, val2)
: Asserts that val1 is greater than or equal to val2. -
EXPECT_TRUE(condition)
: Expects that a condition is true. -
ASSERT_TRUE(condition)
: Asserts that a condition is true. -
EXPECT_FALSE(condition)
: Expects that a condition is false. -
ASSERT_FALSE(condition)
: Asserts that a condition is false. -
EXPECT_STREQ(expected_str, actual_str)
: Expects that two C-style strings are equal. -
ASSERT_STREQ(expected_str, actual_str)
: Asserts that two C-style strings are equal. -
EXPECT_STRNE(str1, str2)
: Expects that two C-style strings are not equal. -
ASSERT_STRNE(str1, str2)
: Asserts that two C-style strings are not equal. -
EXPECT_THROW(statement, exception_type)
: Expects that a specific statement throws a particular exception. -
ASSERT_THROW(statement, exception_type)
: Asserts that a specific statement throws a particular exception.
5.3.2 Tips
- 假设编译得到的二进制是
test
,通过执行./test --help
就可以看到所有gtest支持的参数,包括执行特定case等等
5.4 benchmark
安装benchmark:
1 | git clone https://github.com/google/benchmark.git --depth 1 |
在cmake
中添加benchmark
依赖:
1 | find_package(benchmark REQUIRED) |
完整示例
1 | # 编写CMakeLists.txt |
输出如下:
Time
:每次迭代消耗的总时间,包括cpu时间+等待时间CPU
:每次迭代真正占用cpu的时间Iterations
:迭代次数
1 | ------------------------------------------------------------ |
5.4.1 quick-benchmark
5.4.2 Tips
5.4.2.1 benchmark::DoNotOptimize
避免优化本不应该优化的代码,其源码如下:
1 | inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) { |
5.4.2.2 Run Specific Case
使用参数--benchmark_filter=<regexp>
,此外可以使用--help
查看所有参数
5.4.3 Reference
5.5 gperftools/gperftools
5.6 snappy
Snappy is a compression/decompression library
6 Apache
6.1 arrow
- Building Arrow C++: Find all building Optional Components here
Requirement:
protobuf
1 | git clone -b apache-arrow-16.1.0 https://github.com/apache/arrow.git |
Build with llvm’s libc++
1 | cmake -B build --preset ninja-release -DPARQUET_REQUIRE_ENCRYPTION=ON \ |
6.1.1 Parquet Module
Related Docs:
Parquet demo without encryption:
1 |
|
1 | gcc -o arrow_parquet_demo arrow_parquet_demo.cpp -lstdc++ -std=gnu++17 -larrow -lparquet |
Parquet demo with encryption:
1 |
|
1 | gcc -o arrow_parquet_demo arrow_parquet_demo.cpp -lstdc++ -std=gnu++17 -larrow -lparquet |
6.2 thrift
Requirement:
libtool
bison
flex
openssl-devel
1 | git clone -b v0.16.0 https://github.com/apache/thrift.git |
1 | cat > example.thrift << 'EOF' |
7 Pocoproject
1 | git clone -b master https://github.com/pocoproject/poco.git |
7.1 Logger
1 | mkdir poco_logger_demo |
Output:
1 | Hello, World! |
7.2 JSON
1 | mkdir poco_json_demo |
Output:
1 | Name: John Doe, Age: 30, Is Developer: 1 |
8 sqlpp11
How to integrate:
1 |
|
How to create cpp header files:
1 | cat > /tmp/foo.sql << 'EOF' |
8.1 With Sqlite
1 | tree -L 2 |
1 | mkdir sqlpp11_demo && cd sqlpp11_demo |
1 | Sqlite3 debug: Preparing: 'CREATE TABLE users ( |
8.2 With Mysql
1 | tree -L 2 |
1 | mkdir sqlpp11_demo && cd sqlpp11_demo |
8.3 With Mariadb
1 | tree -L 2 |
1 | mkdir sqlpp11_demo && cd sqlpp11_demo |
You may be presented with the following error messages:
1 | CMake Error at contrib/mariadb-connector-c/cmake/install_plugins.cmake:11 (INSTALL): |
Just remove the line 83(contrib/mariadb-connector-c/cmake/plugins.cmake:83
), which is
1 | INSTALL_PLUGIN(${CC_PLUGIN_TARGET} ${CMAKE_CURRENT_BINARY_DIR}) |
And then compile again:
1 | cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && cmake --build build -j $(( (cores=$(nproc))>1?cores/2:1 )) |
1 | INSERT INTO users (id,first_name,last_name,age) VALUES(10000001,'Emma','Watson',15) ON DUPLICATE KEY UPDATE first_name='Emma', last_name='Watson', age=15 |
9 Assorted
- Awesome C++ Projects
- parallel-hashmap:
parallel-hashmap
提供了一组高性能、并发安全的map
,用于替换std
以及boost
中的map
- cpp-httplib:
cpp-httplib
以头文件的方式提供http
协议的相关支持 - json:
json
库 - libfiu(Failure Injection Unit):错误注入