UBuntu CMake使用示例

本文详细介绍如何在Ubuntu系统上安装CMake,并通过一个简单的C++程序实例展示如何编写CMakeLists.txt文件来配置编译过程,最终实现从源代码到可执行文件的构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一:安装CMake
sudo apt-get install cmake
 
Do you want to continue [Y/n]? y
Get:1 http://cn.archive.ubuntu.com/ubuntu/ quantal/main libxmlrpc-core-c3 i386 1.16.33-3.1ubuntu6 [182 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu/ quantal/main emacsen-common all 2.0.3 [17.3 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu/ quantal/main cmake-data all 2.8.9-0ubuntu1 [796 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu/ quantal/main cmake i386 2.8.9-0ubuntu1 [4730 kB]
......
 
二:编写待测试程序
hhh@hhh-VirtualBox:~/project/test$ vi hhh.cpp
#include <iostream>
using namespace std;
 
int main()
{
    cout<<"hello my test"<<endl;
    return 0;
}
 
三:编写CMakefile.txt
hhh@hhh-VirtualBox:~/project/test$ vi CMakeLists.txt
 
PROJECT (hhh)
SET(SRC_LIST hhh.cpp)
MESSAGE(STATUS "This is BINARY dir "${HHH_BINARY_DIR}) 
MESSAGE(STATUS "This is SOURCE dir "${HHH_SOURCE_DIR})
ADD_EXECUTABLE(hhh ${SRC_LIST})  
 
四:执行cmake命令生成makefile文件
hhh@hhh-VirtualBox:~/project/test$ cmake .
-- The C compiler identification is GNU 4.7.2
-- The CXX compiler identification is GNU 4.7.2
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- 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
-- This is BINARY dir 
-- This is SOURCE dir 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hhh/project/test
 
五:执行make命令,生成目标文件
hhh@hhh-VirtualBox:~/project/test$ make
Scanning dependencies of target hhh
[100%] Building CXX object CMakeFiles/hhh.dir/hhh.cpp.o
Linking CXX executable hhh
[100%] Built target hhh
 
六:运行程序
hhh@hhh-VirtualBox:~/project/test$ ./hhh
hello my test

转载于:https://www.cnblogs.com/tyjw/archive/2013/02/13/2910765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值