cmake 及make 实践记录
DEBIAN操作系统
预备操作:
安装 gcc g++ make cmake
开启Terminal
切换到超级用户 下载安装上述软件
1 2 3 4 5 6 7 8 9 10 11 12 | A@debian:~$ su Password: root@debian:/home/A# apt- get install gcc g++ make cmake Reading package lists... Done Building dependency tree Reading state information... Done make is already the newest version. gcc is already the newest version. g++ is already the newest version. cmake is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. |
//==========================================================
makefile 内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | TARGET= main CPP_FILES = $(shell ls *.cpp) BASE = $(basename $(CPP_FILES)) OBJS = $(addsuffix .o, $(addprefix obj/,$(BASE))) $(TARGET):$(OBJS) -rm -f $@ g++ -o $(TARGET) $(OBJS) obj/%.o:%.cpp @ if test -d "obj" ;then\ mkdir -p obj;\ fi;\ g++ -c -o $@ $< clean: -rm -f $(TARGET) -rm -f obj/*.o |
同一目录下有三个文件 main.cpp Test1.cpp Test1.h Test2.cpp Test2.h
最后结果:
1 2 3 | # make rm -f main g++ -o main obj/main.o obj/Test1.o obj/Test2.o |
内容解释参考
http://blog.csdn.net/wcl199274/article/details/39140459
由于网页排版 makefile内容请大家注意重新使用TAB排版 否则可能编译不过
//=============================================================
CMAKE的测试环境如下
一个main.cpp 内容随意 可编译即可
一个CMakeLists.txt
内容如下:
1 2 3 4 | PROJECT(main) CMAKE_MINIMUM_REQUIRED(VERSION 2.6) AUX_SOURCE_DIRECTORY(. DIR_SRCS) ADD_EXECUTABLE(main ${DIR_SRCS}) |
编译结果如下
1 2 3 4 | cmake . -- Configuring done -- Generating done -- Build files have been written to: /home/A/Desktop/3 |
1 2 3 4 | make [100%] Building CXX object CMakeFiles/main.dir/main.cpp.o Linking CXX executable main [100%] Built target main |
//===================================================
更进一步的 根目录下 放入main.cpp CMakeLists.txt
在新建一个子目录src 子目录下放置Test1.cpp Test1.h CMakeLists.txt
根目录CMakeLists.txt内容如下:
1 2 3 4 5 6 | PROJECT(main) CMAKE_MINIMUM_REQUIRED(VERSION 2.6) ADD_SUBDIRECTORY( src ) AUX_SOURCE_DIRECTORY(. DIR_SRCS) ADD_EXECUTABLE(main ${DIR_SRCS} ) TARGET_LINK_LIBRARIES( main Test ) |
子目录CMakeLists.txt内容如下:
1 2 | AUX_SOURCE_DIRECTORY(. DIR_TEST1_SRCS) ADD_LIBRARY ( Test ${DIR_TEST1_SRCS}) |
编译显示如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | cmake . -- The C compiler identification is GNU 4.9.2 -- The CXX compiler identification is GNU 4.9.2 -- 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 -- 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 -- Configuring done -- Generating done -- Build files have been written to: /home/A/Desktop/2 |
1 2 3 4 5 6 7 8 9 | make Scanning dependencies of target Test [ 50%] Building CXX object src/CMakeFiles/Test.dir/Test1.cpp.o Linking CXX static library libTest.a [ 50%] Built target Test Scanning dependencies of target main [100%] Building CXX object CMakeFiles/main.dir/main.cpp.o Linking CXX executable main [100%] Built target main |
内容解释参考
http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话