前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《CMake那些事》HelloWorld bin

《CMake那些事》HelloWorld bin

作者头像
公众号guangcity
发布2023-09-02 10:40:47
1770
发布2023-09-02 10:40:47
举报
文章被收录于专栏:光城(guangcity)

今天开始长更《CMake那些事》系列

每一期的内容将会以:例子驱动、目录驱动的方式来Step by step方式学习,欢迎收藏转发。

生成可执行文件

假设我们只有一个.cc 文件。

  • 根目录中创建一个CMakeLists.txt文件。
代码语言:javascript
复制
cmake_minimum_required(VERSION 2.8)

project(app_project)

add_executable(hello main.cc)

install(TARGETS hello DESTINATION bin)

执行步骤,见->后面内容,先创建build目录,然后编译安装,最后便会安装到/usr/local/bin/hello,上面bin也可以替换为自己的目录。

代码语言:javascript
复制
➜   mkdir build
➜   cd build 
➜   cmake ..
-- The C compiler identification is AppleClang 12.0.5.12050022
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /cmake/build
➜   make
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cc.o
[100%] Linking CXX executable hello
[100%] Built target hello
➜   make install
Consolidate compiler generated dependencies of target hello
[100%] Built target hello
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/hello

最后执行:

代码语言:javascript
复制
➜   /usr/local/bin/hello 
hello world

接下来,我们将上面内容拆解:

第一步:cmake ..

生成Makefile

第二步:make

编译Makefile生成bin文件

第三步:make install

安装到指定的目录

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-07-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 光城 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 生成可执行文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档