Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

关于源码编译的基础知识 via LinuxSir (下篇) #7

Open
JackieMium opened this issue Mar 8, 2018 · 0 comments
Open

关于源码编译的基础知识 via LinuxSir (下篇) #7

JackieMium opened this issue Mar 8, 2018 · 0 comments
Labels
Code Talk is chip, show me the code Linux Linux related 基础 很基础的东西
Milestone

Comments

@JackieMium
Copy link
Owner

2017-05-18 21:41:38

首先说下**/etc/ld.so.conf**:
这个文件记录了编译时使用的动态链接库的路径。
默认情况下,编译器只会使用/lib/usr/lib这两个目录下的库文件
如果你安装了某些库,比如在安装gtk+-2.4.13时它会需要glib-2.0 >= 2.4.0, 辛苦的安装好glib后
没有指定--prefix=/usr这样glib库就装到了/usr/local下,而又没有在/etc/ld.so.conf中添加/usr/local/lib
这个搜索路径,所以编译gtk+-2.4.13就会出错了
对于这种情况有两种方法解决:

  1. 在编译glib-2.4.x时,指定安装到/usr下,这样库文件就会放在/usr/lib中,gtk就不会找不到需要的库文件了
    对于安装库文件来说,这是个好办法,这样也不用设置PKG_CONFIG_PATH了 (稍后说明)
  2. /usr/local/lib加入到/etc/ld.so.conf中,这样安装gtk时就会去搜索/usr/local/lib, 同样可以找到需要的库
    /usr/local/lib加入到/etc/ld.so.conf也是必须的,这样以后安装东东到local下,就不会出现这样的问题了。
    将自己可能存放库文件的路径都加入到/etc/ld.so.conf中是明智的选择

再来看看**ldconfig**是个什么东东吧 :
它是一个程序,通常它位于/sbin下,是root用户使用的东东。具体作用及用法可以man ldconfig查到
简单的说,它的作用就是将/etc/ld.so.conf列出的路径下的库文件 缓存到/etc/ld.so.cache以供使用
因此当安装完一些库文件,(例如刚安装好glib),或者修改ld.so.conf增加新的库路径后,需要运行一下/sbin/ldconfig使所有的库文件都被缓存到ld.so.cache中,如果没做,即使库文件明明就在/usr/lib下的,也是不会被使用的,结果 编译过程中抱错,缺少xxx库,去查看发现明明就在那放着 。所以
切记改动库文件后一定要运行一下ldconfig,在任何目录下运行都可以。

再来说说**PKG_CONFIG_PATH**这个变量吧:

经常在论坛上看到有人问"为什么我已经安装了glib-2.4.x, 但是编译gtk+-2.4.x还是提示glib版本太低阿?
为什么我安装了glib-2.4.x,还是提示找不到阿?。。。。。。"都是这个变量搞的鬼。
先来看一个编译过程中出现的错误 (编译gtk+-2.4.13):

checking for pkg-config... /usr/bin/pkg-config checking for glib-2.0 >= 2.4.0 atk >= 1.0.1 pango >= 1.4.0... Package glib-2.0 was not found in the pkg-config search path. 
Perhaps you should add the directory containing `glib-2.0.pc' to the PKG_CONFIG_PATH environment  variable
No package 'glib-2.0' found 

configure: error: Library requirements (glib-2.0 >= 2.4.0 atk >= 1.0.1 pango >= 1.4.0) not met; consider adjusting the PKG_CONFIG_PATH environment variable if your libraries are in a nonstandard prefix so pkg-config can find them. 
[root@NEWLFS gtk+-2.4.13]# 

很明显,上面这段说明,没有找到glib-2.4.x, 并且提示应该将glib-2.0.pc加入到PKG_CONFIG_PATH下。
究竟这个pkg-config目录PKG_CONFIG_PATH变量glib-2.0.pc文件 是做什么的呢?
先说说它是哪冒出来的,当安装了pkgconfig-x.x.x这个包后,就多出了pkg-config,它就是需要PKG_CONFIG_PATH的东东
来看一段说明:

The pkgconfig package contains tools for passing the include path and/or library paths to build tools during the make file execution.
pkg-config is a function that returns meta information for the specified library.
The default setting for PKG_CONFIG_PATH is /usr/lib/pkgconfig because of the prefix we use to install pkgconfig. You may add to PKG_CONFIG_PATH by exporting additional paths on your system where pkgconfig files are installed. Note that PKG_CONFIG_PATH is only needed when compiling packages, not during run-time.

我想看过这段说明后,你已经大概了解了它是做什么的吧。
其实pkg-config就是向configure程序提供系统信息的程序,比如软件的版本、库的版本啦、库的路径,等等
这些信息只是在编译其间使用。你可以 ls /usr/lib/pkgconfig 下,会看到许多的*.pc, 用文本编辑器打开
会发现类似下面的信息:

prefix=/usr 
exec_prefix=${prefix} 
libdir=${exec_prefix}/lib 
includedir=${prefix}/include 

glib_genmarshal=glib-genmarshal 
gobject_query=gobject-query 
glib_mkenums=glib-mkenums 

Name: GLib 
Description: C Utility Library 
Version: 2.4.7 
Libs: -L${libdir} -lglib-2.0 
Cflags: -I${includedir}/glib-2.0 -I${libdir}/glib-2.0/include 

明白了吧,编译期间configure就是靠这些信息判断你的软件版本是否符合要求。并且得到这些东东所在的位置,要不去哪里找呀。
不用我说你也知道为什么会出现上面那些问题了吧。 解决的办法很简单,设定正确的PKG_CONFIG_PATH,假如将
glib-2.x.x装到了/usr/local/下,那么glib-2.0.pc就会在 /usr/local/lib/pkgconfig下, 将这个路径添加
PKG_CONFIG_PATH下就可以了。并且确保configure找到的是正确的glib-2.0.pc, 将其他的lib/pkgconfig目录glib-2.0.pc干掉就是啦 (如果有的话 ) 。
设定好后可以加入到~/.bashrc中,例如:

PKG_CONFIG_PATH=/opt/kde-3.3.0/lib/pkgconfig:/usr/lib/pkgconfig:/usr/local/pkgconfig: /usr/X11R6/lib/pkgconfig 
[root@NEWLFS ~]#echo $PKG_CONFIG_PATH 
/opt/kde-3.3.0/lib/pkgconfig:/usr/lib/pkgconfig:/usr/local/pkgconfig:/usr/X11R6/lib/pkgconfig 

另外./configure通过,make出错,遇到这样的问题比较难办,只能凭经验查找原因,比如某个头文件没有找到,
这时候要顺着出错的位置一行的一行往上找错,比如显示xxxx.h no such file or directory说明缺少头文件
然后去google搜。
或者找到感觉有价值的错误信息,拿到google去搜,往往会找到解决的办法。还是开始的那句话,要仔细看README, INSTALL
一:编译完成后,输入echo $? 如果返回结果为0,则表示正常结束,否则就出错了 :(
echo $? 表示 检查上一条命令的退出状态,程序正常退出 返回0,错误退出返回非0。
二:编译时,可以用&&连接命令, && 表示"当前一条命令正常结束,后面的命令才会执行",就是"与"啦。
这个办法很好,即节省时间,又可防止出错。例:

./configure --prefix=/usr && make && make install 

实例:
编译DOSBOX时出现cdrom.h:20:23: SDL_sound.h: No such file or directory
于是下载,安装,很顺利,没有指定安装路径,于是默认的安装到了/usr/local/
当编译DOSBOX make时,出现如下错误:

if g++ -DHAVE_CONFIG_H -I. -I. -I../.. -I../../include -I/usr/include/SDL -D_REENTRANT -march=pentium4 -O3 -pipe -fomit-frame-pointer -MT dos_programs.o -MD -MP -MF ".deps/dos_programs.Tpo" -c -o dos_programs.o dos_programs.cpp; \ 
then mv -f ".deps/dos_programs.Tpo" ".deps/dos_programs.Po"; else rm -f ".deps/dos_programs.Tpo"; exit 1; fi 
In file included from dos_programs.cpp:30: 
cdrom.h:20:23: SDL_sound.h: No such file or directory <------错误的原因在这里 
In file included from dos_programs.cpp:30: 
cdrom.h:137: error: ISO C++ forbids declaration of `Sound_Sample' with no type 
cdrom.h:137: error: expected `;' before '*' token 
make[3]: *** [dos_programs.o] Error 1 
make[3]: Leaving directory `/root/software/dosbox-0.63/src/dos' 
make[2]: *** [all-recursive] Error 1 
make[2]: Leaving directory `/root/software/dosbox-0.63/src' 
make[1]: *** [all-recursive] Error 1 
make[1]: Leaving directory `/root/software/dosbox-0.63' 
make: *** [all] Error 2 
[root@NEWLFS dosbox-0.63]# 

看来是因为cdrom.h没有找到SDL_sound.h这个头文件
所以出现了下面的错误,但是我明明已经安装好了SDL_sound阿?
经过查找,在/usr/local/include/SDL/下找到了SDL_sound.h
看来dosbox没有去搜寻/usr/local/include/SDL下的头文件,既然找到了原因,就容易解决啦

[root@NEWLFS dosbox-0.63]#ln -s /usr/local/include/SDL/SDL_sound.h /usr/include 

做个链接到/usr/include下,这样DOSBOX就可以找到了,顺利编译成功。

@JackieMium JackieMium added Linux Linux related 基础 很基础的东西 Code Talk is chip, show me the code labels Mar 8, 2018
@JackieMium JackieMium added this to the Migration milestone Mar 9, 2018
@JackieMium JackieMium added this to Linux in 博文分类 Jun 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Code Talk is chip, show me the code Linux Linux related 基础 很基础的东西
Projects
博文分类
  
Linux
Development

No branches or pull requests

1 participant