c++构建工具之xmake使用实例
1.下載源碼編譯安裝
- 在線源碼
- 在線文檔
makefile中有默認編譯完成的安裝路徑:
prefix:=$(if $(prefix),$(prefix),$(if $(findstring /usr/local/bin,$(PATH)),/usr/local,/usr))由于不想安裝在系統中,修改為安裝到當前目錄(安裝在系統目錄的好處就是可以直接使用xmake命令不用帶目錄也不用配置環境變量):
prefix=.下面是自帶編譯的makefile,里面可以看見對各種平臺的支持: # is debug? debug :=n verbose:=#debug :=y #verbose:=-v# prefix #prefix:=$(if $(prefix),$(prefix),$(if $(findstring /usr/local/bin,$(PATH)),/usr/local,/usr)) prefix=.# platform PLAT :=$(if $(PLAT),$(PLAT),$(if ${shell uname | egrep -i linux},linux,)) PLAT :=$(if $(PLAT),$(PLAT),$(if ${shell uname | egrep -i darwin},macosx,)) PLAT :=$(if $(PLAT),$(PLAT),$(if ${shell uname | egrep -i cygwin},cygwin,)) PLAT :=$(if $(PLAT),$(PLAT),$(if ${shell uname | egrep -i mingw},mingw,)) PLAT :=$(if $(PLAT),$(PLAT),$(if ${shell uname | egrep -i windows},windows,)) PLAT :=$(if $(PLAT),$(PLAT),linux)# architecture ifeq ($(ARCH),)ARCH :=$(if $(findstring windows,$(PLAT)),x86,$(ARCH)) ARCH :=$(if $(findstring mingw,$(PLAT)),x86,$(ARCH)) ARCH :=$(if $(findstring macosx,$(PLAT)),x$(shell getconf LONG_BIT),$(ARCH)) ARCH :=$(if $(findstring linux,$(PLAT)),x$(shell getconf LONG_BIT),$(ARCH)) ARCH :=$(if $(findstring x32,$(ARCH)),i386,$(ARCH)) ARCH :=$(if $(findstring x64,$(ARCH)),x86_64,$(ARCH)) ARCH :=$(if $(findstring iphoneos,$(PLAT)),armv7,$(ARCH)) ARCH :=$(if $(findstring android,$(PLAT)),armv7,$(ARCH))endifxmake_dir_install :=$(prefix)/share/xmake xmake_core :=./core/src/demo/demo.b xmake_core_install :=$(xmake_dir_install)/xmake xmake_loader :=/tmp/xmake_loader xmake_loader_install:=$(prefix)/bin/xmaketip:@echo 'Usage: '@echo ' $ make build'@echo ' $ sudo make install [prefix=/usr/local]'build:@echo compiling xmake-core ...@if [ -f core/.config.mak ]; then rm core/.config.mak; fi@$(MAKE) -C core --no-print-directory f DEBUG=$(debug)@$(MAKE) -C core --no-print-directory c@$(MAKE) -C core --no-print-directoryinstall:@echo installing to $(prefix) ...@echo plat: $(PLAT)@echo arch: $(ARCH)@# create the xmake install directory@if [ -d $(xmake_dir_install) ]; then rm -rf $(xmake_dir_install); fi@if [ ! -d $(xmake_dir_install) ]; then mkdir -p $(xmake_dir_install); fi@# install the xmake core file@cp $(xmake_core) $(xmake_core_install)@chmod 777 $(xmake_core_install)@# install the xmake directory@cp -r xmake/* $(xmake_dir_install)@# make the xmake loader@echo '#!/bin/bash' > $(xmake_loader)@echo 'export XMAKE_PROGRAM_DIR=$(xmake_dir_install)' >> $(xmake_loader)@echo '$(xmake_core_install) $(verbose) "$$@"' >> $(xmake_loader)@# install the xmake loader@if [ ! -d $(prefix)/bin ]; then mkdir -p $(prefix)/bin; fi@mv $(xmake_loader) $(xmake_loader_install)@chmod 777 $(xmake_loader_install)@# remove xmake.out@if [ -f '/tmp/xmake.out' ]; then rm /tmp/xmake.out; fi@# ok@echo ok!uninstall:@echo uninstalling from $(prefix) ...@if [ -f $(xmake_loader_install) ]; then rm $(xmake_loader_install); fi@if [ -d $(xmake_dir_install) ]; then rm -rf $(xmake_dir_install); fi@echo ok!test:@xmake lua --backtrace tests/test.lua $(name)@echo ok!.PHONY: tip build install uninstall
執行命令make build,就完成了編譯:
執行命令make install,就安裝到當前目錄了:
編譯完成了,主程序xmake在bin里面,使用./bin/xmake -h看看幫助:
2.使用模板生成工程
使用命令模板可以創建不同的工程,下面是不同的選項:
默認語言是c, 后面的-t和--template參數指定的是需要創建的模板類型,目前只支持console、靜態庫、動態庫三種模板,后續還會支持:application等app應用程序模板。
??? -l LANGUAGE, --language=LANGUAGE?????? The project language (default: c)
?????????????????????????????????????????????? - c
?????????????????????????????????????????????? - c++
?????????????????????????????????????????????? - objc
?????????????????????????????????????????????? - objc++
?????????????????????????????????????????????? - swift
??? -t TEMPLATE, --template=TEMPLATE?????? Select the project template id of the given language. (default: 1)
?????????????????????????????????????????????? - language: c
???????????????????????????????????????????????? 1. The Console Program
???????????????????????????????????????????????? 2. The Console Program (tbox)
???????????????????????????????????????????????? 3. The Shared Library
???????????????????????????????????????????????? 4. The Shared Library (tbox)
???????????????????????????????????????????????? 5. The Static Library
???????????????????????????????????????????????? 6. The Static Library (tbox)
?????????????????????????????????????????????? - language: c++
???????????????????????????????????????????????? 1. The Console Program
???????????????????????????????????????????????? 2. The Console Program (tbox)
???????????????????????????????????????????????? 3. The Shared Library
???????????????????????????????????????????????? 4. The Shared Library (tbox)
???????????????????????????????????????????????? 5. The Static Library
???????????????????????????????????????????????? 6. The Static Library (tbox)
?????????????????????????????????????????????? - language: objc
???????????????????????????????????????????????? 1. The Console Program
?????????????????????????????????????????????? - language: objc++
???????????????????????????????????????????????? 1. The Console Program
?????????????????????????????????????????????? - language: swift
???????????????????????????????????????????????? 1. The Console Program
創建一個控制臺工程:
創建一個動態庫工程:
創建一個靜態庫工程:
生成工程的目錄結構是如下:
工程創建好了以后,里面就有了一個xmake.lua的編譯腳本,動態庫和靜態庫還有一個測試demo
-- the debug mode if is_mode("debug") then-- enable the debug symbolsset_symbols("debug")-- disable optimizationset_optimize("none") end-- the release mode if is_mode("release") then-- set the symbols visibility: hiddenset_symbols("hidden")-- enable fastest optimizationset_optimize("fastest")-- strip all symbolsset_strip("all") end-- add target target("console")-- set kindset_kind("binary")-- add filesadd_files("src/*.cpp")-- the debug mode if is_mode("debug") then-- enable the debug symbolsset_symbols("debug")-- disable optimizationset_optimize("none") end-- the release mode if is_mode("release") then-- set the symbols visibility: hiddenset_symbols("hidden")-- enable fastest optimizationset_optimize("fastest")-- strip all symbolsset_strip("all") end-- add target target("console2")-- set kindset_kind("static")-- add filesadd_files("src/interface.cpp") -- add target target("console2_demo")-- set kindset_kind("binary")-- add depsadd_deps("console2")-- add filesadd_files("src/test.cpp")
-- the debug mode if is_mode("debug") then-- enable the debug symbolsset_symbols("debug")-- disable optimizationset_optimize("none") end-- the release mode if is_mode("release") then-- set the symbols visibility: hiddenset_symbols("hidden")-- enable fastest optimizationset_optimize("fastest")-- strip all symbolsset_strip("all") end-- add target target("console3")-- set kindset_kind("shared")-- add filesadd_files("src/interface.cpp") -- add target target("console3_demo")-- set kindset_kind("binary")-- add depsadd_deps("console3")-- add filesadd_files("src/test.cpp")
從這3個lua腳本我們可以看出,創建不同類型的工程,就是set_kind這個類型不一樣了。
選一個動態庫工程看看生成的代碼:
#ifdef __cplusplus extern "C" { #endif#if defined(_WIN32) # define __export __declspec(dllexport) #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) # define __export __attribute__((visibility("default"))) #else # define __export #endif/*! calculate add(a, b) ** @param a the first argument* @param b the second argument** @return the result*/ __export int add(int a, int b);#ifdef __cplusplus } #endif#include "interface.h"int add(int a, int b) {return a + b; }
#include "interface.h" #include <iostream>using namespace std;int main(int argc, char** argv) {cout << "add(1, 2) = " << add(1, 2) << endl; return 0; }
3.使用lua腳本編譯
使用xmake命令分別編譯3個工程,由于我不是安裝在系統目錄,也沒有配置環境變量,所以執行的時候要帶目錄:
4.lua腳本例子
-- project set_project("xmake")-- version set_version("2.1.7", {build = "%Y%m%d%H%M"})-- set xmake min version set_xmakever("2.1.6")-- set warning all as error set_warnings("all", "error")-- set language: c99, c++11 set_languages("c99", "cxx11")-- disable some compiler errors add_cxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing", "-Wno-error=nullability-completeness")-- add defines add_defines("_GNU_SOURCE=1", "_FILE_OFFSET_BITS=64", "_LARGEFILE_SOURCE")-- set the symbols visibility: hidden set_symbols("hidden")-- strip all symbols set_strip("all")-- fomit the frame pointer add_cxflags("-fomit-frame-pointer")-- for the windows platform (msvc) if is_plat("windows") then -- add some defines only for windowsadd_defines("NOCRYPT", "NOGDI")-- link libcmt.libadd_cxflags("-MT") -- no msvcrt.libadd_ldflags("-nodefaultlib:\"msvcrt.lib\"") end-- for mode coverage if is_mode("coverage") thenadd_ldflags("-coverage", "-fprofile-arcs", "-ftest-coverage") end-- add projects includes("src/sv","src/luajit", "src/tbox", "src/xmake", "src/demo")5.參考資料
http://blog.csdn.net/earbao/article/details/52238568
總結
以上是生活随笔為你收集整理的c++构建工具之xmake使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++构建工具之shell,config
- 下一篇: c++构建工具之make使用小结