Say bye to CMake and Makefile
用了几年的CMake,最近想试着琢磨如何将C++应用的动态链接全部改成静态链接,发现还需要研究CMake的用法,进入CMake的文档,
http://www.cmake.org/cmake/help/syntax.html
看到这句:
In many ways writing a CMakeLists file is like a writing a program in a simple language. Like most languages CMake provides flow control structures to help you along your way. CMake provides three flow control structures:
再往下看,里面是什么条件语句,循环,定义过程等,一个简单的script语言用来控制CMake生成Makefile。
过去CMake帮助了我,因为我对写Makefile很厌烦,CMake让我能够设计良好的目录结构,不要再自己实现目录树递归的Makefile脚本。
但是时至今天,对于已经熟练掌握newlisp语言的我,还有什么比用newlisp来实现脚本更强大的工具,为什么我还用CMake?我并不需要考虑跨平台编译,我只是在实践Linux服务器上的C++编程。我仅仅使用GCC作为编译器。直接用newlisp来控制GCC,可以让我直接使用GCC,更深层次的理解编译器的行为。
说做就做,本着仍然不改变原有CMake工程的精神,创建了几个newlisp脚本实现递归目录树,编译文件,最后动态或者静态链接。
现在介绍一下使用。这个CppCMS程序目录结构是:
tree -L 4
.
├── builder
│ ├── build_config.lsp
│ ├── compile.lsp
│ ├── compile.lsp~
│ ├── dlink.lsp
│ ├── dlink.lsp~
│ ├── g++.lsp
│ ├── link.lsp~
│ ├── rebuild.lsp
│ ├── slink.lsp
│ ├── slink.lsp~
│ └── tmpl.lsp
├── codes
│ ├── main
│ │ ├── build
│ │ ├── CMakeLists.txt
│ │ ├── conf.d
│ │ │ ├── ca-certs.crt
│ │ │ ├── site.crt
│ │ │ ├── ssl.key
│ │ │ ├── ssl-pwd.key
│ │ │ ├── site.conf
│ │ │ ├── default.conf_bk
│ │ │ ├── example_ssl.conf
│ │ │ ├── fastcgi.cml
│ │ │ ├── nginx_signing.key
│ │ │ ├── server.crt
│ │ │ ├── server.csr
│ │ │ ├── server.key
│ │ │ └── server.key.org
│ │ ├── config.js
│ │ ├── create_deploy.sh
│ │ ├── include
│ │ │ ├── bean
│ │ │ ├── configuration.h
│ │ │ ├── controller
│ │ │ ├── display.h
│ │ │ ├── displays.h
│ │ │ ├── exception
│ │ │ ├── group.h
│ │ │ ├── groups.h
│ │ │ ├── helper
│ │ │ ├── module
│ │ │ ├── my_application.h
│ │ │ ├── response.h
│ │ │ ├── service
│ │ │ └── web_user.h
│ │ ├── install.sh
│ │ ├── jscheck
│ │ │ ├── pom.xml
│ │ │ └── src
│ │ ├── proto
│ │ │ ├── build.sh
│ │ │ ├── input
│ │ │ └── output
│ │ ├── resources
│ │ │ ├── html
│ │ │ ├── images
│ │ │ ├── plugin
│ │ │ ├── script
│ │ │ └── style
│ │ ├── src
│ │ │ ├── bean
│ │ │ ├── CMakeLists.txt
│ │ │ ├── controller
│ │ │ ├── display.cpp
│ │ │ ├── displays.cpp
│ │ │ ├── group.cpp
│ │ │ ├── groups.cpp
│ │ │ ├── helper
│ │ │ ├── main.cpp
│ │ │ ├── module
│ │ │ ├── my_application.cpp
│ │ │ ├── service
│ │ │ ├── view
│ │ │ └── web_user.cc
│ │ ├── template
│ │ │ ├── accounts.tmpl
│ │ │ ├── default.tmpl
│ │ │ ├── deny.tmpl
│ │ │ ├── gprsConfig.tmpl
│ │ │ ├── gprsManager.tmpl
│ │ │ ├── groupManagement.tmpl
│ │ │ ├── home.tmpl
│ │ │ ├── login.tmpl
│ │ │ ├── log.tmpl
│ │ │ ├── message.tmpl
│ │ │ ├── register_client.tmpl
│ │ │ ├── updatePassword.tmpl
│ │ │ └── userTitle.tmpl
│ │ └── tool.sh
│ └── test
└── README
运行方式就是进入builder目录执行
./rebuild.lsp
首先将CppCMS的tmpl模板文件编译成C++类
然后将所有.cc和.cpp文件编译成.o文件
最后动态链接成可执行程序。
先看rebuild.lsp代码:
#!/usr/bin/newlisp ;; init
(load "/opt/build_config.lsp")
(set 'armory-folder (env "NEWLISP_ARMORY_HOME"))
(println (append "newlisp armory home folder: " armory-folder))
(load (append armory-folder "/codes/file/file.lsp"))
(file:init) ;; clean files genereated by CMake
(set 'cmake-build-dir "../codes/main/build")
(file:clean-folder cmake-build-dir) ;; clean view/*.cpp files generated from cppcms_tmpl_cc
(set 'view-dir "../codes/main/src/view")
(file:clean-folder view-dir) (set 'tmpl-dir "../codes/main/template") ;; generate .cc files in view folder
(load "tmpl.lsp")
(tmpl-to-cc tmpl-dir view-dir) ;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'include-paths
(list "../codes/main/include"
"../codes/main/src/../../loki-0.1.7/include"))
(set 'o-dir "./o")
(set 'src-paths
(list "../codes/main/src"
"../codes/main/src/bean"
"../codes/main/src/controller"
"../codes/main/src/helper"
"../codes/main/src/module"
"../codes/main/src/service"
"../codes/main/src/view"
)) (compile include-paths src-paths o-dir) ;; link all .o files
(set 'libs
(list "pthread"
"cppcms"
"mongoclient"
"booster"
"loki"
"cryptopp"
"boost_system"
"boost_thread"
"boost_filesystem"
))
(set 'binary-name "sports_lottery")
(set 'bin-dir "bin")
(dynamic-link o-dir bin-dir binary-name libs) (exit)
该文件用到了newlisp armory模块,参考我的GitHub项目:
https://github.com/csfreebird/newlisp_armory
tmpl.lsp文件专门负责处理CppCMS tmpl文件,代码如下:
(define (get-extension name)
(first (regex "[^.]*$" name))
) ;; @syntax (remove-extension name)
;; @return file name without extension
;; @note remove extension name e.g a.b .b is extension, it will be removed
(define (remove-extension name)
((regex "(.*)\\.(.*)$" name) 3)
) ;; @syntax (tmpl-to-cc)
;; @note find all tmpl files in tmpl-folder, translate them into *.cc files
(define (tmpl-to-cc tmpl-folder cc-folder)
(set 'tmpl-files (directory tmpl-folder "\\.tmpl"))
(set 'cmd-tmpl (append "cppcms_tmpl_cc " tmpl-folder "/%s -o " cc-folder "/%s.cc"))
(dolist (tmpl-file tmpl-files)
(set 'cmd (format cmd-tmpl tmpl-file (remove-extension tmpl-file)))
(println cmd)
(exec cmd)
)
)
g++.lsp专门生成g++命令,文件内容如下:
;; @syntax (compile include-dirs src-dirs o-dir)
;; @parameter include-dir a list contains one or more relative or absolute include directories
;; @parameter src-dirs a list contains one or more relative or absolute src dirs
(define (compile include-dirs src-dirs o-dir)
(if (directory? o-dir)
(file:clean-folder o-dir)
(make-dir o-dir))
(set 'path1 "")
(dolist (path include-dirs)
(set 'path1 (append path1 "-I" path " "))) (set 'cmd-template (format "/usr/bin/c++ -g %s -Wall -o %s/" path1 o-dir)) (dolist (dir src-dirs)
(compile-dir dir cmd-template)
)
) ;; @syntax (compile-dir dir cmd-template)
;; @parameter dir one folder which contains many .cc or .cpp files
;; @parameter cmd-template the command template that has -g, -Wall and -I args
(define (compile-dir dir cmd-template)
(set 'file-list (directory dir "\\.cc|\\.cpp"))
(println "dir: " dir)
(dolist (cc-path file-list)
(set 'str (append cc-path ".o"))
(set 'cmd (append cmd-template str " -c " dir "/" cc-path))
(println cmd)
(exec cmd))
) ;; @syntax (dynamic-link o-dir bin-dir binary-name libs)
;; @parameter o-dir the direcotry includs all .o files
;; @parameter bin-dir the location of linked binary file
;; @libs the list of all dependencies
(define (dynamic-link o-dir bin-dir binary-name libs)
(if (directory? bin-dir)
(file:clean-folder bin-dir)
(make-dir bin-dir))
(set 'cmd "/usr/bin/c++ -g")
(set 'o-files (directory o-dir "\\.o"))
(dolist (o-file o-files)
(set 'cmd (append cmd " " (real-path o-dir) "/" o-file)))
(set 'cmd (append cmd " -o " bin-dir "/" binary-name " -rdynamic"))
(dolist (lib libs)
(set 'cmd (append cmd " -l" lib)))
(println cmd)
(exec cmd)
) ;; @syntax (static-link o-dir bin-dir binary-name libs)
;; @parameter o-dir the direcotry includs all .o files
;; @parameter bin-dir the location of linked binary file
;; @libs the list of all dependencies
(define (static-link o-dir bin-dir binary-name libs)
(if (directory? bin-dir)
(file:clean-folder bin-dir)
(make-dir bin-dir))
(set 'cmd "/usr/bin/c++ -g ")
(set 'o-files (directory o-dir "\\.o"))
(dolist (o-file o-files)
(set 'cmd (append cmd " " (real-path o-dir) "/" o-file)))
(set 'cmd (append cmd " -o " bin-dir "/" binary-name " -static-libgcc -static-libstdc++ -static -L/usr/lib/x86_64-linux-gnu"))
(dolist (lib libs)
(set 'cmd (append cmd " -l" lib))
(println cmd))
(exec cmd)
)
为了方便使用,还提供了专门用于编译的文件compile.lsp,可以直接运行。
;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'include-paths
(list "../codes/main/include"
"../codes/main/src/../../loki-0.1.7/include"))
(set 'o-dir "./o")
(set 'src-paths
(list "../codes/main/src"
"../codes/main/src/bean"
"../codes/main/src/controller"
"../codes/main/src/helper"
"../codes/main/src/module"
"../codes/main/src/service"
"../codes/main/src/view"
)) (compile include-paths src-paths o-dir) (exit)
专门动态链接的文件dlink.lsp
#!/usr/bin/newlisp ;; init
(load "/opt/build_config.lsp")
(set 'armory-folder (env "NEWLISP_ARMORY_HOME"))
(println (append "newlisp armory home folder: " armory-folder))
(load (append armory-folder "/codes/file/file.lsp"))
(file:init) ;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'o-dir "./o")
;; link all .o files
(set 'libs
(list "pthread"
"cppcms"
"mongoclient"
"booster"
"loki"
"cryptopp"
"boost_system"
"boost_thread"
"boost_filesystem"
))
(set 'binary-name "sports_lottery_d")
(set 'bin-dir "bin")
(dynamic-link o-dir bin-dir binary-name libs) (exit)
专门静态链接的文件slink.lsp
#!/usr/bin/newlisp ;; init
(load "/opt/build_config.lsp")
(set 'armory-folder (env "NEWLISP_ARMORY_HOME"))
(println (append "newlisp armory home folder: " armory-folder))
(load (append armory-folder "/codes/file/file.lsp"))
(file:init) ;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'o-dir "./o")
;; link all .o files
(set 'libs
(list "mongoclient"
"cppcms"
"booster"
"boost_system"
"loki"
"cryptopp"
"boost_thread"
"pthread"
"boost_filesystem"
"dl"
"gcrypt"
"z"
"pcre"
"icuuc"
"icui18n"
"gpg-error"
"icuuc"
"icudata"
))
(set 'binary-name "sports_lottery_s")
(set 'bin-dir "bin")
(static-link o-dir bin-dir binary-name libs) (exit)
注意,slink.lsp中这些静态库的名称和顺序是不断尝试出来的。我总结了一个好方法:
1. A依赖B,A必须出现在B前
2. 一开始只加一个库mongoclient,然后看连接器报错,看少什么库,然后通过ldd来查找正确的库名称,添加在后面。这样就能找全所有的静态库。
newlisp真是强大的脚本工具,不断改进我的生活。
Say bye to CMake and Makefile的更多相关文章
- 【原+转】用CMake代替makefile进行跨平台交叉编译
在开始介绍如何使用CMake编译跨平台的静态库之前,先讲讲我在没有使用CMake之前所趟过的坑.因为很多开源的程序,比如png,都是自带编译脚本的.我们可以使用下列脚本来进行编译: ./configu ...
- cmake利用toolchain.cmake生成makefile之后,make生成静态库失败问题
问题描述 利用toolchian.cmake设置好编译器后,利用make指令生成静态库,出现以下问题 Error running link command: No such file or direc ...
- 用CMake代替makefile进行跨平台交叉编译
在开始介绍如何使用CMake编译跨平台的静态库之前,先讲讲我在没有使用CMake之前所趟过的坑.因为很多开源的程序,比如png,都是自带编译脚本的.我们可以使用下列脚本来进行编译: 1 2 3 ./c ...
- IDE、Cmake、makefile、make
makefile :就是一个类似脚本的文件,根据一系列规则用于决定哪些文件先编译,哪些文件重新编译等等.甚至于进行更复杂的功能操作,而且还可以执行操作系统的命令.makefile带来的好处就是——“自 ...
- mcstructs使用CMake生成Makefile文件
CMakeLists.txt project(MCSTRUCTS) set(SRC_LIST src/main.c src/mcslist.c src/mcsringbuf.c) add_execut ...
- Cmake Make makefile GNU autotools
个人总结 首先makefile是由make来编译,而makefile的生成可以由GUN autotools和CMake来实现,但前者没有CMake的CMakelist.txt直观,所以我们一般用CMa ...
- Cmake生成Makefile
cmake 相比automake 最大的区别是: 步骤没有automake那么多 main.cpp #include<iostream> #include"student.h&q ...
- CMake编译Makefile
以编译Libtif文件为例: 你可以用CMake编译libtiff,超简单,两个步骤. 参考文章 CharlesSimonyi,libtiff库的问题的答复
- cmake生成Makefile时指定c/c++编译器
cmake .. -DCMAKE_CXX_COMPILER:FILEPATH=/usr/local/bin/g++ -DCMAKE_C_COMPILER:FILEPATH=/usr/local/bin ...
随机推荐
- js本地存储解决方案(localStorage与userData)
WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的user ...
- php计算剩余时间的自定义函数
把两个日期格式的字符串转化成unix时间戳,然后相减获得时间戳差,最后判断剩余时间,生成类似(2小时30分钟20秒前发布)这样的时间格式,代码如下: function gettime($time_s, ...
- 汉诺塔 Hanoi Tower
电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...
- vsftpd 访问 权限控制
vsftpd 重启命令 service vsftpd start|restart|stop vsftpd 关于权限控制,有两个文件分别设置,都会起作用 /etc/vsftpd/user_list / ...
- Python3 如何优雅地使用正则表达式(详解六)
修改字符串 我们已经介绍完如何对字符进行搜索,接下来我们讲讲正则表达式如何修改字符串. 正则表达式使用以下方法修改字符串: 方法 用途 split() 在正则表达式匹配的地方进行分割,并返回一个列表 ...
- Nmap Snote
Title:Nmap Snote --2011-11-15 21:28 用Nmap上瘾了,怕以后忘记,也就记一下. Nmap -v -sS -n -p1-65535 IP Nmap -v -sS -p ...
- DBUtils框架
一.O-R Mapping 简介 一]概念:可以理解为对象和数据库的映射. 二]常用O-R Mapping映射工具 1)Hibernate(全自动框架) 2)l ...
- fatal error C1083: Cannot open include file: 'qttreepropertybrowser.moc': No such file or directory
编译QtPropertyBrowser的时候出错.要用moc来生成.moc文件.我好奇的是与之前的通过.h生成moc_xxx.cpp不一样. 要用以下命令.一般来说是处理带Q_OBJECT宏的.h文件 ...
- B450配置
电脑型号 联想 B450 笔记本电脑 (扫描时间:2015?5?7?操作系统 Windows 7 旗舰版 32位 SP1 ( DirectX 11 ) 处理器 英特尔 酷睿2 双核 T9800 @ ...
- JAVA并发七(多线程环境中安全使用集合API)
在集合API中,最初设计的Vector和Hashtable是多线程安全的.例如:对于Vector来说,用来添加和删除元素的方法是同步的.如果只有一个线程与Vector的实例交互,那么,要求获取和释放对 ...