如何快速构建一个简单的c/c++程序

本贴最后更新于 3273 天前,其中的信息可能已经天翻地覆

首先我们通过内置的工程模板创建一个空工程:

$ xmake create -P ./hello create hello ... create ok!:ok_hand:

这个时候 xmake 将会产生一些工程文件,如下:

$ cd ./hello $ tree . . ├── src │   └── main.c └── xmake.lua

这个简单的程序仅仅只是为了打印输出: hello xmake!

$ cat ./src/main.c #include <stdio.h> int main(int argc, char** argv) { printf("hello xmake!\n"); return 0; }

xmake.lua 是基于 lua 语法的工程描述文件,它很简单:

$ cat xmake.lua target("hello") set_kind("binary") add_files("src/*.c")

现在我们开始编译这个程序

$ xmake checking for the architecture ... x86_64 checking for the Xcode SDK version for macosx ... 10.11 checking for the target minimal version ... 10.11 checking for the c compiler (cc) ... xcrun -sdk macosx clang checking for the c++ compiler (cxx) ... xcrun -sdk macosx clang checking for the objc compiler (mm) ... xcrun -sdk macosx clang checking for the objc++ compiler (mxx) ... xcrun -sdk macosx clang++ checking for the assember (as) ... xcrun -sdk macosx clang checking for the linker (ld) ... xcrun -sdk macosx clang++ checking for the static library archiver (ar) ... xcrun -sdk macosx ar checking for the static library extractor (ex) ... xcrun -sdk macosx ar checking for the shared library linker (sh) ... xcrun -sdk macosx clang++ checking for the swift compiler (sc) ... xcrun -sdk macosx swiftc checking for the debugger (dd) ... xcrun -sdk macosx lldb configure { ex = "xcrun -sdk macosx ar" , ccache = "ccache" , plat = "macosx" , ar = "xcrun -sdk macosx ar" , buildir = "build" , as = "xcrun -sdk macosx clang" , sh = "xcrun -sdk macosx clang++" , arch = "x86_64" , mxx = "xcrun -sdk macosx clang++" , xcode_dir = "/Applications/Xcode.app" , target_minver = "10.11" , sc = "xcrun -sdk macosx swiftc" , mode = "release" , make = "make" , cc = "xcrun -sdk macosx clang" , host = "macosx" , dd = "xcrun -sdk macosx lldb" , kind = "static" , ld = "xcrun -sdk macosx clang++" , xcode_sdkver = "10.11" , cxx = "xcrun -sdk macosx clang" , mm = "xcrun -sdk macosx clang" } configure ok! clean ok! [00%]: ccache compiling.release src/main.c [100%]: linking.release hello build ok!:ok_hand:

接着运行它:

$ xmake run hello hello world!

或者进行调试

$ xmake run -d hello [lldb]$target create "build/hello" Current executable set to 'build/hello' (x86_64). [lldb]$b main Breakpoint 1: where = hello`main, address = 0x0000000100000f50 [lldb]$r Process 7509 launched: '/private/tmp/hello/build/hello' (x86_64) Process 7509 stopped * thread #1: tid = 0x435a2, 0x0000000100000f50 hello`main, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 frame #0: 0x0000000100000f50 hello`main hello`main: -> 0x100000f50 <+0>: pushq %rbp 0x100000f51 <+1>: movq %rsp, %rbp 0x100000f54 <+4>: leaq 0x2b(%rip), %rdi ; "hello world!" 0x100000f5b <+11>: callq 0x100000f64 ; symbol stub for: puts [lldb]$

接着我们尝试构建一个 android 版本,这个时候得设置 ndk 路径,当然也能配置到全局配置中,一劳永逸

$ xmake f -p android --ndk=~/files/android-ndk-r10e/ checking for the architecture ... armv7-a checking for the SDK version of NDK ... android-21 checking for the c compiler (cc) ... arm-linux-androideabi-gcc checking for the c++ compiler (cxx) ... arm-linux-androideabi-g++ checking for the assember (as) ... arm-linux-androideabi-gcc checking for the linker (ld) ... arm-linux-androideabi-g++ checking for the static library archiver (ar) ... arm-linux-androideabi-ar checking for the static library extractor (ex) ... arm-linux-androideabi-ar checking for the shared library linker (sh) ... arm-linux-androideabi-g++ configure { ex = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ar" , ccache = "ccache" , ndk = "~/files/android-ndk-r10e/" , sc = "xcrun -sdk macosx swiftc" , ar = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ar" , ld = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++" , buildir = "build" , host = "macosx" , as = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc" , toolchains = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin" , arch = "armv7-a" , mxx = "xcrun -sdk macosx clang++" , xcode_dir = "/Applications/Xcode.app" , target_minver = "10.11" , ndk_sdkver = 21 , mode = "release" , cc = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc" , cxx = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++" , make = "make" , dd = "xcrun -sdk macosx lldb" , kind = "static" , sh = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++" , xcode_sdkver = "10.11" , plat = "android" , mm = "xcrun -sdk macosx clang" } configure ok! $ xmake clean ok! [00%]: ccache compiling.release src/main.c [100%]: linking.release hello build ok!:ok_hand:

或者我们编一个 iphoneos 的版本,例如:

$ xmake f -p iphoneos checking for the architecture ... armv7 checking for the Xcode SDK version for iphoneos ... 9.2 checking for the target minimal version ... 9.2 checking for the c compiler (cc) ... xcrun -sdk iphoneos clang checking for the c++ compiler (cxx) ... xcrun -sdk iphoneos clang checking for the objc compiler (mm) ... xcrun -sdk iphoneos clang checking for the objc++ compiler (mxx) ... xcrun -sdk iphoneos clang++ checking for the assember (as) ... gas-preprocessor.pl xcrun -sdk iphoneos clang checking for the linker (ld) ... xcrun -sdk iphoneos clang++ checking for the static library archiver (ar) ... xcrun -sdk iphoneos ar checking for the static library extractor (ex) ... xcrun -sdk iphoneos ar checking for the shared library linker (sh) ... xcrun -sdk iphoneos clang++ checking for the swift compiler (sc) ... xcrun -sdk iphoneos swiftc configure { ex = "xcrun -sdk iphoneos ar" , ccache = "ccache" , ndk = "~/files/android-ndk-r10e/" , sc = "xcrun -sdk iphoneos swiftc" , ar = "xcrun -sdk iphoneos ar" , sh = "xcrun -sdk iphoneos clang++" , buildir = "build" , xcode_dir = "/Applications/Xcode.app" , as = "/usr/local/share/xmake/tools/utils/gas-preprocessor.pl xcrun -sdk iphoneos clang" , toolchains = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin" , arch = "armv7" , mxx = "xcrun -sdk iphoneos clang++" , ndk_sdkver = 21 , target_minver = "9.2" , cc = "xcrun -sdk iphoneos clang" , mode = "release" , host = "macosx" , cxx = "xcrun -sdk iphoneos clang" , make = "make" , dd = "xcrun -sdk macosx lldb" , kind = "static" , ld = "xcrun -sdk iphoneos clang++" , xcode_sdkver = "9.2" , plat = "iphoneos" , mm = "xcrun -sdk iphoneos clang" } configure ok! $ xmake [00%]: ccache compiling.release src/main.c [100%]: linking.release hello build ok!:ok_hand:

最后我们尝试为 mingw 平台进行编译,sdk 指定交叉工具链目录,交叉编译 linux 平台也可以这么用哦。。

$ xmake f -p mingw --sdk=/usr/local/i386-mingw32-4.3.0/ checking for the architecture ... i386 checking for the c compiler (cc) ... i386-mingw32-gcc checking for the c++ compiler (cxx) ... i386-mingw32-g++ checking for the assember (as) ... i386-mingw32-gcc checking for the linker (ld) ... i386-mingw32-g++ checking for the static library archiver (ar) ... i386-mingw32-ar checking for the static library extractor (ex) ... i386-mingw32-ar checking for the shared library linker (sh) ... i386-mingw32-g++ checking for the swift compiler (sc) ... no configure { ex = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-ar" , ccache = "ccache" , ndk = "~/files/android-ndk-r10e/" , sc = "xcrun -sdk iphoneos swiftc" , sdk = "/usr/local/i386-mingw32-4.3.0/" , cc = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-gcc" , ndk_sdkver = 21 , buildir = "build" , plat = "mingw" , as = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-gcc" , toolchains = "/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin" , arch = "i386" , mxx = "xcrun -sdk iphoneos clang++" , xcode_dir = "/Applications/Xcode.app" , target_minver = "9.2" , sh = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-g++" , mode = "release" , host = "macosx" , cxx = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-g++" , make = "make" , dd = "xcrun -sdk macosx lldb" , kind = "static" , ar = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-ar" , xcode_sdkver = "9.2" , ld = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-g++" , mm = "xcrun -sdk iphoneos clang" } configure ok! $ xmake [00%]: ccache compiling.release src/main.c [100%]: linking.release hello.exe build ok!:ok_hand:

xmake 还能直接在 windows 的 cmd 终端下,进行直接编译 windows 的程序,它会去自动检测当前系统装的 vs 环境,调用里面的 cl.exe 编译器进行编译,一切都是自动化的,我们不需要额外配置什么,只需要执行:xmake 就行了。。

例如:

$ xmake checking for the architecture ... x86 checking for the Microsoft Visual Studio version ... 2008 checking for the c compiler (cc) ... cl.exe checking for the c++ compiler (cxx) ... cl.exe checking for the assember (as) ... ml.exe checking for the linker (ld) ... link.exe checking for the static library archiver (ar) ... link.exe -lib checking for the shared library linker (sh) ... link.exe -dll checking for the static library extractor (ex) ... lib.exe configure { ex = "lib.exe" , sh = "link.exe -dll" , host = "windows" , ar = "link.exe -lib" , as = "ml.exe" , plat = "windows" , buildir = "build" , arch = "x86" , cc = "cl.exe" , cxx = "cl.exe" , mode = "release" , clean = true , kind = "static" , ld = "link.exe" , vs = "2008" } configure ok! [00%]: compiling.release src\main.c [100%]: linking.release hello.exe build ok!

顺便说一下,在 windows 下编译,xmake 是完全支持多任务的哦,默认就是自动多任务构建的,比起以前在 msys, cygwin 里面用 gmake 来编译快多了,因为 windows 下的 gmake 就算你启用了 -j 4 也没啥效果,非常非常得慢。。。


  • makefile
    4 引用 • 6 回帖
  • Lua
    17 引用 • 17 回帖 • 1 关注
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    954 引用 • 944 回帖
  • C

    C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

    86 引用 • 165 回帖 • 3 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
waruqi
专注于跨平台开发解决方案 http://xmake.io http://tboox.org 上海

推荐标签 标签

  • 音乐

    你听到信仰的声音了么?

    62 引用 • 512 回帖
  • SEO

    发布对别人有帮助的原创内容是最好的 SEO 方式。

    36 引用 • 200 回帖 • 34 关注
  • 七牛云

    七牛云是国内领先的企业级公有云服务商,致力于打造以数据为核心的场景化 PaaS 服务。围绕富媒体场景,七牛先后推出了对象存储,融合 CDN 加速,数据通用处理,内容反垃圾服务,以及直播云服务等。

    29 引用 • 230 回帖 • 124 关注
  • OpenShift

    红帽提供的 PaaS 云,支持多种编程语言,为开发人员提供了更为灵活的框架、存储选择。

    14 引用 • 20 回帖 • 661 关注
  • 导航

    各种网址链接、内容导航。

    45 引用 • 177 回帖 • 1 关注
  • 域名

    域名(Domain Name),简称域名、网域,是由一串用点分隔的名字组成的 Internet 上某一台计算机或计算机组的名称,用于在数据传输时标识计算机的电子方位(有时也指地理位置)。

    44 引用 • 208 回帖 • 1 关注
  • Solidity

    Solidity 是一种智能合约高级语言,运行在 [以太坊] 虚拟机(EVM)之上。它的语法接近于 JavaScript,是一种面向对象的语言。

    3 引用 • 18 回帖 • 442 关注
  • 微软

    微软是一家美国跨国科技公司,也是世界 PC 软件开发的先导,由比尔·盖茨与保罗·艾伦创办于 1975 年,公司总部设立在华盛顿州的雷德蒙德(Redmond,邻近西雅图)。以研发、制造、授权和提供广泛的电脑软件服务业务为主。

    8 引用 • 44 回帖 • 2 关注
  • 30Seconds

    📙 前端知识精选集,包含 HTML、CSS、JavaScript、React、Node、安全等方面,每天仅需 30 秒。

    • 精选常见面试题,帮助您准备下一次面试
    • 精选常见交互,帮助您拥有简洁酷炫的站点
    • 精选有用的 React 片段,帮助你获取最佳实践
    • 精选常见代码集,帮助您提高打码效率
    • 整理前端界的最新资讯,邀您一同探索新世界
    488 引用 • 384 回帖 • 4 关注
  • Caddy

    Caddy 是一款默认自动启用 HTTPS 的 HTTP/2 Web 服务器。

    10 引用 • 54 回帖 • 180 关注
  • BAE

    百度应用引擎(Baidu App Engine)提供了 PHP、Java、Python 的执行环境,以及云存储、消息服务、云数据库等全面的云服务。它可以让开发者实现自动地部署和管理应用,并且提供动态扩容和负载均衡的运行环境,让开发者不用考虑高成本的运维工作,只需专注于业务逻辑,大大降低了开发者学习和迁移的成本。

    19 引用 • 75 回帖 • 678 关注
  • 自由行
    3 关注
  • Netty

    Netty 是一个基于 NIO 的客户端-服务器编程框架,使用 Netty 可以让你快速、简单地开发出一个可维护、高性能的网络应用,例如实现了某种协议的客户、服务端应用。

    49 引用 • 33 回帖 • 38 关注
  • MongoDB

    MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是一个基于分布式文件存储的数据库,由 C++ 语言编写。旨在为应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似 JSON 的 BSON 格式,因此可以存储比较复杂的数据类型。

    91 引用 • 59 回帖
  • Vditor

    Vditor 是一款浏览器端的 Markdown 编辑器,支持所见即所得、即时渲染(类似 Typora)和分屏预览模式。它使用 TypeScript 实现,支持原生 JavaScript、Vue、React 和 Angular。

    372 引用 • 1857 回帖 • 1 关注
  • GitHub

    GitHub 于 2008 年上线,目前,除了 Git 代码仓库托管及基本的 Web 管理界面以外,还提供了订阅、讨论组、文本渲染、在线文件编辑器、协作图谱(报表)、代码片段分享(Gist)等功能。正因为这些功能所提供的便利,又经过长期的积累,GitHub 的用户活跃度很高,在开源世界里享有深远的声望,并形成了社交化编程文化(Social Coding)。

    209 引用 • 2040 回帖 • 1 关注
  • 笔记

    好记性不如烂笔头。

    311 引用 • 794 回帖
  • 生活

    生活是指人类生存过程中的各项活动的总和,范畴较广,一般指为幸福的意义而存在。生活实际上是对人生的一种诠释。生活包括人类在社会中与自己息息相关的日常活动和心理影射。

    230 引用 • 1432 回帖
  • Maven

    Maven 是基于项目对象模型(POM)、通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。

    188 引用 • 319 回帖 • 240 关注
  • 区块链

    区块链是分布式数据存储、点对点传输、共识机制、加密算法等计算机技术的新型应用模式。所谓共识机制是区块链系统中实现不同节点之间建立信任、获取权益的数学算法 。

    92 引用 • 752 回帖 • 2 关注
  • Kafka

    Kafka 是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。 这种动作(网页浏览,搜索和其他用户的行动)是现代系统中许多功能的基础。 这些数据通常是由于吞吐量的要求而通过处理日志和日志聚合来解决。

    36 引用 • 35 回帖 • 3 关注
  • 爬虫

    网络爬虫(Spider、Crawler),是一种按照一定的规则,自动地抓取万维网信息的程序。

    106 引用 • 275 回帖
  • Rust

    Rust 是一门赋予每个人构建可靠且高效软件能力的语言。Rust 由 Mozilla 开发,最早发布于 2014 年 9 月。

    59 引用 • 22 回帖 • 9 关注
  • RYMCU

    RYMCU 致力于打造一个即严谨又活泼、专业又不失有趣,为数百万人服务的开源嵌入式知识学习交流平台。

    4 引用 • 6 回帖 • 61 关注
  • 外包

    有空闲时间是接外包好呢还是学习好呢?

    26 引用 • 233 回帖 • 5 关注
  • Excel
    31 引用 • 28 回帖
  • Outlook
    1 引用 • 5 回帖 • 1 关注