GVKun编程网logo

推迟C静态对象构建 – Linux上的GCC(c++静态对象)

14

在这篇文章中,我们将带领您了解推迟C静态对象构建–Linux上的GCC的全貌,包括c++静态对象的相关情况。同时,我们还将为您介绍有关arm-linux-gcc静态编译和动态编译的区别、linuxce

在这篇文章中,我们将带领您了解推迟C静态对象构建 – Linux上的GCC的全貌,包括c++静态对象的相关情况。同时,我们还将为您介绍有关arm-linux-gcc静态编译和动态编译的区别、linux centos7 使用 crosstool-ng 构建 交叉编译 工具链 即构建各cpu架构平台的gcc编译器、linux gcc 宏 显示 (linux c中判断gcc版本)、linux gcc 链接静态库的几种方式的知识,以帮助您更好地理解这个主题。

本文目录一览:

推迟C静态对象构建 – Linux上的GCC(c++静态对象)

推迟C静态对象构建 – Linux上的GCC(c++静态对象)

想象一下,我有一个名为MyClass的C类.

想象一下,我无法访问MyClass的源代码……它包含在一个库中,我只提供了库和MyClass的头文件.

想象一下,类本身需要环境预配置……例如……在调用类的构造函数之前,我需要做一些设置.该类通常用于如下:

void func() {
   doGlobalSetup();
   MyClass myInstance(1,2,3);
   myInstance.doSomething();
   ...
}

现在我遇到了需要创建类的全局实例的情况,例如:

MyClass myInstance(1,3);

int main(int argc,char *argv[]) {
   doGlobalSetup();
   myInstance.doSomething();
}

问题是在这个故事中,MyClass的实例是在调用doGlobalSetup()之前创建的.它在调用main()之前被实例化.我想要做的是将myInstance()的创建推迟到以后,或者能够在类的实例化之前以某种方式运行doGlobalSetup().

这是对实际故事的简化……所以让我们假设:

>我无法改变MyClass的内部.
>必须有一个名为myInstance的MyClass类型的实例变量(我无法将逻辑更改为MyClass * pMyInstance).

非常感谢阅读.

解决方法

因为你已经限制了这个问题,所以不能使用new.您应该能够始终创建对象并将其复制到全局实例.例如:
MyClass createMyClass()
{
    doGlobalSetup();
    return MyClass(1,3);
}

MyClass myInstance = createMyClass();

int main()
{
    myInstance.doSomething();

    return 0;
}

arm-linux-gcc静态编译和动态编译的区别

arm-linux-gcc静态编译和动态编译的区别

 

很多教程会提到加上-static是静态编译,但对于新手来说没有用例子来说明可能不太好理解,今天我就介绍一下关于这方面知识的一个例子:

最近在做一个关于freetype字体的东西,需要依赖freetype官方提供的库,我已经把电脑这边的环境配置好了,如下图,我分别用-static和不用-static编译出了两个可执行程序,用-static编译出来的文件明显大得多,因为它已经把该程序依赖的一些库文件链接到该文件里面去了,在ARM开发板上面运行该程序时不需要依赖库了,而不加-static的运行时则需要依赖的库。

root@ubuntu:/home/linuxsystemcode/04th_print_info# arm-none-linux-gnueabi-gcc -finput-charset=GBK -o example1_static example1.c  -lfreetype -lm -static
root@ubuntu:/home/linuxsystemcode/04th_print_info# 
root@ubuntu:/home/linuxsystemcode/04th_print_info# 
root@ubuntu:/home/linuxsystemcode/04th_print_info# ls
example1.c  example1_static  simsun.ttc
root@ubuntu:/home/linuxsystemcode/04th_print_info# arm-none-linux-gnueabi-gcc -finput-charset=GBK -o example1 example1.c  -lfreetype -lm
root@ubuntu:/home/linuxsystemcode/04th_print_info# ls
example1  example1.c  example1_static  simsun.ttc
root@ubuntu:/home/linuxsystemcode/04th_print_info# ls -l
total 12728
-rwxr-xr-x 1 root root    13481 Aug 16 22:54 example1
-rw-r--r-- 1 root root     4698 Aug 15 19:57 example1.c
-rwxr-xr-x 1 root root  2491106 Aug 16 22:53 example1_static
-rw-r--r-- 1 root root 10512288 Jul 11 00:18 simsun.ttc

 静态编译的程序能够直接在开发板上面运行,而动态编译的则不行

[root@iTOP-4412]# ./example1_static ./simsun.ttc
Uniocde:
0x97e6 0x67 0x69 0x66
Unicode: 0x97e6
 ./example1 ./simsun.ttc
./example1: error while loading shared libraries: libfreetype.so.6: cannot open shared object file: No such file or directory

如果需要在开发板上运行该程序则需要把该程序依赖的库拷贝到开发板的/lib目录下,如下图:

[root@iTOP-4412]# cp /mnt/udisk/lib/* /lib/ -rf -d
[root@iTOP-4412]# ./example1 ./simsun.ttc
Uniocde:
0x97e6 0x67 0x69 0x66
Unicode: 0x97e6

这些依赖文件需要到我们的交叉编译工具链里面找,我的是在/usr/local/arm/arm-2009q3/arm-none-linux-gnueabi/libc/lib下,不同的环境下路径会不一样,根据实际情况判断。

 

linux centos7 使用 crosstool-ng 构建 交叉编译 工具链 即构建各cpu架构平台的gcc编译器

linux centos7 使用 crosstool-ng 构建 交叉编译 工具链 即构建各cpu架构平台的gcc编译器

简介

crosstool-ng,全称是crosstool Next Generation,即下一代crosstool。crosstool是个交叉编译器的制作工具,但是做的不够好,于是有人(Yann E. MORIN)弄出了个更好的——crosstool-ng。crosstool-ng的特点:

支持menuconfig(类似于Linux内核配置)

支持众多的架构

可选多种不同的C库等模块

提供示例配置

支持多种主机编译环境:各种Linux发行版,Cygwin等

安装

#yum安装需要的包
yum install -y gperf bison flex texinfo help2man gcc-c++ expat-devel patch ncurses-devel automake libstdc++-static


#下载crosstool-ng并源码编译 设置软连接
wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.23.0.tar.xz
tar -Jvxf crosstool-ng-1.23.0.tar.xz
cd crosstool-ng-1.23.0
./configure --prefix=/usr/local/crosstool-ng
make
make install
ln -s  /usr/local/crosstool-ng/bin/ct-ng /usr/local/bin/ct-ng

制作交叉编译工具链

1.使用ct-ng list-samples命令查看具有哪些默认配置。具体如下:

[G..]   aarch64-rpi3-linux-gnueabi
[G..]   aarch64-unkNown-linux-gnueabi
[G..]   aarch64-unkNown-linux-uclibcgnueabi
[G..]   alphaev56-unkNown-linux-gnu
[G..]   alphaev67-unkNown-linux-gnu
[G..]   arm-bare_newlib_cortex_m3_nommu-eabi
[G..]   arm-cortex_a15-linux-gnueabihf
[G.X]   arm-cortexa5-linux-uclibcgnueabihf
[G..]   arm-cortex_a8-linux-gnueabi
[G.X]   arm-cortexa9_neon-linux-gnueabihf
[G.X]   x86_64-w64-mingw32,arm-cortexa9_neon-linux-gnueabihf
[G..]   armeb-unkNown-eabi
[G..]   armeb-unkNown-linux-gnueabi
[G..]   armeb-unkNown-linux-uclibcgnueabi
[G..]   arm-multilib-linux-uclibcgnueabi
[G..]   arm-nano-eabi
[G..]   arm-unkNown-eabi
[G..]   arm-unkNown-linux-gnueabi
[G.X]   arm-unkNown-linux-musleabi
[G..]   arm-unkNown-linux-uclibcgnueabi
[G.X]   arm-unkNown-linux-uclibcgnueabihf
[G..]   armv6-nommu-linux-uclibcgnueabi
[G..]   armv6-rpi-linux-gnueabi
[G..]   armv7-rpi2-linux-gnueabihf
[G..]   armv8-rpi3-linux-gnueabihf
[G..]   avr
[G..]   i586-geode-linux-uclibc
[G..]   i686-centos6-linux-gnu
[G..]   i686-centos7-linux-gnu
[G..]   i686-nptl-linux-gnu
[G..]   i686-ubuntu12.04-linux-gnu
[G..]   i686-ubuntu14.04-linux-gnu
[G..]   i686-ubuntu16.04-linux-gnu
[G.X]   i686-w64-mingw32
[G..]   m68k-unkNown-elf
[G..]   m68k-unkNown-uclinux-uclibc
[G..]   powerpc-unkNown-linux-uclibc,m68k-unkNown-uclinux-uclibc
[G..]   mips64el-multilib-linux-uclibc
[G..]   mips-ar2315-linux-gnu
[G..]   mipsel-multilib-linux-gnu
[G..]   mipsel-sde-elf
[G..]   mipsel-unkNown-linux-gnu
[G..]   mips-malta-linux-gnu
[G..]   mips-unkNown-elf
[G..]   mips-unkNown-linux-uclibc
[G.X]   i686-w64-mingw32,nios2-spico-elf
[G..]   powerpc-405-linux-gnu
[G..]   powerpc64le-unkNown-linux-gnu
[G..]   powerpc64-multilib-linux-gnu
[G..]   powerpc64-unkNown-linux-gnu
[G..]   powerpc-860-linux-gnu
[G..]   powerpc-e300c3-linux-gnu
[G..]   powerpc-e500v2-linux-gnuspe
[G..]   x86_64-multilib-linux-uclibc,powerpc-unkNown-elf
[G..]   powerpc-unkNown-linux-gnu
[G..]   powerpc-unkNown-linux-uclibc
[G..]   powerpc-unkNown_nofpu-linux-gnu
[G.X]   s390-ibm-linux-gnu
[G..]   s390x-ibm-linux-gnu
[G..]   sh4-multilib-linux-gnu
[G..]   sh4-multilib-linux-uclibc
[G..]   sh4-unkNown-linux-gnu
[G..]   sparc64-multilib-linux-gnu
[G..]   sparc-leon-linux-uclibc
[G..]   sparc-unkNown-linux-gnu
[G..]   x86_64-centos6-linux-gnu
[G..]   x86_64-centos7-linux-gnu
[G..]   x86_64-multilib-linux-gnu
[G.X]   x86_64-multilib-linux-musl
[G..]   x86_64-multilib-linux-uclibc
[G.X]   x86_64-w64-mingw32,x86_64-pc-linux-gnu
[G..]   x86_64-ubuntu12.04-linux-gnu
[G..]   x86_64-ubuntu14.04-linux-gnu
[G..]   x86_64-ubuntu16.04-linux-gnu
[G..]   x86_64-unkNown-linux-gnu
[G..]   x86_64-unkNown-linux-uclibc
[G.X]   x86_64-w64-mingw32
[G..]   xtensa-fsf-linux-uclibc

列表中名词解析参考:crosstool-ng 交叉工具链默认配置 名词解释  crosstool-ng 交叉工具链默认配置 名词解释_whatday的专栏-CSDN博客

2.以其中的 arm-unkNown-linux-gnueabi 配置为基础,进行一些自定义配置:必须是非root用户 这里以test用户为示例

cd /home/test
mkdir -p crosstool-ng_build/src crosstool-ng_build/arm-unkNown-linux-gnueabi
# 注意 /home/test/crosstool-ng_lib/ 里是 crosstool-ng 交叉编译需要的依赖库
cp /home/test/crosstool-ng_lib/* /home/test/crosstool-ng_build/src
cd crosstool-ng_build/arm-unkNown-linux-gnueabi/
ct-ng arm-unkNown-linux-gnueabi
ct-ng menuconfig

弹出配置对话框 修改 Paths and misc options --->(${HOME}/src) Local tarballs directory :指定下载的源码保存的位置,根据自己情况修改为:/home/test/crosstool-ng_build/src

选中​ Forbid downloads:不从网络下载需要的源码包 因为自动下载速度太慢 自己在windows用迅雷下载更快些

Target options  --->Emit assembly for cpu 设置cpu内核 

例如 armv4可设置为arm920t    armv5可设置为arm1022e   armv6可设置为arm1136j-s

具体可参考:

ARM架构和ARM核区别和联系   ARM架构和ARM核区别和联系_whatday的专栏-CSDN博客_arm核

crosstool-ng配置中的Architecture level、Emit assembly for cpu 、Tune for cpu  crosstool-ng配置中的Architecture level、Emit assembly for CPU 、Tune for CPU_whatday的专栏-CSDN博客

选择GCC版本:C compiler  ---> gcc version (6.3.0)   

Operating System  --->Linux kernel version (3.19.8 (EOL))  ---> 

这个项是选择内核版本的 当然选择后在构建工具链时 需要提供相应的内核源码包

C-library  ---> Minimum supported kernel version (Same as kernel headers (default))  --->

使用gcc编译后的程序  能够运行的最低的内核版本 

假如gcc内核版本选择的3.19.8 系统环境是3.10.0 此时运行该gcc编译的程序会报错:FATAL: kernel too old

就是因为这个选项造成的  这里修改为 Let ./configure decide  就不会有这个问题了

3.使用ct-ng build命令开始制作交叉工具编译链。

这是使用root权限会提示错误[ERROR]  You must NOT be root to run crosstool-ng 

用普通用户运行 ct-ng build 因为没有需求的源码包且配置了不允许网络自动下载 所以会出现下列错误

[ERROR]  >>  Build Failed in step 'Retrieving needed toolchain components' tarballs'
[ERROR]  >>        called in step '(top-level)'

cat build.log可以看到需求的源码包名

[DEBUG]    Trying to retrieve an already downloaded copy of 'autoconf-2.69'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.xz'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.lzma'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.bz2'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.gz'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tgz'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.zip'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69'
[DEBUG]    Not allowed to download from the Internet,aborting autoconf-2.69 download

在windows中google直接搜索autoconf-2.69.tar.xz 迅雷下载 传到centos7的${HOME}/crosstool-ng_build/src目录下 上 再次 ct-ng build 依次循环下载各个需要的包 列表如下:

[root@bogon crosstool-ng_build]# ll src/
total 283444
-rwxrwxrwx. 1 root root  1214744 Apr 24  2012 autoconf-2.69.tar.xz
-rwxrwxrwx. 1 root root  1496708 Feb 10 09:09 automake-1.15.tar.xz
-rwxrwxrwx. 1 root root 36228252 Feb 10 09:47 binutils-2.28.tar.gz
-rwxrwxrwx. 1 root root   240479 Feb 10 09:52 duma_2_5_15.tar.gz
-rwxrwxrwx. 1 root root   414352 Feb 10 10:22 expat-2.2.0.tar.bz2
-rwxrwxrwx. 1 root root 99903185 Feb 10 09:49 gcc-6.3.0.tar.bz2
-rwxrwxrwx. 1 root root 19225392 Feb 10 09:53 gdb-7.12.1.tar.xz
-rwxrwxrwx. 1 root root  7209808 Feb 10 09:44 gettext-0.19.8.1.tar.xz
-rwxrwxrwx. 1 root root 13873900 Feb 10 09:50 glibc-2.25.tar.xz
-rwxrwxrwx. 1 root root  1946336 Feb 10 09:22 gmp-6.1.2.tar.xz
-rwxrwxrwx. 1 root root  1449164 Feb 10 09:25 isl-0.16.1.tar.xz
-rwxrwxrwx. 1 root root   148529 Feb 10 09:26 libelf-0.8.13.tar.gz
-rwxrwxrwx. 1 root root  5264188 Feb 10 09:42 libiconv-1.15.tar.gz
-rwxrwxrwx. 1 root root   973080 Feb 16  2015 libtool-2.4.6.tar.xz
-rwxrwxrwx. 1 root root 94235192 Feb 10 09:19 linux-4.10.8.tar.xz
-rwxrwxrwx. 1 root root   482658 Feb 10 09:55 ltrace_0.7.3.orig.tar.bz2
-rwxrwxrwx. 1 root root   669925 Feb 10 09:25 mpc-1.0.3.tar.gz
-rwxrwxrwx. 1 root root  1126668 Feb 10 09:24 mpfr-3.1.5.tar.xz
-rwxrwxrwx. 1 root root  3131891 Feb 10 09:40 ncurses-6.0.tar.gz
-rwxrwxrwx. 1 root root   966668 Feb 10 09:57 strace-4.16.tar.xz

设置权限 chmod 777 -R ${HOME}/crosstool-ng_build/src 再次ct-ng build 经过一个小时的等待会出现完成提示 如下

[INFO ]  =================================================================
[INFO ]  Finalizing the toolchain's directory
[INFO ]    Stripping all toolchain executables
[EXTRA]    Installing the populate helper
[EXTRA]    Installing a cross-ldd helper
[EXTRA]    Creating toolchain aliases
[INFO ]  Finalizing the toolchain's directory: done in 1.51s (at 56:57)
[INFO ]  Build completed at 20190210.230611
[INFO ]  (elapsed: 56:56.82)
[INFO ]  Finishing installation (may take a few seconds)...
[56:57] / [test@bogon arm-unkNown-linux-gnueabi]$ 

完成后,交叉工具编译链的位置在/home/tools/${CT_TARGET}目录下

[root@bogon bin]# pwd
/home/test/x-tools/arm-unkNown-linux-gnueabi/bin
[root@bogon bin]# ll
total 30256
-r-xr-xr-x. 1 test test  841072 Feb 11 01:56 arm-unkNown-linux-gnueabi-addr2line
-r-xr-xr-x. 2 test test  869656 Feb 11 01:56 arm-unkNown-linux-gnueabi-ar
-r-xr-xr-x. 2 test test 1451520 Feb 11 01:56 arm-unkNown-linux-gnueabi-as
-r-xr-xr-x. 2 test test  817280 Feb 11 01:56 arm-unkNown-linux-gnueabi-c++
lrwxrwxrwx. 1 test test      29 Feb 11 01:46 arm-unkNown-linux-gnueabi-cc -> arm-unkNown-linux-gnueabi-gcc
-r-xr-xr-x. 1 test test  836528 Feb 11 01:56 arm-unkNown-linux-gnueabi-c++filt
-r-xr-xr-x. 1 test test  817280 Feb 11 01:56 arm-unkNown-linux-gnueabi-cpp
-r-xr-xr-x. 1 test test    3177 Feb 11 00:58 arm-unkNown-linux-gnueabi-ct-ng.config
-r-xr-xr-x. 1 test test 2457632 Feb 11 01:56 arm-unkNown-linux-gnueabi-dwp
-r-xr-xr-x. 1 test test   31552 Feb 11 01:56 arm-unkNown-linux-gnueabi-elfedit
-r-xr-xr-x. 2 test test  817280 Feb 11 01:56 arm-unkNown-linux-gnueabi-g++
-r-xr-xr-x. 2 test test  813184 Feb 11 01:56 arm-unkNown-linux-gnueabi-gcc
-r-xr-xr-x. 2 test test  813184 Feb 11 01:56 arm-unkNown-linux-gnueabi-gcc-6.3.0
-r-xr-xr-x. 1 test test   27152 Feb 11 01:56 arm-unkNown-linux-gnueabi-gcc-ar
-r-xr-xr-x. 1 test test   27152 Feb 11 01:56 arm-unkNown-linux-gnueabi-gcc-nm
-r-xr-xr-x. 1 test test   27152 Feb 11 01:56 arm-unkNown-linux-gnueabi-gcc-ranlib
-r-xr-xr-x. 1 test test  429344 Feb 11 01:56 arm-unkNown-linux-gnueabi-gcov
-r-xr-xr-x. 1 test test  388408 Feb 11 01:56 arm-unkNown-linux-gnueabi-gcov-tool
-r-xr-xr-x. 1 test test 5295208 Feb 11 01:56 arm-unkNown-linux-gnueabi-gdb
-r-xr-xr-x. 1 test test  903760 Feb 11 01:56 arm-unkNown-linux-gnueabi-gprof
-r-xr-xr-x. 1 test test     143 Feb 11 01:07 arm-unkNown-linux-gnueabi-ld
-r-xr-xr-x. 2 test test 1350528 Feb 11 01:56 arm-unkNown-linux-gnueabi-ld.bfd
-r-xr-xr-x. 1 test test   15078 Feb 11 01:56 arm-unkNown-linux-gnueabi-ldd
-r-xr-xr-x. 2 test test 4584576 Feb 11 01:56 arm-unkNown-linux-gnueabi-ld.gold
-r-xr-xr-x. 2 test test  854064 Feb 11 01:56 arm-unkNown-linux-gnueabi-nm
-r-xr-xr-x. 2 test test 1033232 Feb 11 01:56 arm-unkNown-linux-gnueabi-objcopy
-r-xr-xr-x. 2 test test 1308880 Feb 11 01:56 arm-unkNown-linux-gnueabi-objdump
-r-xr-xr-x. 1 test test   10448 Feb 11 01:56 arm-unkNown-linux-gnueabi-populate
-r-xr-xr-x. 2 test test  869656 Feb 11 01:56 arm-unkNown-linux-gnueabi-ranlib
-r-xr-xr-x. 2 test test  505760 Feb 11 01:56 arm-unkNown-linux-gnueabi-readelf
-r-xr-xr-x. 1 test test  840912 Feb 11 01:56 arm-unkNown-linux-gnueabi-size
-r-xr-xr-x. 1 test test  841072 Feb 11 01:56 arm-unkNown-linux-gnueabi-strings
-r-xr-xr-x. 2 test test 1033232 Feb 11 01:56 arm-unkNown-linux-gnueabi-strip
[root@bogon bin]# 

工具详细解释参考: Binutils工具集 GCC工具集介绍  Binutils工具集 GCC工具集介绍_whatday的专栏-CSDN博客_gcc工具集

出现以下信息说明交叉编译工具链有效

[root@bogon bin]# ./arm-unkNown-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=./arm-unkNown-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/home/test/x-tools/arm-unkNown-linux-gnueabi/libexec/gcc/arm-unkNown-linux-gnueabi/6.3.0/lto-wrapper
Target: arm-unkNown-linux-gnueabi
Configured with: /home/test/Desktop/crosstool-ng_build/arm-unkNown-linux-gnueabi/.build/src/gcc-6.3.0/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-unkNown-linux-gnueabi --prefix=/home/test/x-tools/arm-unkNown-linux-gnueabi --with-sysroot=/home/test/x-tools/arm-unkNown-linux-gnueabi/arm-unkNown-linux-gnueabi/sysroot --enable-languages=c,c++ --with-float=soft --with-pkgversion='crosstool-ng crosstool-ng-1.23.0' --disable-sjlj-exceptions --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --disable-libmpx --with-gmp=/home/test/Desktop/crosstool-ng_build/arm-unkNown-linux-gnueabi/.build/arm-unkNown-linux-gnueabi/buildtools --with-mpfr=/home/test/Desktop/crosstool-ng_build/arm-unkNown-linux-gnueabi/.build/arm-unkNown-linux-gnueabi/buildtools --with-mpc=/home/test/Desktop/crosstool-ng_build/arm-unkNown-linux-gnueabi/.build/arm-unkNown-linux-gnueabi/buildtools --with-isl=/home/test/Desktop/crosstool-ng_build/arm-unkNown-linux-gnueabi/.build/arm-unkNown-linux-gnueabi/buildtools --enable-lto --enable-threads=posix --enable-target-optspace --enable-plugin --enable-gold --disable-nls --disable-multilib --with-local-prefix=/home/test/x-tools/arm-unkNown-linux-gnueabi/arm-unkNown-linux-gnueabi/sysroot --enable-long-long
Thread model: posix
gcc version 6.3.0 (crosstool-ng crosstool-ng-1.23.0) 
[root@bogon bin]# 


标准化安装交叉编译器

由于标准Makefile需要实用标准的交叉编译器的名称,一般这个名称是arm-linux-gcc这样的  vim link.sh

#!/bin/sh
PREFIX=arm-unkNown-linux-gnueabi- 
AFTFIX=arm-linux-
ln -s ${PREFIX}gcc ${AFTFIX}gcc
ln -s ${PREFIX}addr2line ${AFTFIX}addr2line
ln -s  ${PREFIX}gdbtui ${AFTFIX}gdbtui
ln -s  ${PREFIX}ar ${AFTFIX}ar
ln -s  ${PREFIX}as ${AFTFIX}as
ln -s  ${PREFIX}c++ ${AFTFIX}c++
ln -s  ${PREFIX}c++filt ${AFTFIX}c++filt
ln -s  ${PREFIX}cpp ${AFTFIX}cpp
ln -s  ${PREFIX}g++ ${AFTFIX}g++
ln -s  ${PREFIX}gccbug ${AFTFIX}gccbug
ln -s  ${PREFIX}gcj ${AFTFIX}gcj
ln -s  ${PREFIX}gcov ${AFTFIX}gcov
ln -s  ${PREFIX}gdb ${AFTFIX}gdb
ln -s  ${PREFIX}gfortran ${AFTFIX}gfortran
ln -s  ${PREFIX}gprof ${AFTFIX}gprof
ln -s  ${PREFIX}jcf-dump ${AFTFIX}jcf-dump
ln -s  ${PREFIX}ld ${AFTFIX}ld
ln -s  ${PREFIX}ldd ${AFTFIX}ldd
ln -s  ${PREFIX}nm ${AFTFIX}nm
ln -s  ${PREFIX}objcopy ${AFTFIX}objcopy
ln -s  ${PREFIX}objdump ${AFTFIX}objdump
ln -s  ${PREFIX}populate ${AFTFIX}populate
ln -s  ${PREFIX}ranlib ${AFTFIX}ranlib
ln -s  ${PREFIX}readelf ${AFTFIX}readelf
ln -s  ${PREFIX}run ${AFTFIX}run
ln -s  ${PREFIX}size ${AFTFIX}size
ln -s  ${PREFIX}strings ${AFTFIX}strings
ln -s  ${PREFIX}strip ${AFTFIX}strip

保存并执行  这样我们就得到了整个使用标准名称的交叉编译工具链

linux gcc 宏 显示 (linux c中判断gcc版本)

linux gcc 宏 显示 (linux c中判断gcc版本)

 

root@test-desktop:~# gcc -E -dM - </dev/null
#define __DBL_MIN_EXP__ (-1021)
#define __FLT_MIN__ 1.17549435e-38F
#define __CHAR_BIT__ 8
#define __WCHAR_MAX__ 2147483647
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
#define __DBL_DEnorM_MIN__ 4.9406564584124654e-324
#define __FLT_EVAL_METHOD__ 2
#define __unix__ 1
#define __DBL_MIN_10_EXP__ (-307)
#define __FINITE_MATH_ONLY__ 0
#define __GNUC_PATCHLEVEL__ 3
#define __DEC64_MAX_EXP__ 385
#define __SHRT_MAX__ 32767
#define __LDBL_MAX__ 1.18973149535723176502e+4932L
#define __UINTMAX_TYPE__ long long unsigned int
#define __linux 1
#define __DEC32_EPSILON__ 1E-6DF
#define __unix 1
#define __LDBL_MAX_EXP__ 16384
#define __linux__ 1
#define __SCHAR_MAX__ 127
#define __DBL_DIG__ 15
#define _FORTIFY_SOURCE 2
#define __SIZEOF_INT__ 4
#define __SIZEOF_POINTER__ 4
#define __USER_LABEL_PREFIX__ 
#define __STDC_HOSTED__ 1
#define __LDBL_HAS_INFINITY__ 1
#define __FLT_EPSILON__ 1.19209290e-7F
#define __LDBL_MIN__ 3.36210314311209350626e-4932L
#define __DEC32_MAX__ 9.999999E96DF
#define __SIZEOF_LONG__ 4
#define __DECIMAL_DIG__ 21
#define __gnu_linux__ 1
#define __LDBL_HAS_QUIET_NAN__ 1
#define __GNUC__ 4
#define __FLT_HAS_DEnorM__ 1
#define __SIZEOF_LONG_DOUBLE__ 12
#define __BIGGEST_ALIGNMENT__ 16
#define __DBL_MAX__ 1.7976931348623157e+308
#define __DBL_HAS_INFINITY__ 1
#define __DEC32_MIN_EXP__ (-94)
#define __LDBL_HAS_DEnorM__ 1
#define __DEC128_MAX__ 9.999999999999999999999999999999999E6144DL
#define __DEC32_MIN__ 1E-95DF
#define __DBL_MAX_EXP__ 1024
#define __DEC128_EPSILON__ 1E-33DL
#define __LONG_LONG_MAX__ 9223372036854775807LL
#define __SIZEOF_SIZE_T__ 4
#define __SIZEOF_WINT_T__ 4
#define __GXX_ABI_VERSION 1002
#define __FLT_MIN_EXP__ (-125)
#define __DBL_MIN__ 2.2250738585072014e-308
#define __DECIMAL_BID_FORMAT__ 1
#define __DEC128_MIN__ 1E-6143DL
#define __REGISTER_PREFIX__ 
#define __DBL_HAS_DEnorM__ 1
#define __NO_INLINE__ 1
#define __i386 1
#define __FLT_MANT_DIG__ 24
#define __VERSION__ "4.4.3"
#define __DEC64_EPSILON__ 1E-15DD
#define __DEC128_MIN_EXP__ (-6142)
#define __i486__ 1
#define unix 1
#define __i386__ 1
#define __SIZE_TYPE__ unsigned int
#define __ELF__ 1
#define __FLT_RADIX__ 2
#define __LDBL_EPSILON__ 1.08420217248550443401e-19L
#define __SIZEOF_PTRDIFF_T__ 4
#define __DEC32_SUBnorMAL_MIN__ 0.000001E-95DF
#define __FLT_HAS_QUIET_NAN__ 1
#define __FLT_MAX_10_EXP__ 38
#define __LONG_MAX__ 2147483647L
#define __DEC128_SUBnorMAL_MIN__ 0.000000000000000000000000000000001E-6143DL
#define __FLT_HAS_INFINITY__ 1
#define __DEC64_MAX__ 9.999999999999999E384DD
#define __CHAR16_TYPE__ short unsigned int
#define __DEC64_MANT_DIG__ 16
#define __DEC32_MAX_EXP__ 97
#define linux 1
#define __LDBL_MANT_DIG__ 64
#define __DBL_HAS_QUIET_NAN__ 1
#define __WCHAR_TYPE__ int
#define __SIZEOF_FLOAT__ 4
#define __DEC64_MIN_EXP__ (-382)
#define __FLT_DIG__ 6
#define __INT_MAX__ 2147483647
#define __i486 1
#define __FLT_MAX_EXP__ 128
#define __DBL_MANT_DIG__ 53
#define __DEC64_MIN__ 1E-383DD
#define __WINT_TYPE__ unsigned int
#define __SIZEOF_SHORT__ 2
#define __LDBL_MIN_EXP__ (-16381)
#define __ssp__ 1
#define __LDBL_MAX_10_EXP__ 4932
#define __DBL_EPSILON__ 2.2204460492503131e-16
#define __SIZEOF_WCHAR_T__ 4
#define __DEC_EVAL_METHOD__ 2
#define __INTMAX_MAX__ 9223372036854775807LL
#define __FLT_DEnorM_MIN__ 1.40129846e-45F
#define __CHAR32_TYPE__ unsigned int
#define __FLT_MAX__ 3.40282347e+38F
#define __SIZEOF_DOUBLE__ 8
#define __FLT_MIN_10_EXP__ (-37)
#define __INTMAX_TYPE__ long long int
#define i386 1
#define __DEC128_MAX_EXP__ 6145
#define __GNUC_MInor__ 4
#define __DEC32_MANT_DIG__ 7
#define __DBL_MAX_10_EXP__ 308
#define __LDBL_DEnorM_MIN__ 3.64519953188247460253e-4951L
#define __STDC__ 1
#define __PTRDIFF_TYPE__ int
#define __DEC64_SUBnorMAL_MIN__ 0.000000000000001E-383DD
#define __DEC128_MANT_DIG__ 34
#define __LDBL_MIN_10_EXP__ (-4931)
#define __SIZEOF_LONG_LONG__ 8
#define __LDBL_DIG__ 18
#define __GNUC_GNU_INLINE__ 1

用处实例: 在linux c中 可以用来判断gcc版本 比如这里是  #define __i386 1

32位 64位cpu判断

#if __SIZEOF_POINTER__ == 4
        /* 32位系统 */
#elif __SIZEOF_POINTER__ == 8
       /* 64位系统 */
#endif
 

linux gcc 链接静态库的几种方式

linux gcc 链接静态库的几种方式

开发一个应用程序不可避免要使用多个第三方库(library).
默认情况下,gcc采用动态连接的方式连接第三方库,比如指定-lpng,连接程序就会去找libpng.so

gcc提供了一个-static参数,可以改变gcc默认的连接方式,GNU官网上关于gcc连接选项的手册《3.14 Options for Linking》中有说明:如下

文件,完成静态连接,如果找不到就报错了。这里指的所有是不仅指我们常用的第三方库比如jpeg,png,opencv,zlib,...,还包括gcc编译器自带的库libgcc,libstdc++,libc,libm...,总之就是linux kernal之外的所有库。而且还要包括所有被间接引用的第三方库,比如png这个库在编译时还用到了zlib,那么静态连接png的时候,就要带上zlib的库:-lpng -lz,

这可麻烦大了,要把这些东西全静态连接,这得有多大?呵呵,这事儿我干过,十几兆字节总是有的,取决你的程序用到多少第三方库。
这种全静态连接有啥用处呢?也有用,就是你的程序自带干粮,只需要一个linux kernal就能跑了。

但是实际应用中,我们绝大多数应用场景不需要这么做,即使在嵌入式系统中也不一定必要,尤其是嵌入式系统的存储容量受限,这么一只大象装都装不下。所以我们大多数情况下需要有选择的进行静态编译,-static并不适合。

 

如何有选择的进行静态编译呢?

最简单的方式直接在连接参数中以全路径指定连接库就好了: your/path/lib<name>.a
但这种形式对管理结构简单而且自己写Makefile的小型项目还好,当一个项目结构复杂,有时需要静态连接有时需要动态连接,这种频繁的修改编译脚本的方式,可维护性就太差了。

 

-Bstatic

gnu的连接程序ld提供了一个-Bstatic选项用于对指定的库静态连接,ld的官方手册《2.1 Command Line Options》有说明,如下:

搜索libpng.a

-Bstatic -lpng -lz

 

-l:filename

如果你觉得上面一种静态连接方式不适合你,可以看看ld的官方手册《2.1 Command Line Options》中关于-l参数的说明,如下:

一个文件名,连接程序直接去找这个文件名了,不会再像使用-lname时将name扩展成lib<name>.a格式的文件名.
所以使用 -l:libpng.a这样的形式来指定连接库,就指定了静态连接png库。
当然如果库的位置不在gcc默认搜索路径中,要用-L参数另外指定搜索库的路径,否则连接程序不知道该从哪里找到filename

-L/your/library/path -l:libmylib.a

顺便贴出ld的官方手册《2.1 Command Line Options》中关于-L参数的说明

参考资料

以上内容只是我对最近做项目编译的一些总结和体会,可能会有所遗漏,权威资料还请参见gcc的官方文档
《3.14 Options for Linking》
《2.1 Command Line Options》

 

 

今天关于推迟C静态对象构建 – Linux上的GCCc++静态对象的介绍到此结束,谢谢您的阅读,有关arm-linux-gcc静态编译和动态编译的区别、linux centos7 使用 crosstool-ng 构建 交叉编译 工具链 即构建各cpu架构平台的gcc编译器、linux gcc 宏 显示 (linux c中判断gcc版本)、linux gcc 链接静态库的几种方式等更多相关知识的信息可以在本站进行查询。

本文标签: