$ g++ -c szlib.cpp
$ ar crv libszlib.a szlib.o a - szlib.o
$ nm lib\*.a szlib.o: 0000000000000000 b .bss 0000000000000000 d .ctors 0000000000000000 d .data 0000000000000000 p .pdata 0000000000000000 r .rdata 0000000000000000 r .rdata$zzz 0000000000000000 t .text 0000000000000000 r .xdata U __mingw_vprintf 00000000000004ad t __tcf_0 0000000000000504 t _GLOBAL__sub_I__Z15StopWatch_startv 00000000000000a8 T _Z10SZSW_startv 00000000000001ce T _Z11PrintfArrayPii 0000000000000379 T _Z11sort_bubblePiib 000000000000022d T _Z12FillArrayRndPii 0000000000000061 T _Z14StopWatch_stopv 0000000000000041 T _Z15StopWatch_startv 00000000000004c8 t _Z41__static_initialization_and_destruction_0ii 0000000000000106 T _Z4swapRiS_ 000000000000013b T _Z5swap2PiS_ 0000000000000170 T _Z7verinfoPKcS0_ 000000000000028c T _Z8sort_selPii 00000000000000c2 T _Z9SZSW_stopv 0000000000000010 b _ZL10szsw_start 0000000000000008 b _ZL23StopWatch_seconds_start 0000000000000000 t _ZL6printfPKcz U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev 0000000000000000 b _ZStL8__ioinit U atexit U clock U difftime U rand U srand U time
$ gcc -o p204-5 p204-5.cpp ./lib/libszlib.a -lstdc++
p204-5.cpp中要使用库libszlib.a,经过头文件szlib.hhtml
D:\exp $ gcc -L./lib -lszlib -o p204-5 p204-5.cpp C:\Users\ADMINI~1\AppData\Local\Temp\ccpVKqoM.o:p204-5.cpp:(.text+0x71d): undefined reference to `verinfo(char const*, char const*)' C:\Users\ADMINI~1\AppData\Local\Temp\ccpVKqoM.o:p204-5.cpp:(.text+0x722): undefined reference to `SZSW_start()' C:\Users\ADMINI~1\AppData\Local\Temp\ccpVKqoM.o:p204-5.cpp:(.text+0x74a): undefined reference to `SZSW_stop()' C:\Users\ADMINI~1\AppData\Local\Temp\ccpVKqoM.o:p204-5.cpp:(.text+0x793): undefined reference to `std::ios_base::Init::~Init()' C:\Users\ADMINI~1\AppData\Local\Temp\ccpVKqoM.o:p204-5.cpp:(.text+0x7c3): undefined reference to `std::ios_base::Init::Init()' collect2.exe: error: ld returned 1 exit status
gcc 依赖顺序问题linux
这个主要的缘由是gcc编译的时候,各个文件依赖顺序的问题。ios
在gcc编译的时候,若是文件a依赖于文件b,那么编译的时候必须把a放前面,b放后面。c++
例如:在main.c中使用了pthread库相关函数,那么编译的时候必须是main.c在前,-lpthread在后。gcc main.c -lpthread -o a.out。bash
上面出现问题的缘由就是引入库的顺序在前面了,将其放置在后面便可了。app
$ gcc -o p204-5 p204-5.cpp -L./lib -lszlib
D:\exp $ gcc -o p204-5 p204-5.cpp -L./lib -lszlib C:\Users\ADMINI~1\AppData\Local\Temp\cc93nev7.o:p204-5.cpp:(.text+0x793): undefined reference to `std::ios_base::Init::~Init()' C:\Users\ADMINI~1\AppData\Local\Temp\cc93nev7.o:p204-5.cpp:(.text+0x7c3): undefined reference to `std::ios_base::Init::Init()' ./lib/libszlib.a(szlib.o):szlib.cpp:(.text+0x4bd): undefined reference to `std::ios_base::Init::~Init()' ./lib/libszlib.a(szlib.o):szlib.cpp:(.text+0x4ed): undefined reference to `std::ios_base::Init::Init()' collect2.exe: error: ld returned 1 exit status
D:\exp $ gcc -o p204-5 p204-5.cpp -L./lib -lszlib -lstdc++
$ ver Microsoft Windows [版本 10.0.14393] $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=P:/TDM-GCC-64/bin/../libexec/gcc/x86_64-w64-mingw32/5.1.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../../../src/gcc-5.1.0/configure --build=x86_64-w64-mingw32 --enable-targets=all --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-cxx-flags=-DWINPTHREAD_STATIC --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --prefix=/mingw64tdm --with-local-prefix=/mingw64tdm --with-pkgversion=tdm64-1 --with-bugurl=http://tdm-gcc.tdragon.net/bugs Thread model: posix gcc version 5.1.0 (tdm64-1)
https://blog.csdn.net/kittaaron/article/details/8101391函数
用gcc(C编译器)编译C++程序,会报标题的错误。post
缘由是用gcc编译c++程序时,连接的库文件为libstdc++.so,而不是默认的libc.so,所以须要用-lstdc++参数指明,不然会在连接时发生错误.ui
如: gcc helloworld.cpp -lstdc++url
http://www.linuxdiyf.com/linux/16754.html