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

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

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

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

    952 引用 • 944 回帖
  • C

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

    85 引用 • 165 回帖

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • 人工智能

    人工智能(Artificial Intelligence)是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门技术科学。

    161 引用 • 307 回帖
  • Hibernate

    Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,使得 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。

    39 引用 • 103 回帖 • 724 关注
  • CloudFoundry

    Cloud Foundry 是 VMware 推出的业界第一个开源 PaaS 云平台,它支持多种框架、语言、运行时环境、云平台及应用服务,使开发人员能够在几秒钟内进行应用程序的部署和扩展,无需担心任何基础架构的问题。

    5 引用 • 18 回帖 • 176 关注
  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1063 引用 • 3455 回帖 • 164 关注
  • V2Ray
    1 引用 • 15 回帖
  • 博客

    记录并分享人生的经历。

    273 引用 • 2388 回帖
  • 书籍

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

    78 引用 • 396 回帖
  • Scala

    Scala 是一门多范式的编程语言,集成面向对象编程和函数式编程的各种特性。

    13 引用 • 11 回帖 • 158 关注
  • LeetCode

    LeetCode(力扣)是一个全球极客挚爱的高质量技术成长平台,想要学习和提升专业能力从这里开始,充足技术干货等你来啃,轻松拿下 Dream Offer!

    209 引用 • 72 回帖
  • Ubuntu

    Ubuntu(友帮拓、优般图、乌班图)是一个以桌面应用为主的 Linux 操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词,意思是“人性”、“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想。Ubuntu 的目标在于为一般用户提供一个最新的、同时又相当稳定的主要由自由软件构建而成的操作系统。

    127 引用 • 169 回帖
  • PWA

    PWA(Progressive Web App)是 Google 在 2015 年提出、2016 年 6 月开始推广的项目。它结合了一系列现代 Web 技术,在网页应用中实现和原生应用相近的用户体验。

    14 引用 • 69 回帖 • 177 关注
  • 禅道

    禅道是一款国产的开源项目管理软件,她的核心管理思想基于敏捷方法 scrum,内置了产品管理和项目管理,同时又根据国内研发现状补充了测试管理、计划管理、发布管理、文档管理、事务管理等功能,在一个软件中就可以将软件研发中的需求、任务、bug、用例、计划、发布等要素有序的跟踪管理起来,完整地覆盖了项目管理的核心流程。

    6 引用 • 15 回帖 • 27 关注
  • 反馈

    Communication channel for makers and users.

    126 引用 • 930 回帖 • 272 关注
  • Q&A

    提问之前请先看《提问的智慧》,好的问题比好的答案更有价值。

    9571 引用 • 43560 回帖 • 97 关注
  • Kotlin

    Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,由 JetBrains 设计开发并开源。Kotlin 可以编译成 Java 字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。在 Google I/O 2017 中,Google 宣布 Kotlin 成为 Android 官方开发语言。

    19 引用 • 33 回帖 • 79 关注
  • Linux

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

    952 引用 • 944 回帖
  • DevOps

    DevOps(Development 和 Operations 的组合词)是一组过程、方法与系统的统称,用于促进开发(应用程序/软件工程)、技术运营和质量保障(QA)部门之间的沟通、协作与整合。

    57 引用 • 25 回帖 • 3 关注
  • Vue.js

    Vue.js(读音 /vju ː/,类似于 view)是一个构建数据驱动的 Web 界面库。Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。

    267 引用 • 666 回帖
  • Dubbo

    Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是 [阿里巴巴] SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

    60 引用 • 82 回帖 • 608 关注
  • QQ

    1999 年 2 月腾讯正式推出“腾讯 QQ”,在线用户由 1999 年的 2 人(马化腾和张志东)到现在已经发展到上亿用户了,在线人数超过一亿,是目前使用最广泛的聊天软件之一。

    45 引用 • 557 回帖 • 1 关注
  • App

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

    91 引用 • 384 回帖
  • FlowUs

    FlowUs.息流 个人及团队的新一代生产力工具。

    让复杂的信息管理更轻松、自由、充满创意。

    1 引用 • 2 关注
  • 设计模式

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

    200 引用 • 120 回帖
  • Flume

    Flume 是一套分布式的、可靠的,可用于有效地收集、聚合和搬运大量日志数据的服务架构。

    9 引用 • 6 回帖 • 653 关注
  • SQLite

    SQLite 是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是全世界使用最为广泛的数据库引擎。

    5 引用 • 7 回帖 • 1 关注
  • Quicker

    Quicker 您的指尖工具箱!操作更少,收获更多!

    36 引用 • 155 回帖
  • GraphQL

    GraphQL 是一个用于 API 的查询语言,是一个使用基于类型系统来执行查询的服务端运行时(类型系统由你的数据定义)。GraphQL 并没有和任何特定数据库或者存储引擎绑定,而是依靠你现有的代码和数据支撑。

    4 引用 • 3 回帖 • 5 关注