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

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

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

$ 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
    16 引用 • 17 回帖 • 1 关注
  • Linux

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

    915 引用 • 931 回帖 • 1 关注
  • C

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

    83 引用 • 165 回帖 • 43 关注

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • Hibernate

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

    39 引用 • 103 回帖 • 684 关注
  • 强迫症

    强迫症(OCD)属于焦虑障碍的一种类型,是一组以强迫思维和强迫行为为主要临床表现的神经精神疾病,其特点为有意识的强迫和反强迫并存,一些毫无意义、甚至违背自己意愿的想法或冲动反反复复侵入患者的日常生活。

    15 引用 • 161 回帖 • 4 关注
  • 游戏

    沉迷游戏伤身,强撸灰飞烟灭。

    169 引用 • 799 回帖 • 1 关注
  • Ngui

    Ngui 是一个 GUI 的排版显示引擎和跨平台的 GUI 应用程序开发框架,基于
    Node.js / OpenGL。目标是在此基础上开发 GUI 应用程序可拥有开发 WEB 应用般简单与速度同时兼顾 Native 应用程序的性能与体验。

    7 引用 • 9 回帖 • 346 关注
  • etcd

    etcd 是一个分布式、高可用的 key-value 数据存储,专门用于在分布式系统中保存关键数据。

    5 引用 • 26 回帖 • 491 关注
  • Vim

    Vim 是类 UNIX 系统文本编辑器 Vi 的加强版本,加入了更多特性来帮助编辑源代码。Vim 的部分增强功能包括文件比较(vimdiff)、语法高亮、全面的帮助系统、本地脚本(Vimscript)和便于选择的可视化模式。

    27 引用 • 66 回帖 • 1 关注
  • Solidity

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

    3 引用 • 18 回帖 • 350 关注
  • 微信

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

    129 引用 • 793 回帖 • 1 关注
  • DNSPod

    DNSPod 建立于 2006 年 3 月份,是一款免费智能 DNS 产品。 DNSPod 可以为同时有电信、网通、教育网服务器的网站提供智能的解析,让电信用户访问电信的服务器,网通的用户访问网通的服务器,教育网的用户访问教育网的服务器,达到互联互通的效果。

    6 引用 • 26 回帖 • 521 关注
  • BND

    BND(Baidu Netdisk Downloader)是一款图形界面的百度网盘不限速下载器,支持 Windows、Linux 和 Mac,详细介绍请看这里

    107 引用 • 1281 回帖 • 23 关注
  • RabbitMQ

    RabbitMQ 是一个开源的 AMQP 实现,服务器端用 Erlang 语言编写,支持多种语言客户端,如:Python、Ruby、.NET、Java、C、PHP、ActionScript 等。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。

    49 引用 • 60 回帖 • 395 关注
  • 深度学习

    深度学习(Deep Learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法。

    40 引用 • 40 回帖
  • Laravel

    Laravel 是一套简洁、优雅的 PHP Web 开发框架。它采用 MVC 设计,是一款崇尚开发效率的全栈框架。

    19 引用 • 23 回帖 • 684 关注
  • 旅游

    希望你我能在旅途中找到人生的下一站。

    85 引用 • 895 回帖
  • Sublime

    Sublime Text 是一款可以用来写代码、写文章的文本编辑器。支持代码高亮、自动完成,还支持通过插件进行扩展。

    10 引用 • 5 回帖 • 1 关注
  • NGINX

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

    311 引用 • 546 回帖 • 34 关注
  • 以太坊

    以太坊(Ethereum)并不是一个机构,而是一款能够在区块链上实现智能合约、开源的底层系统。以太坊是一个平台和一种编程语言 Solidity,使开发人员能够建立和发布下一代去中心化应用。 以太坊可以用来编程、分散、担保和交易任何事物:投票、域名、金融交易所、众筹、公司管理、合同和知识产权等等。

    34 引用 • 367 回帖 • 2 关注
  • jsDelivr

    jsDelivr 是一个开源的 CDN 服务,可为 npm 包、GitHub 仓库提供免费、快速并且可靠的全球 CDN 加速服务。

    5 引用 • 31 回帖 • 44 关注
  • Telegram

    Telegram 是一个非盈利性、基于云端的即时消息服务。它提供了支持各大操作系统平台的开源的客户端,也提供了很多强大的 APIs 给开发者创建自己的客户端和机器人。

    5 引用 • 35 回帖 • 1 关注
  • Pipe

    Pipe 是一款小而美的开源博客平台。Pipe 有着非常活跃的社区,可将文章作为帖子推送到社区,来自社区的回帖将作为博客评论进行联动(具体细节请浏览 B3log 构思 - 分布式社区网络)。

    这是一种全新的网络社区体验,让热爱记录和分享的你不再感到孤单!

    131 引用 • 1114 回帖 • 152 关注
  • MySQL

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

    675 引用 • 535 回帖
  • Elasticsearch

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

    116 引用 • 99 回帖 • 267 关注
  • SMTP

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。

    4 引用 • 18 回帖 • 588 关注
  • MongoDB

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

    90 引用 • 59 回帖
  • ActiveMQ

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

    19 引用 • 13 回帖 • 626 关注
  • 思源笔记

    思源笔记是一款隐私优先的个人知识管理系统,支持完全离线使用,同时也支持端到端加密同步。

    融合块、大纲和双向链接,重构你的思维。

    18680 引用 • 69695 回帖
  • 996
    13 引用 • 200 回帖 • 1 关注