提示:文章
文章目录
前言
前期疑问:
本文目标:
一、背景
最近
二、使用autotool生成makefile
4.0 程序文件
建立目录:mkdir include src
编写程序:include/str.h
#include <stdio.h>
int str(char *string);
编写程序:src/str.c
#src/str.c
#include "str.h"
//print string
int str(char *string){
printf("\n----PRINT STRING----\n\"%s\"\n",string);
return 0;
}
//interface of this program
int main(int argc , char **argv){
char str_read[1024];
printf("Please INPUT something end by [ENTER]\n");
scanf("%s",str_read);
return str(str_read );
}
4.1执行autoscan
[root@localhost str]# ls
include src
[root@localhost str]# autoscan
静默执行
4.1.1报错
root@0002:~/gdal_0407/MakefileTest# autoscan
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/${ <-- HERE [^}]*}/ at /usr/bin/autoscan line 361.
解决办法:
执行autoscan后生成文档
-rw-r--r-- 1 root root 0 Apr 8 15:11 autoscan.log
-rw-r--r-- 1 root root 469 Apr 8 15:11 configure.scan
drwxr-xr-x 2 root root 4096 Apr 8 15:00 include/
drwxr-xr-x 2 root root 4096 Apr 8 15:01 src/
4.2修改 configure.ac
4.3执行aclocal
静默执行
生成文档
drwxr-xr-x 5 root root 4096 Apr 8 15:16 ./
drwxr-xr-x 6 root root 4096 Apr 8 14:58 ../
-rw-r--r-- 1 root root 42141 Apr 8 15:16 aclocal.m4
drwxr-xr-x 2 root root 4096 Apr 8 15:16 autom4te.cache/
-rw-r--r-- 1 root root 0 Apr 8 15:11 autoscan.log
-rw-r--r-- 1 root root 486 Apr 8 15:16 configure.ac
-rw-r--r-- 1 root root 469 Apr 8 15:11 configure.scan
drwxr-xr-x 2 root root 4096 Apr 8 15:00 include/
drwxr-xr-x 2 root root 4096 Apr 8 15:01 src/
4.4 vi Makefile.am
4.5 autoheader
静默执行
4.6 touch NEWS README AUTHORS ChangeLog
静默执行
4.7automake -a
root@0002:~/gdal_0407/MakefileTest# automake -a
configure.ac:11: 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:3: warning: source file 'src/str.c' is in a subdirectory,
Makefile.am:3: but option 'subdir-objects' is disabled
automake: warning: possible forward-incompatibility.
automake: At least a source file is in a subdirectory, but the 'subdir-objects'
automake: automake option hasn't been enabled. For now, the corresponding output
automake: object file(s) will be placed in the top-level directory. However,
automake: this behaviour will change in future Automake versions: they will
automake: unconditionally cause object files to be placed in the same subdirectory
automake: of the corresponding sources.
automake: You are advised to start using 'subdir-objects' option throughout your
automake: project, to avoid future incompatibilities.
Makefile.am: installing './depcomp'
4.8 autoconf
静默执行
执行测试:
4.9 执行./configure
4.11执行make
生成了str可执行文件
可以使用./str执行文件
4.12中make install安装
将str安装到系统里面,这样只要在终端输入str就可以运行程序了。
make[1]: Entering directory '/root/gdal_0407/MakefileTest'
/bin/mkdir -p '/u/bin'
/usr/bin/install -c str '/u/bin'
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/root/gdal_0407/MakefileTest'
安装路径是根目录/u/bin/
文章中说的是可以在终端输入str执行,但是试了好像不行,需要进入安装路径中才行
4.13 执行make clean
可以清除.o文件
三、使用cmake生成makefile
3.1
简单样例
root@0002:~/gdal_0407/cmakeTest# cmake -version
cmake version 3.9.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
root@0002:~/gdal_0407/cmakeTest# ll
total 16
drwxr-xr-x 2 root root 4096 Apr 8 16:27 ./
drwxr-xr-x 7 root root 4096 Apr 8 16:24 ../
-rw-r--r-- 1 root root 80 Apr 8 16:27 CMakeLists.txt
-rw-r--r-- 1 root root 76 Apr 8 16:25 main.c
root@0002:~/gdal_0407/cmakeTest# cmake .
-- The C compiler identification is GNU 10.3.0
-- The CXX compiler identification is GNU 10.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/gdal_0407/cmakeTest
root@0002:~/gdal_0407/cmakeTest# ll
total 48
drwxr-xr-x 3 root root 4096 Apr 8 16:28 ./
drwxr-xr-x 7 root root 4096 Apr 8 16:24 ../
-rw-r--r-- 1 root root 12578 Apr 8 16:28 CMakeCache.txt
drwxr-xr-x 5 root root 4096 Apr 8 16:28 CMakeFiles/
-rw-r--r-- 1 root root 1361 Apr 8 16:28 cmake_install.cmake
-rw-r--r-- 1 root root 80 Apr 8 16:27 CMakeLists.txt
-rw-r--r-- 1 root root 76 Apr 8 16:25 main.c
-rw-r--r-- 1 root root 4682 Apr 8 16:28 Makefile
root@0002:~/gdal_0407/cmakeTest# vi Makefile
root@0002:~/gdal_0407/cmakeTest# make
Scanning dependencies of target main
[ 50%] Building C object CMakeFiles/main.dir/main.c.o
[100%] Linking C executable main
[100%] Built target main
root@0002:~/gdal_0407/cmakeTest# ll
total 68
drwxr-xr-x 3 root root 4096 Apr 8 16:28 ./
drwxr-xr-x 7 root root 4096 Apr 8 16:24 ../
-rw-r--r-- 1 root root 12578 Apr 8 16:28 CMakeCache.txt
drwxr-xr-x 5 root root 4096 Apr 8 16:28 CMakeFiles/
-rw-r--r-- 1 root root 1361 Apr 8 16:28 cmake_install.cmake
-rw-r--r-- 1 root root 80 Apr 8 16:27 CMakeLists.txt
-rwxr-xr-x 1 root root 16432 Apr 8 16:28 main*
-rw-r--r-- 1 root root 76 Apr 8 16:25 main.c
-rw-r--r-- 1 root root 4682 Apr 8 16:28 Makefile
root@0002:~/gdal_0407/cmakeTest# ./main
Hello World
总结
未完待续