g++代码修改未被gdb/链接器识别的技术求助
环境信息
- Win 11-64 Pro v24H2
- Cygwin 3.7.0-0.116
- g++ 15.1.1
- gdb 15.2
编译、链接与调试命令
编译目标文件:
g++ -c -g -std=c++11 -o obj/SlipReader.o SlipReader.cpp
生成DLL及导入库:
g++ -shared -o cygslip.dll \ -Wl,--out-implib=libslip.dll.a \ -Wl,--export-all-symbols \ -Wl,--enable-auto-import *.o
生成测试可执行文件:
g++ /usr/local/lib/cygslip.dll *.o -o SlipTest.exe
启动调试:
gdb SlipTest.exe
代码场景说明
SlipReader.h第622行声明SlipReaderCell* reader;用于后续调用,针对currentCell()函数有四种代码场景:
- 场景1(原代码):SlipReader.h第716行
存在无限递归问题。SlipCell& currentCell() const {return currentCell(); } - 场景2:修改SlipReader.h第716行
SlipCell& currentCell() const {return reader->currentCell(); } - 场景3:在SlipReader.h中插入空行/注释后,上述修改代码位于第719行,内容与场景2一致。
- 场景4:将函数实现移至cpp文件
- 头文件SlipReader.h第718行仅声明:
SlipCell& currentCell() const; - 实现文件SlipReader.cpp第67行:
SlipCell& currentCell() const {return reader->currentCell(); }
- 头文件SlipReader.h第718行仅声明:
问题现象
- 场景1、2、3:gdb调试时始终显示场景1的无限递归代码,完全不识别场景2、3的修改,最终因递归调用失败。
- 场景4:触发链接器错误:
错误信息存在多处异常:/usr/lib/gcc/x86_64-pc-cygwin/15/../../../../x86_64-pc-cygwin/bin/ld: SlipReader.o: in function `std::allocator<char>::~allocator()': /cygdrive/e/home/skidmarks/Projects/SLIP/gSlip/SlipReader.cpp:67: multiple definition of `slip::SlipReader::currentCell() const'; SlipHeader.o:/cygdrive/e/home/skidmarks/Projects/SLIP/gSlip/SlipReader.h:716: first defined here- 代码中未使用
std::allocator - SlipReader.h第716行是函数声明而非定义
- SlipHeader.o并未参与当前编译链接流程
- 错误行号与实际代码位置不符
- 代码中未使用
已执行重新编译、链接操作,但调试信息及错误提示的行号始终未更新,求解决办法。
内容的提问来源于stack exchange,提问作者lostbits
相关产品推荐
相关产品推荐

