【Linux笔记】【基础系列】 automake基础使用3

【Linux笔记】【基础系列】 Automake基础使用 3

上一篇笔记Automake基础使用 2记录了源文件在不同目录下如何实现 Automake 生成 Makefile 的步骤。接下来在基于上一篇的例子上增加引用动态库。

下面还是基于树莓派4B平台进行开发,基于上一篇的示例程序进行修改。

新建一个用于写示例的文件夹:

pi@raspberrypi:~/Desktop $ mkdir automaketest3

在原先的基础上修改 isPrime.c 文件,将判断素数的方法修改,其中用到了sqrt,需要包含#include <math.h>,编译的时候需要链接数学库。

isPrime.c 的内容变更为:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int isPrime(unsigned int num)
{
    unsigned int i;
    unsigned int sqr;
    
    if (num <= 3)
        return num > 1;
    
    if ((num % 6 != 1) && (num % 6 != 5))
        return 0;

    sqr = (unsigned int)sqrt((double)num);
    for(i = 5; i <= sqr; i += 6)
    {
        if ((num % i == 0) || (num % (i + 2) == 0))
            return 0;
    }

    return 1;
}

所有的文件如下:

pi@raspberrypi:~/Desktop/automaketest3 $ ls -R
.:
include  src  test.c

./include:
isOdd.h  isPrime.h

./src:
isOdd.c  isPrime.c

所需的文件就都准备好了,接下来开始生成Makefile。

生成的步骤的顺序和上一篇还是一样的,下面只展示不同的部分。

不同点在于创建 Makefile.am 文件,一共需要创建两个Makefile.am 文件,但是不同的地方只在项目当前目录下的 Makefile.am 文件。

pi@raspberrypi:~/Desktop/automaketest3 $ touch Makefile.am

文件内容为:

AUTOMAKE_OPTIONS = foreign
SUBDIRS = src
bin_PROGRAMS = test
test_SOURCES = test.c
test_LDADD = src/libnumtest.a
AM_CFLAGS = -I./include
LIBS = -lm

增加了LIBS进行动态链接,-lm表示动态链接数学库。

后续的步骤就都一样了,执行 automake --add-missing 和 ./configure 命令:

pi@raspberrypi:~/Desktop/automaketest3 $ automake --add-missing
configure.ac:12: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses
Makefile.am: installing './depcomp'
pi@raspberrypi:~/Desktop/automaketest3 $ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for ranlib... ranlib
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for stdlib.h... (cached) yes
checking for sqrt... no
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands

完成 Makefile 文件的生成。

执行 make 命令,生成可执行文件。

pi@raspberrypi:~/Desktop/automaketest3 $ make
make  all-recursive
make[1]: 进入目录“/home/pi/Desktop/automaketest3”
Making all in src
make[2]: 进入目录“/home/pi/Desktop/automaketest3/src”
gcc -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT isOdd.o -MD -MP -MF .deps/isOdd.Tpo -c -o isOdd.o isOdd.c
mv -f .deps/isOdd.Tpo .deps/isOdd.Po
gcc -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT isPrime.o -MD -MP -MF .deps/isPrime.Tpo -c -o isPrime.o isPrime.c
mv -f .deps/isPrime.Tpo .deps/isPrime.Po
rm -f libnumtest.a
ar cru libnumtest.a isOdd.o isPrime.o 
ar: `u' 修饰符被忽略,因为 `D' 为默认(参见 `U')
ranlib libnumtest.a
make[2]: 离开目录“/home/pi/Desktop/automaketest3/src”
make[2]: 进入目录“/home/pi/Desktop/automaketest3”
gcc -DHAVE_CONFIG_H -I.    -I./include -g -O2 -MT test.o -MD -MP -MF .deps/test.Tpo -c -o test.o test.c
mv -f .deps/test.Tpo .deps/test.Po
gcc -I./include -g -O2   -o test test.o src/libnumtest.a -lm
make[2]: 离开目录“/home/pi/Desktop/automaketest3”
make[1]: 离开目录“/home/pi/Desktop/automaketest3”

编译完成,运行 test 程序。

pi@raspberrypi:~/Desktop/automaketest3 $ ./test 
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
1 3 5 7 9 

运行程序的结果与之前一致。


【参考资料】

Linux c 开发 - Autotools使用详细解读

Autoconf

automake


本文链接:https://blog.csdn.net/u012028275/article/details/125092369

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值