Say bye to CMake and Makefile

2023-11-27 01:38
文章标签 makefile cmake say bye

本文主要是介绍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真是强大的脚本工具,不断改进我的生活。将来完善了,放到我自己的Github项目中。

这篇关于Say bye to CMake and Makefile的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/426720

相关文章

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

Cmake之3.0版本重要特性及用法实例(十三)

简介: CSDN博客专家、《Android系统多媒体进阶实战》一书作者 新书发布:《Android系统多媒体进阶实战》🚀 优质专栏: Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏: 多媒体系统工程师系列【原创干货持续更新中……】🚀 优质视频课程:AAOS车载系统+AOSP14系统攻城狮入门视频实战课 🚀 人生格言: 人生从来没有捷径,只有行动才是治疗恐惧

ubuntu通过apt的方式更新cmake到最新版(ppa)

添加签名密钥 wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | sudo apt-key add - 将存储库添加到您的源列表并进行更新 稳定版 sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic ma

Kernel 中MakeFile 使用if条件编译

有时需要通过if  else来选择编译哪个驱动,单纯的obj-$(CONFIG_)就不是很方便,下面提供两种参考案例: 案例一: 来源:drivers/char/tpm/Makefileifdef CONFIG_ACPItpm-y += tpm_eventlog.o tpm_acpi.oelseifdef CONFIG_TCG_IBMVTPMtpm-y += tpm_eventlog.o

Windows下使用cmake编译OpenCV

Windows下使用cmake编译OpenCV cmake下载OpenCV下载编译OpenCV cmake下载 下载地址:https://cmake.org/download/ 下载完成,点击选择路径安装即可 OpenCV下载 下载地址:https://github.com/opencv/opencv/releases/tag/4.8.1因为我们是编译OpenCV,下图选择

gcc make cmake例程

main.cpp文件: #include <iostream>#include "utils.h"int main(void) {int a = 1;int b = 2;int c = AddFunc(a, b);std::cout<< c <<std::endl;return 0;} utils.h文件: #pragma onceint AddFunc(int a, int b);

leetcode#38. Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 12. 113. 214. 12115. 111221 1 is read off as “one 1” or 11. 11 is read off

Linux cmake使用笔记

CMake: All problems in computer science can be solved by another level of indirection. cmake会根据cmake-language编写的 CMakeLists.txt 或.cmake后缀文件编译自动生成Makefile CMake使用流程: 在 linux 平台下使用 CMake 生成 Makefile

Cmake note

cmake 指定交叉编译工具 指定install安装目录 $CC=arm-linux-uclibcgnueabi-gcc cmake -DCMAKE_INSTALL_PREFIX=./output . $make $make install 删除camke cache文件: find . -iname ‘cmake’ -not -name CMakeLists.txt -exec rm -rf

Makefile问答之02 预处理器与宏

GCC Makefile中,怎样设定预处理器名称 在 GCC 的 Makefile 中,预处理器(preprocessor)的名称通常是 cpp(C PreProcessor),但在实际的 Makefile 中,我们一般是通过 gcc 命令来调用预处理器,而不是直接调用 cpp。不过,你可以通过设置 CPP 变量来显式指定预处理器名称和选项。以下是如何在 Makefile 中进行设置和使用的