如何快速構建一個簡單的程序

首先我們通過內置的工程模板創建一個空工程:

$ xmake create -P ./hellonncreate hello ...ncreate ok!??n

這個時候xmake將會產生一些工程文件,如下:

$ cd ./hellon$ tree .nn.n├── srcn│ └── main.cn└── xmake.luan

這個簡單的程序僅僅只是為了列印輸出: hello xmake!

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

xmake.lua是基於lua語法的工程描述文件,它很簡單:

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

現在我們開始編譯這個程序

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

接著運行它:

$ xmake run hellonnhello world!n

或者進行調試

$ xmake run -d hello nn[lldb]$target create "build/hello"nCurrent executable set to build/hello (x86_64).n[lldb]$b mainnBreakpoint 1: where = hello`main, address = 0x0000000100000f50n[lldb]$rnProcess 7509 launched: /private/tmp/hello/build/hello (x86_64)nProcess 7509 stoppedn* thread #1: tid = 0x435a2, 0x0000000100000f50 hello`main, queue = com.apple.main-thread, stop reason = breakpoint 1.1n frame #0: 0x0000000100000f50 hello`mainnhello`main:n-> 0x100000f50 <+0>: pushq %rbpn 0x100000f51 <+1>: movq %rsp, %rbpn 0x100000f54 <+4>: leaq 0x2b(%rip), %rdi ; "hello world!"n 0x100000f5b <+11>: callq 0x100000f64 ; symbol stub for: putsn[lldb]$n

接著我們嘗試構建一個android版本,這個時候得設置ndk路徑,當然也能配置到全局配置中,一勞永逸

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

或者我們編一個iphoneos的版本,例如:

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

最後我們嘗試為mingw平台進行編譯,sdk指定交叉工具鏈目錄,交叉編譯linux平台也可以這麼用哦。。

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

xmake還能直接在windows的cmd終端下,進行直接編譯windows的程序,它會去自動檢測當前系統裝的vs環境,調用裡面的cl.exe編譯器進行編譯,一切都是自動化的,我們不需要額外配置什麼,只需要執行:xmake 就行了。。

例如:

$ xmakennchecking for the architecture ... x86nchecking for the Microsoft Visual Studio version ... 2008nchecking for the c compiler (cc) ... cl.exenchecking for the c++ compiler (cxx) ... cl.exenchecking for the assember (as) ... ml.exenchecking for the linker (ld) ... link.exenchecking for the static library archiver (ar) ... link.exe -libnchecking for the shared library linker (sh) ... link.exe -dllnchecking for the static library extractor (ex) ... lib.exenconfiguren{n ex = "lib.exe"n, sh = "link.exe -dll"n, host = "windows"n, ar = "link.exe -lib"n, as = "ml.exe"n, plat = "windows"n, buildir = "build"n, arch = "x86"n, cc = "cl.exe"n, cxx = "cl.exe"n, mode = "release"n, clean = truen, kind = "static"n, ld = "link.exe"n, vs = "2008"n}nconfigure ok!n[00%]: compiling.release srcmain.cn[100%]: linking.release hello.exenbuild ok!n

順便說一下,在windows下編譯,xmake是完全支持多任務的哦,默認就是自動多任務構建的,比起以前在msys, cygwin裡面用 gmake來編譯快多了,因為windows下的gmake就算你啟用了-j 4 也沒啥效果,非常非常得慢。。。

最後給個演示視頻:

演示視頻

個人主頁:TBOOX開源工程

原文出處:tboox.org/cn/2016/07/16

推薦閱讀:

為什麼會選擇 make,cmake 之流來控制程序編譯,而不是用現成的腳本語言,如 Python?

TAG:Makefile | 跨平台 | 编译软件 |