我的环境是
以下程序按预期工作(主要执行没有错误,返回0)
#include <iostream>
#include <string>
using namespace std;
//#define USE_STRING 1
int
main(int argc, char *argv[])
{
cout << "begin " << endl;
#ifdef USE_STRING
string w("fish");
#endif
cout << "blah " << endl;
return 0;
}
但是如果我取消注释#define USE_STRING,程序会崩溃,甚至可以在main中命中断点。
以下是我的编译方式:
g++ -ggdb -v my_string.cc -o my_string
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-5.2.0-1.x86_64/src/gcc-5.2.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-5.2. 0-1.x86_64/src/gcc-5.2.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --ht mldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-pr efix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-speci fic-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran, lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm --enable-libquadma th --enable-libquadmath-support --enable-libssp --enable-libada --enable-libgcj-sublibs --disable-java-awt --disable-symvers --wi th-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-pre fix --without-libintl-prefix --with-system-zlib --enable-linker-build-id
Thread model: posix
gcc version 5.2.0 (GCC)
COLLECT_GCC_OPTIONS='-ggdb' '-v' '-o' 'my_string.exe' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/cc1plus.exe -quiet -v -iprefix /bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/ -Dunix -idirafter /usr/lib/../lib/../include/w32api -idirafter ../../include/w32api my_string.cc -quiet -dumpbase my_string.cc -mtune=generic -marc h=x86-64 -auxbase my_string -ggdb -version -o /tmp/cc6aWK4I.s
GNU C++ (GCC) version 5.2.0 (x86_64-pc-cygwin)
compiled by GNU C version 5.2.0, GMP version 6.0.0, MPFR version 3.1.3, MPC version 1.0.3
warning: GMP header version 6.0.0 differs from library version 6.1.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/../../../../x86_64-pc-cygwin/include"
ignoring duplicate directory "/bin/../lib/gcc/../../lib/gcc/x86_64-pc-cygwin/5.2.0/include/c++"
ignoring duplicate directory "/bin/../lib/gcc/../../lib/gcc/x86_64-pc-cygwin/5.2.0/include/c++/x86_64-pc-cygwin"
ignoring duplicate directory "/bin/../lib/gcc/../../lib/gcc/x86_64-pc-cygwin/5.2.0/include/c++/backward"
ignoring duplicate directory "/bin/../lib/gcc/../../lib/gcc/x86_64-pc-cygwin/5.2.0/include"
ignoring nonexistent directory "/usr/local/include"
ignoring duplicate directory "/bin/../lib/gcc/../../lib/gcc/x86_64-pc-cygwin/5.2.0/include-fixed"
ignoring nonexistent directory "/bin/../lib/gcc/../../lib/gcc/x86_64-pc-cygwin/5.2.0/../../../../x86_64-pc-cygwin/include"
ignoring nonexistent directory "../../include/w32api"
#include "..." search starts here:
#include <...> search starts here:
/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/include/c++
/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/include/c++/x86_64-pc-cygwin
/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/include/c++/backward
/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/include
/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/include-fixed
/usr/include
/usr/lib/../lib/../include/w32api
End of search list.
GNU C++ (GCC) version 5.2.0 (x86_64-pc-cygwin)
compiled by GNU C version 5.2.0, GMP version 6.0.0, MPFR version 3.1.3, MPC version 1.0.3
warning: GMP header version 6.0.0 differs from library version 6.1.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 594d57f36425b84b161aac261544e04e
COLLECT_GCC_OPTIONS='-ggdb' '-v' '-o' 'my_string.exe' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
as -v -o /tmp/ccEdjvZ0.o /tmp/cc6aWK4I.s
GNU assembler version 2.25.2 (x86_64-pc-cygwin) using BFD version (GNU Binutils) 2.25.2
COMPILER_PATH=/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/:/bin/../lib/gcc/
LIBRARY_PATH=/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/:/bin/../lib/gcc/:/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/../../../../lib/:/li b/../lib/:/usr/lib/../lib/:/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-ggdb' '-v' '-o' 'my_string.exe' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/collect2.exe -plugin /bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/cyglto_plugin.dll -plugin-opt =/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/lto-wrapper.exe -plugin-opt=-fresolution=/tmp/cc6lfDyO.res -plugin-opt=-pass-through=-lgc c_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lcygwin -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-thr ough=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lgcc_s -plugin- opt=-pass-through=-lgcc --build-id -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZdlPv --wrap _ZdaPv --wrap _ZnwmRKSt9nothrow_t -- wrap _ZnamRKSt9nothrow_t --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPvRKSt9nothrow_t -Bdynamic --dll-search-prefix=cyg --tsaware -o m y_string.exe -L/usr/lib/w32api /bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/../../../../lib/crt0.o /bin/../lib/gcc/x86_64-pc-cygwin/5.2 .0/crtbegin.o -L/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0 -L/bin/../lib/gcc -L/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/../../.. /tmp/ccEdjvZ0.o -lstdc++ -lgcc_s -lgcc -lcygwi n -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc_s -lgcc /bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/../../../../lib/default-manifest. o /bin/../lib/gcc/x86_64-pc-cygwin/5.2.0/crtend.o
并且gdb中的失败案例如下所示:
$ gdb ./my_string
GNU gdb (GDB) Cygwin 7.9.1-1
Copyright (C) 2015 Free Software Foundation, Inc.
...
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./my_string...done.
(gdb) b main
Breakpoint 1 at 0x1004010fa: file my_string.cc, line 10.
(gdb) run
Starting program: /home/Admin/progs/c++/word_bubbles/my_string
[New Thread 107280.0x1a15c]
[New Thread 107280.0x1a2ac]
[New Thread 107280.0x1a050]
[New Thread 107280.0x1a3f4]
[Thread 107280.0x1a3f4 exited with code 3221225785]
[Thread 107280.0x1a2ac exited with code 3221225785]
[Thread 107280.0x1a050 exited with code 3221225785]
During startup program exited with code 0xc0000139.
(gdb)
最后动态链接如下所示:
$ ldd my_string
ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x7ffef9820000)
KERNEL32.DLL => /cygdrive/c/Windows/system32/KERNEL32.DLL (0x7ffef9370000)
KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x7ffef6d10000)
我知道编译器输出有点冗长,但我正在尝试提供诊断问题所需的一切。
编辑1:根据建议,这里是strace输出:
$ strace ./my_string.exe
--- Process 105768 created
--- Process 105768 loaded C:\Windows\System32\ntdll.dll at 00007FFEF9820000
--- Process 105768 loaded C:\Windows\System32\kernel32.dll at 00007FFEF9370000
--- Process 105768 loaded C:\Windows\System32\KernelBase.dll at 00007FFEF6D10000
--- Process 105768 thread 107956 created
--- Process 105768 thread 56196 created
--- Process 105768 thread 92192 created
--- Process 105768 loaded E:\cygwin64\bin\cyggcc_s-seh-1.dll at 00000003F4260000
--- Process 105768 loaded E:\cygwin64\bin\cygstdc++-6.dll at 00000003EDF70000
--- Process 105768 loaded E:\cygwin64\bin\cygwin1.dll at 0000000180040000
--- Process 105768, exception c0000139 at 00007FFEF98B2F50
--- Process 105768 thread 92192 exited with status 0xc0000139
--- Process 105768 thread 105388 exited with status 0xc0000139
--- Process 105768 thread 56196 exited with status 0xc0000139
--- Process 105768 exited with status 0xc0000139
进程异常似乎是在一个Windows DLL地址范围(ntdll.dll)中?
编辑2:使用strace,弹出以下error message window
my_string.exe entry point not found
The entry point
_ZNSt7__cxx1112basic_stringlcSt11char_traitslcESalcEEC1EPKcRKS3_ could
not be located in the dynamic link library.
我安装了Anaconda Python发行版,在成功的程序文件中,我看到了Anaconda路径的一些行,所以我暂时重命名了Anaconda目录,以确保重新编译&amp;重新运行不会发现Anaconda但是没有效果。