可持续性踩坑的交叉编译 gcc ./configure 环境变量 及其他

可能踩到的坑

  1. 用成上位机的编译器
  2. 库和头文件被上位机的库和头文件污染
  3. pkg-config搜索到上位机的库和文件 给引用了进去
  4. 存在依赖的so库 又依赖了别的so的库的情况。编译器直接就傻了。

有用的解决方法

去装一个编译器

Arm交叉编译linaro会比较流行。去搜一下 到他们网上下载一个。
根据host和target机来选就行。
arm的soc一般就选 aarch64…linux。 host是 x86-64 linux 。

仔细看看./configure -h

吃了亏才知道,这玩意里面的 有影响的环境变量不是闹着玩的。 在开始configure之前先老老实实配好。

例如:

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  LIBS        libraries to pass to the linker, e.g. -l<library>
  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  CPP         C preprocessor
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags
  PYTHON      the Python interpreter
  LT_SYS_LIBRARY_PATH
              User-defined run-time library search path.
  CXXCPP      C++ preprocessor
  PKG_CONFIG  path to pkg-config utility
  PKG_CONFIG_PATH
              directories to add to pkg-config's search path
  PKG_CONFIG_LIBDIR
              path overriding pkg-config's built-in search path
  URCU_CFLAGS C compiler flags for URCU, overriding pkg-config
  URCU_LIBS   linker flags for URCU, overriding pkg-config
  CLASSPATH   Java class path

Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.

特别是在CFLAGSLDFLAGS PKG_CONFIG_LIBDIR这样的东西。

配置环境变量和./configure 来make 和 install

./configure --prefix=想要编译产出install的根目录 --host=aarch64-none-linux-gnu 
--with-sysroot=交叉编译器目录/aarch64-none-linux-gnu/libc

host 后面跟的是交叉编译器的前缀。注意最后没有-。另外需要把编译器这一大堆bin文件的路径export到PATH里去。

--with-sysroot我觉得可能是和gcc 的–sysroot一样。设了这个编译器就在这个root下面找/usr/lib和/usr/include。不会用到上位机根目录下的库和头文件了。

CFLAGSLDFLAGS PKG_CONFIG_LIBDIR这些东西统统指向生成目录里的/lib和/include

-I -L指定头文件和库(除了所谓的rootsystem 的文件)

pkg-config设置地址PKG_CONFIG_LIBDIR到产出的/lib/pkgconfig的文件夹下面 屏蔽上位机的文件。防止Makefile里面搞事情,搜到上位机的库引用进来。

-Wl,-rpath=或者-Wl,rpath-linker=用于so引用so的情况。因为没法在交叉环境用ldconfig所以只能这么干。
前面一个会把路径写入生成bin文件,后一个不会,只在连接时用。

现在还面临的问题

现在遇到一个奇葩问题,交叉编译的时候,先编译完的库在自己的/.lib这种隐藏文件夹下。没有install到我的目录,后面自己编译别的库依赖自己。傻了。找不到前面的库。搞得链接的时候报错找不到代码终端referenced。 离谱。
按理说最后还是要通过-Wl,rpath-linker=解决。目前还没解决

猜你喜欢

转载自blog.csdn.net/kuno_y/article/details/127538632