zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux系统编程手册 错误 undefined reference to `usageErr‘ errExit errExit getLong cmdLineErr collect2: erro

Linux错误系统编程 to 手册 reference undefined
2023-09-14 09:06:14 时间

/tmp/ccJxsZJ1.o: In function `main':
seek_io.c:(.text+0x4a): undefined reference to `usageErr'
seek_io.c:(.text+0x86): undefined reference to `errExit'
seek_io.c:(.text+0x11d): undefined reference to `getLong'
seek_io.c:(.text+0x149): undefined reference to `errExit'
seek_io.c:(.text+0x17a): undefined reference to `errExit'
seek_io.c:(.text+0x31a): undefined reference to `errExit'
seek_io.c:(.text+0x391): undefined reference to `getLong'
seek_io.c:(.text+0x3c2): undefined reference to `errExit'
seek_io.c:(.text+0x41a): undefined reference to `cmdLineErr'
collect2: error: ld returned 1 exit status

    其中ename.c.inc文件定义了一个字符串数组,用于对应错误码的名称

    error_functions.h文件声明了本书自定义的错误处理函数

    get_num.h文件声明了本书自定义的数值提取函数

     tlpi_hdr.h文件则包含了后续需用到的系统调用头文件

由于存在3个头文件以及2个实现,每次编译时必须对实现也进行编译,为方便后续学习,采用将头文件复制到默认的编译器寻找目录下,并将实现打包成静态库,然后使用别名来默认链接静态库

第一步:下载本书所给的源码文件

wget "http://man7.org/tlpi/code/download/tlpi-161214-dist.tar.gz"

或者点此下载

第二步:解压后,make编译

tar -zxvf tlpi-161214-dist.tar.gz
cd tlpi-dist/
make -j

第三步:拷贝头文件至系统目录

cd lib/
sudo cp tlpi_hdr.h /usr/local/include/
sudo cp get_num.h /usr/local/include/
sudo cp error_functions.h /usr/local/include/
sudo cp ename.c.inc /usr/local/include/

第四步:制作静态库文件

g++ -c get_num.c error_functions.c
ar -crv libtlpi.a get_num.o error_functions.o
sudo cp libtlpi.a /usr/local/lib

第五步:简化编译命令

alias gl++='new() { g++ $1 -ltlpi;}; new'

第五步需要在当前用户的主目录下的.bashrc文件中设置别名,由于alias不支持参数,因此需要使用函数来间接实现,并且接受一个参数$1以指定对某个源文件进行编译并链接之前的静态库libtlpi.a。配置完毕后重新读入.bashrc文件以生效。这里使用函数来接受一个参数存在一个缺点即只能使用一个命令行参数,多余参数将被忽略。

完成上面的步骤后,即可使用快捷命令来编译我们的程序了:

gl++ xxx.c

该命令将编译生成a.out文件