胶南第二纺织机械厂:Fedora 15 编译APUE2源代码

来源:百度文库 编辑:偶看新闻 时间:2024/05/10 10:27:40

编译源代码

1、从官网下载源代码www.apuebook.com

2、解压 tar -xzvf src.tar.gz 

3、默认解压后的源代码名称为:apue.2e,里面包含了所有的代码

4、修改目录,我的系统是Fedora 15,修改apue.2e下的Make.defines.linux 
        WKDIR=为当前源代码所在的路径(主要是该路径)

5、在apue.2e目录下运行make命令

提示缺少:stropts.h

尝试:1.create a blank file named stropts.h under /usr/include  2.或者拷贝gilbc源文件 glibc-2.9\streams\sys\ stropts.h  到/usr/include 仍然不行 好在已经生成:./lib/libapue.a 
既然主要是单个程序为主,不用修改每个文件
拷贝./include/apue.h 到 ./
修改fig1.3为fig1.3.c直接编译单个文件#gcc fig1.3.c lib/libapue.a 

则会生成可执行文件a.out。执行命令

#./a.out /home

成功!



undefined reference to 'pthread_create'

问题原因:
    pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。

问题解决:
    在编译中要加 -lpthread参数
    gcc thread.c -o thread -lpthread
    thread.c为你些的源文件,不要忘了加上头文件#include
见附录1Submitted by admin on Sun, 01/31/2010 - 00:02

It is a known issue that modern Linux systems are missing stropts.file. You will probably have some problems when trying to compile software like pppd, pptp, gftp, etc from sources. Seems to be a strange thing, as you won’t get any errors in previous versions of Linux. Most recent versions of Fedora don’t contain this file, that’s why we need to know what to do if software compilation fails because of missing stropts.h.

Let’s determine when we need to have this file on our machine.When we’re compiling any “old” package from sources, we may receive the following error:

error: stropts.h: No such file or directory

What could be the reason and why isn’t this file included into our Linux distribution?This error means that your system doesn’t support STREAMS.

Linux doesn’t support STREAMS (many years ago it was available as a third party module, but it hasn’t worked for years). stropts.h is part of a POSIX XSR option, which is not supported in modern Linux distributions. Do we really need it in Linux?

This means that software you’re trying to compile, will not use the functions listed under stropts.h as they’re not supported by the operating system. So we will do a simple trick that will let you to compile your software without these functions.

Since the stropts.h is required for a successful compilation, the most simple way to solve the issue is to create a blank file named stropts.h under /usr/include. You may want to put any comments there, this way you won’t forget what was the reason to create this file.

This simple trick will help you to compile pptp (1.7.2),  pppd (2.4.5), and I think that the list will be much bigger. That was just my experience, but the sense remains the same: you need this file for a successful compilation. Just create it and have fun!

original link: http://www.blisstering.com/stroptsh-no-such-file-or-directory-%E2%80%93-how-fix

附录二

 前面一篇文章讲解了《UNIX环境高级编程》源码编译方法。文中所讲到的编译方法是非作者提供的编译方法,即不使用作者提供的头文件,程序中所有使用的头文件都一一列出。而程序中的出错处理函数则简单的用printf函数替代。

    随后,也有网友提出如何采用作者的方法来对所有的程序进行编译。出于解决问题,同时也想实现这个方法,毕竟实现之后每个程序都可以直接运行,还是比较方便的。因此,就按照源代码文件夹中的README的步骤,对整个源代码进行了编译。

(一)作者提供的编译方法的实现

     README文件中给出的编译方法如下:

    To build the source, edit the Make.defines.* file for your system and set WKDIR to the pathname of the tree containing the source code.  Then just run "make".  It should figure out the system type and build the source for that platform utomatically.

     参照该方法,我将源码的编译分为三步。整个步骤都是在root超级用户下进行的,如果其他用户没有权限进行编译,可以通过su命令切换到超级用户。

      第一步,编辑Make.defines.*文件。由于我所使用的操作系统是FreeBSD6.1,所以应该编辑文件Make.defines.freebsd。其实,编辑该文件的内容主要是修改其中的WKDIR,即我们源码所在文件夹的绝对路径名。原文件中WKDIR=/home/sar/apue.2e,我们可以根据我们实际文件夹所在的位置进行相应的修改。我的apue.2e文件夹直接放在/home下了,所以我将WKDIR修改为WKDIR=/home/apue.2e。其余内容不用修改,保存修改后的文件。

      第二步,修改脚本文件systype.sh的权限。由于原始的systype.sh文件不具有可执行的权限。通过执行命令:

#chmod +x systype.sh  

给当前用户及其所在组和其他组添加可执行权限;

或者

#chmod u+x systype.sh 

仅给当前用户添加可执行权限。

       该脚本文件的功能主要是检测操作系统的类型。它可以检测到系统的类型有:FreeBSD,Linux,MacOS和Solaris等。如果单独执行这个shell脚本:

#./systype.sh

则输出结果为:freebsd。即检测本机的操作系统为FreeBSD。

    第三步,进行源码的编译。在命令行下执行make命令。通过查看Makefile文件可知,make之后,首先执行systype.sh脚本,即首先确定操作系统的类型,然后在进行源码的编译。在编译的过程中,会有一些Warning。这些都是正常的,导致警告的原因可能是采用编译起的版本不同或者是同一类型操作系统的版本不同。但是,只要make的过程不出现error,就会顺利的生成可执行文件。我的在编译过程中没有出现error,因此意味着编译成功。

       注意:编译的过程中可能会出现的一个问题,也是一个网友曾经问到的问题,就是在编译中出现这个的错误,提示nawk command cannot be found。这个问题可能的原因是,有些操作系统的内核版本较低,可能还不支持nawk(new awk)这个命令。但应该支持awk命令。因此,问题的解决方法就是将相关文件中的nawk命令替换为awk,或者为系统添加一个别名alias,alias nawk awk。这样在编一的过程中,遇到nawk命令时,实际会去执行awk命令。如果还有其他问题,可以去网上搜索相关的解决方法。因为我在编译的过程中没有遇到这样的问题,不再一一赘述。

(二)编译生成可执行文件的位置

     在路径/home/apue.2e/下虽然有所有的源文件,都是以figx.x的形式命名。但是实际编译的过程并不是编译的这些文件。而是编译在该路径下各个文件夹中的后缀名为*.c的程序。作者把同一章节或者相近几个章节的源代码放在某一个文件夹下面(include和lib文件夹除外)。而文件夹的命名一般是和该章对应的标题是一致的,采用的是英文标题的全称或简写。譬如,advio文件夹对应Chapter 14. Advanced I/O,该章的代码就放在该文件夹下面。还有文件夹proc对应Chapter 8. Process Control,文件夹termios对应Chapter 18. Terminal I/O等等,基本上每一章的代码都可以在这些文件夹中找到。

(三)如何编译单独的源文件

     通过make命令是直接将所有的源程序编译成可执行文件的。对于喜欢修改和调试程序的朋友来说,make生成的可执行文件显然不具有这样的功能,而且,也不可能修改了一个源文件,然后还要make。如何需要编译和调试单个程序的话,方法如下:

1.首先还是要用make对所有文件进行编译。成功编译后,会在WKDIR/lib/下生成库文件libapue.a,主要是将apue.h(位于WKDIR/include/)中定义的所有内容生成一个静态的库,这样可以方便调用。

2.我们以WKDIR/下的fig1.3(实现ls部分功能)文件为例说明需要修改的地方。将fig1.3文件重命名为fig1.3.c,然后编辑该文件,将包含头文件的一行代码:

#i nclude "apue.h"  //默认所引用头文件的位置为当前的路径WKDIR=/home/apue.2e

修改为

#i nclude "include/apue.h"

即头文件apue.h的位置为当前路径下inlucde文件夹中,这个就正确的指定了apue.h的位置。

这样就可以进行编译了,但在编译的时候还要加上库文件libapue.a,因为该文件实现了apue.h中的所有功能,主要有常用头文件,宏定义以及自定义函数的实现。

输入命令

#gcc fig1.3.c lib/libapue.a  

则会生成可执行文件a.out。执行命令

#./a.out /home

则列出我的/home路径下的所有文件和文件夹:

.

..

david

simsun .ttc

simkai.ttf

simsun.ttf

MYKERNEL

unix_advance_program

freebsd

APUE Source Code

LumaQQ

apue.2e

bash-script

lumaqq_2005_patch_2006.01.22.15.00.zip

lumaqq_2005-linux_gtk2_x86_with_jre.tar

apue_src_complied.tar

 

   当然,如果需要编译的是各个文件夹中的一个源程序时,则只需对所包含的头文件apue.h的路径作相对修改,改为

#i nclude "../include/apue.h"

以及编译是库文件的位置也相应修改,改为:

#gcc sourcefile.c ../lib/libapue.a

    至此,APUE第二版作者提供的源码编译方法和单独源码的编译都已经实现。