日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++构建工具之xmake使用实例

發(fā)布時(shí)間:2025/3/21 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++构建工具之xmake使用实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.下載源碼編譯安裝

  • 在線源碼
  • 在線文檔

makefile中有默認(rèn)編譯完成的安裝路徑:

prefix:=$(if $(prefix),$(prefix),$(if $(findstring /usr/local/bin,$(PATH)),/usr/local,/usr))

由于不想安裝在系統(tǒng)中,修改為安裝到當(dāng)前目錄(安裝在系統(tǒng)目錄的好處就是可以直接使用xmake命令不用帶目錄也不用配置環(huán)境變量):

prefix=.
下面是自帶編譯的makefile,里面可以看見對各種平臺(tái)的支持:

# 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
執(zhí)行命令make build,就完成了編譯:



執(zhí)行命令make install,就安裝到當(dāng)前目錄了:


編譯完成了,主程序xmake在bin里面,使用./bin/xmake -h看看幫助:



2.使用模板生成工程

使用命令模板可以創(chuàng)建不同的工程,下面是不同的選項(xiàng):

默認(rèn)語言是c, 后面的-t和--template參數(shù)指定的是需要?jiǎng)?chuàng)建的模板類型,目前只支持console、靜態(tài)庫、動(dòng)態(tài)庫三種模板,后續(xù)還會(huì)支持:application等app應(yīng)用程序模板。

??? -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

創(chuàng)建一個(gè)控制臺(tái)工程:



創(chuàng)建一個(gè)動(dòng)態(tài)庫工程:



創(chuàng)建一個(gè)靜態(tài)庫工程:


生成工程的目錄結(jié)構(gòu)是如下:


工程創(chuàng)建好了以后,里面就有了一個(gè)xmake.lua的編譯腳本,動(dòng)態(tài)庫和靜態(tài)庫還有一個(gè)測試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個(gè)lua腳本我們可以看出,創(chuàng)建不同類型的工程,就是set_kind這個(gè)類型不一樣了。

選一個(gè)動(dòng)態(tài)庫工程看看生成的代碼:

#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個(gè)工程,由于我不是安裝在系統(tǒng)目錄,也沒有配置環(huán)境變量,所以執(zhí)行的時(shí)候要帶目錄:



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

總結(jié)

以上是生活随笔為你收集整理的c++构建工具之xmake使用实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。