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

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

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

$ 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 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    955 引用 • 944 回帖
  • C

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

    86 引用 • 165 回帖

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • 支付宝

    支付宝是全球领先的独立第三方支付平台,致力于为广大用户提供安全快速的电子支付/网上支付/安全支付/手机支付体验,及转账收款/水电煤缴费/信用卡还款/AA 收款等生活服务应用。

    29 引用 • 347 回帖 • 2 关注
  • SendCloud

    SendCloud 由搜狐武汉研发中心孵化的项目,是致力于为开发者提供高质量的触发邮件服务的云端邮件发送平台,为开发者提供便利的 API 接口来调用服务,让邮件准确迅速到达用户收件箱并获得强大的追踪数据。

    2 引用 • 8 回帖 • 507 关注
  • Webswing

    Webswing 是一个能将任何 Swing 应用通过纯 HTML5 运行在浏览器中的 Web 服务器,详细介绍请看 将 Java Swing 应用变成 Web 应用

    1 引用 • 15 回帖 • 643 关注
  • 微信

    腾讯公司 2011 年 1 月 21 日推出的一款手机通讯软件。用户可以通过摇一摇、搜索号码、扫描二维码等添加好友和关注公众平台,同时可以将自己看到的精彩内容分享到微信朋友圈。

    134 引用 • 798 回帖
  • 浅吟主题

    Jeffrey Chen 制作的思源笔记主题,项目仓库:https://github.com/TCOTC/Whisper

    1 引用 • 31 回帖
  • Kafka

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

    36 引用 • 35 回帖 • 4 关注
  • Google

    Google(Google Inc.,NASDAQ:GOOG)是一家美国上市公司(公有股份公司),于 1998 年 9 月 7 日以私有股份公司的形式创立,设计并管理一个互联网搜索引擎。Google 公司的总部称作“Googleplex”,它位于加利福尼亚山景城。Google 目前被公认为是全球规模最大的搜索引擎,它提供了简单易用的免费服务。不作恶(Don't be evil)是谷歌公司的一项非正式的公司口号。

    49 引用 • 192 回帖
  • webpack

    webpack 是一个用于前端开发的模块加载器和打包工具,它能把各种资源,例如 JS、CSS(less/sass)、图片等都作为模块来使用和处理。

    42 引用 • 130 回帖 • 253 关注
  • Access
    1 引用 • 3 回帖 • 3 关注
  • Flutter

    Flutter 是谷歌的移动 UI 框架,可以快速在 iOS 和 Android 上构建高质量的原生用户界面。 Flutter 可以与现有的代码一起工作,它正在被越来越多的开发者和组织使用,并且 Flutter 是完全免费、开源的。

    39 引用 • 92 回帖 • 11 关注
  • Elasticsearch

    Elasticsearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

    117 引用 • 99 回帖 • 197 关注
  • Firefox

    Mozilla Firefox 中文俗称“火狐”(正式缩写为 Fx 或 fx,非正式缩写为 FF),是一个开源的网页浏览器,使用 Gecko 排版引擎,支持多种操作系统,如 Windows、OSX 及 Linux 等。

    7 引用 • 30 回帖 • 375 关注
  • Bug

    Bug 本意是指臭虫、缺陷、损坏、犯贫、窃听器、小虫等。现在人们把在程序中一些缺陷或问题统称为 bug(漏洞)。

    76 引用 • 1742 回帖 • 2 关注
  • App

    App(应用程序,Application 的缩写)一般指手机软件。

    91 引用 • 384 回帖
  • 代码片段

    代码片段分为 CSS 与 JS 两种代码,添加在 [设置 - 外观 - 代码片段] 中,这些代码会在思源笔记加载时自动执行,用于改善笔记的样式或功能。

    用户在该标签下分享代码片段时需在帖子标题前添加 [css] [js] 用于区分代码片段类型。

    203 引用 • 1475 回帖 • 1 关注
  • 大数据

    大数据(big data)是指无法在一定时间范围内用常规软件工具进行捕捉、管理和处理的数据集合,是需要新处理模式才能具有更强的决策力、洞察发现力和流程优化能力的海量、高增长率和多样化的信息资产。

    89 引用 • 113 回帖
  • frp

    frp 是一个可用于内网穿透的高性能的反向代理应用,支持 TCP、UDP、 HTTP 和 HTTPS 协议。

    17 引用 • 7 回帖 • 2 关注
  • 招聘

    哪里都缺人,哪里都不缺人。

    188 引用 • 1057 回帖
  • 书籍

    宋真宗赵恒曾经说过:“书中自有黄金屋,书中自有颜如玉。”

    84 引用 • 414 回帖
  • 爬虫

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

    106 引用 • 275 回帖 • 1 关注
  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    315 引用 • 547 回帖 • 1 关注
  • Hexo

    Hexo 是一款快速、简洁且高效的博客框架,使用 Node.js 编写。

    22 引用 • 148 回帖 • 9 关注
  • MySQL

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是最流行的关系型数据库管理系统之一。

    694 引用 • 537 回帖 • 2 关注
  • 架构

    我们平时所说的“架构”主要是指软件架构,这是有关软件整体结构与组件的抽象描述,用于指导软件系统各个方面的设计。另外还有“业务架构”、“网络架构”、“硬件架构”等细分领域。

    142 引用 • 442 回帖 • 1 关注
  • ActiveMQ

    ActiveMQ 是 Apache 旗下的一款开源消息总线系统,它完整实现了 JMS 规范,是一个企业级的消息中间件。

    19 引用 • 13 回帖 • 684 关注
  • Notion

    Notion - The all-in-one workspace for your notes, tasks, wikis, and databases.

    10 引用 • 77 回帖
  • RemNote
    2 引用 • 16 回帖 • 26 关注