0
点赞
收藏
分享

微信扫一扫

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

endif

xmake_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/xmake

tip:
@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-directory

install:
@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,就完成了编译:

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


执行命令make install,就安装到当前目录了:

c++构建工具之xmake使用实例_动态库_02


编译完成了,主程序xmake在bin里面,使用./bin/xmake -h看看帮助:

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


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

创建一个控制台工程:

c++构建工具之xmake使用实例_动态库_04


创建一个动态库工程:

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


创建一个静态库工程:

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

生成工程的目录结构是如下:


c++构建工具之xmake使用实例_动态库_07


工程创建好了以后,里面就有了一个xmake.lua的编译脚本,动态库和静态库还有一个测试demo

-- the debug mode
if is_mode("debug") then

-- enable the debug symbols
set_symbols("debug")

-- disable optimization
set_optimize("none")
end

-- the release mode
if is_mode("release") then

-- set the symbols visibility: hidden
set_symbols("hidden")

-- enable fastest optimization
set_optimize("fastest")

-- strip all symbols
set_strip("all")
end

-- add target
target("console")

-- set kind
set_kind("binary")

-- add files
add_files("src/*.cpp")


-- the debug mode
if is_mode("debug") then

-- enable the debug symbols
set_symbols("debug")

-- disable optimization
set_optimize("none")
end

-- the release mode
if is_mode("release") then

-- set the symbols visibility: hidden
set_symbols("hidden")

-- enable fastest optimization
set_optimize("fastest")

-- strip all symbols
set_strip("all")
end

-- add target
target("console2")

-- set kind
set_kind("static")

-- add files
add_files("src/interface.cpp")

-- add target
target("console2_demo")

-- set kind
set_kind("binary")

-- add deps
add_deps("console2")

-- add files
add_files("src/test.cpp")


-- the debug mode
if is_mode("debug") then

-- enable the debug symbols
set_symbols("debug")

-- disable optimization
set_optimize("none")
end

-- the release mode
if is_mode("release") then

-- set the symbols visibility: hidden
set_symbols("hidden")

-- enable fastest optimization
set_optimize("fastest")

-- strip all symbols
set_strip("all")
end

-- add target
target("console3")

-- set kind
set_kind("shared")

-- add files
add_files("src/interface.cpp")

-- add target
target("console3_demo")

-- set kind
set_kind("binary")

-- add deps
add_deps("console3")

-- add files
add_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个工程,由于我不是安装在系统目录,也没有配置环境变量,所以执行的时候要带目录:

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


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 windows
add_defines("NOCRYPT", "NOGDI")

-- link libcmt.lib
add_cxflags("-MT")

-- no msvcrt.lib
add_ldflags("-nodefaultlib:\"msvcrt.lib\"")
end

-- for mode coverage
if is_mode("coverage") then
add_ldflags("-coverage", "-fprofile-arcs", "-ftest-coverage")
end

-- add projects
includes("src/sv","src/luajit", "src/tbox", "src/xmake", "src/demo")


举报

相关推荐

0 条评论