Windows 下64位TDM-GCC-64 g++自定义库的生成及使用

一、自定义库的生成

生成szlib.o文件:

$ g++ -c szlib.cpp

生成libszlib.a文件:

$ 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++

三、自定义库文件的使用方法二:使用-L -l选项

p204-5.cpp中要使用库libszlib.a,通过头文件szlib.h

编译问题:找不到自定义库中的函数

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 依赖顺序问题

这个主要的原因是gcc编译的时候,各个文件依赖顺序的问题。

在gcc编译的时候,如果文件a依赖于文件b,那么编译的时候必须把a放前面,b放后面。

例如:在main.c中使用了pthread库相关函数,那么编译的时候必须是main.c在前,-lpthread在后。gcc main.c -lpthread -o a.out。

上面出现问题的原因就是引入库的顺序在前面了,将其放置在后面即可了。

解决方法:

$ gcc  -o p204-5 p204-5.cpp -L./lib -lszlib

问题: undefined reference to `std::ios_base::Init::Init()


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++

 

 

环境:Windows 2016 cmd

$ 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)

 

 

 

参考:

undefined reference to `std::ios_base::Init::Init()

https://blog.csdn.net/kittaaron/article/details/8101391

 

2012年10月23日 09:31:01 kittaaron 阅读数:4147
 

用gcc(C编译器)编译C++程序,会报标题的错误。

原因是用gcc编译c++程序时,链接的库文件为libstdc++.so,而不是默认的libc.so,因此需要用-lstdc++参数指明,否则会在链接时发生错误.

如: gcc helloworld.cpp -lstdc++


gcc编译时对'xxxx'未定义的引用问题

http://www.linuxdiyf.com/linux/16754.html

转载于:https://my.oschina.net/SamXIAO/blog/3008690

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值