用了几年的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 ...
随机推荐
-
GPU keylogger &;&; GPU Based rootkit(Jellyfish rootkit)
catalog . OpenCL . Linux DMA(Direct Memory Access) . GPU rootkit PoC by Team Jellyfish . GPU keylogg ...
-
Android中有时候运行程序的时候会报错:An internal error occurred during:。。。。
解决办法: Project -> Properties -> Run/Debug Settings: 1. select "Launching New_configuration ...
-
RANSAC和Flitline
[blog算法原理]RANSAC和FitLine 如果已经有一系列图片,需要拟合出最为合适的一条直线出来,这个时候你会选择RANSAC还是FitLine. 一.算法定义: RANSAC是实际运用非 ...
-
非常实用的10个PHP高级应用技巧
PHP 独特的语法混合了 C.Java.Perl 以及 PHP 自创新的语法.它可以比 CGI或者Perl更快速的执行动态网页.用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML ...
-
Lua代码解析-写给C和C++开发人员
lua语言作为一门轻量级脚本语言,能够非常好的被嵌入到应用程序,因此,在移动游戏开发中举足轻重 然后C/C++开发人员转lua并非非常习惯,我也是..所以,一起努力学习lua吧 lua没有类的概念,有 ...
-
tensorflow 使用 3 模型学习
模型学习 import tensorflow as tf import numpy as np # 生成 100 个随机的点 x_data = np.random.rand( 100 ) y_data ...
-
CentOS6.9切换root用户su root输入正确密码后一直提示Incorrect password,如何解决?
su是切换用户命令,su root时,输入正确的root命令,却提示Incorrect password,当前用户为普通用户,遇到此问题该如何解决呢? 如果设置了wheel组,使用su root命令是 ...
-
CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
-
简单的socket编程
1.socket 服务器搭建 实例化socket服务器,循环获取请求 package com.orange.util; import java.io.IOException; import java. ...
-
大数据入门第一天——基础部分之Linux基础(环境准备与先导知识)
一.Linux环境安装 1.VM的安装 参考Linux环境搭建随笔:http://www.cnblogs.com/jiangbei/p/7248054.html 2.CentOS的安装 同参考上述随笔 ...