$ 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