您当前的位置: 首页 >  c++

phymat.nico

暂无认证

  • 2浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

phymat.nico 发布时间:2017-11-06 10:14:25 ,浏览量:2

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

执行命令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 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 

using namespace std;

int main(int argc, char** argv)
{
    cout             
关注
打赏
1659628745
查看更多评论
0.2273s