C and Cpp


  1. read kraken hash.k2d taxo.k2d opts.k2d
  2. X11与Wayland的区别
  3. GTK、Qt、SDL、Dear ImGui 可以在linux上打开窗口是对X11库的封装吗
  4. c语言在linux上打开窗口
  5. c 语言使用glibc 或 musl 的例子
  6. 函数指针 int (*fp)(int a, int b);
  7. debug 比对软件 bwa
  8. gdb调试c语言
  9. pkgconfig
  10. 关于编译器

$ g++ -pthread -std=c++11 -O2 -Wall -o main main.cpp 
$ ./main 
Hello Concurrent World
$ ldd main 
        linux-vdso.so.1 (0x00007ffc02faf000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff31142a000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ff31140f000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff3113ec000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff3111f8000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff3110a9000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff31162d000)
  • C++11 的 std::thread 在 Linux 上底层依赖 POSIX Threads(pthreads)。

  • 虽然 g++ 会自动链接 C++ 标准库(libstdc++),但 不会自动链接 libpthread。

  • 因此,当你使用 std::thread、std::mutex 等多线程功能时,必须显式告诉链接器链接 pthread 库。

  • -O2:启用中高级别的代码优化

  • -Wall:启用“所有常见且有用的警告”

  • https://nj.gitbooks.io/c/content/

你贴的 Makefile 片段里,build_db 目标的编译命令已经包含了 -g(生成调试信息)和 -O0(关闭优化),这是调试的基础。下面我给你详细讲怎么 debug 这个 build_db 程序:


1️⃣ 确认编译带调试信息

你当前的编译命令:

g++ -Wall -std=c++11 -g -O0 -fPIC -DLINEAR_PROBING -o build_db $^
  • -g → 生成调试信息。
  • -O0 → 禁用优化,方便调试。
  • -Wall → 打开所有警告,推荐保留。

✅ 这已经是调试友好的编译方式。


2️⃣ 使用 gdb 调试

  1. 编译:
make clean
make build_db
  1. 启动 gdb:
gdb ./build_db
  1. 在 gdb 里可以设置断点,比如 build_db.cpp 文件的 main 函数:
break main
run
  1. 常用 gdb 命令:
  • next(n):执行下一行,不进入函数。
  • step(s):执行下一行,进入函数内部。
  • print var:打印变量。
  • backtrace(bt):打印调用栈。
  • continue(c):继续运行直到下一个断点。
  • list(l):显示源码附近行。
  1. 退出 gdb:
quit

3️⃣ 如果想 debug crash / segfault

可以先开启 core dump:

ulimit -c unlimited
./build_db <args>

如果程序崩溃,会生成 core 文件,然后:

gdb ./build_db core

然后用 bt 查看调用栈。