直接上代码首先创建一个项目名字叫sharedTest
foo.h
cpp
#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
__export int add(int a, int b);
foo.c
cpp
#include "foo.h"
int add(int a, int b) {
return a + b;
}
main.c
cpp
#include "foo.h"
#include <stdio.h>
int main(int argc, char** argv) {
printf("add(1, 2) = %d\n", add(1, 2));
return 0;
}
lua
add_rules("mode.debug", "mode.release")
target("foo")
set_kind("shared")
add_files("src/foo.c")
target("sharedTest")
set_kind("binary")
add_deps("foo")
add_files("src/main.c")
--
-- If you want to known more usage about xmake, please see https://xmake.io
--
-- ## FAQ
--
-- You can enter the project directory firstly before building project.
--
-- $ cd projectdir
--
-- 1. How to build project?
--
-- $ xmake
--
-- 2. How to configure project?
--
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
--
-- 3. Where is the build output directory?
--
-- The default output directory is `./build` and you can configure the output directory.
--
-- $ xmake f -o outputdir
-- $ xmake
--
-- 4. How to run and debug target after building project?
--
-- $ xmake run [targetname]
-- $ xmake run -d [targetname]
--
-- 5. How to install target to the system directory or other output directory?
--
-- $ xmake install
-- $ xmake install -o installdir
--
-- 6. Add some frequently-used compilation flags in xmake.lua
--
-- @code
-- -- add debug and release modes
-- add_rules("mode.debug", "mode.release")
--
-- -- add macro definition
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
--
-- -- set warning all as error
-- set_warnings("all", "error")
--
-- -- set language: c99, c++11
-- set_languages("c99", "c++11")
--
-- -- set optimization: none, faster, fastest, smallest
-- set_optimize("fastest")
--
-- -- add include search directories
-- add_includedirs("/usr/include", "/usr/local/include")
--
-- -- add link libraries and search directories
-- add_links("tbox")
-- add_linkdirs("/usr/local/lib", "/usr/lib")
--
-- -- add system link libraries
-- add_syslinks("z", "pthread")
--
-- -- add compilation and link flags
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
--
-- @endcode
--
编译脚本
build.bat
bash
@echo off
setlocal enabledelayedexpansion
:: ----------------------
:: 配置参数
:: ----------------------
:: 允许通过环境变量覆盖NDK路径(默认值D:\hmosSdk\15\native)
set NDK_PATH=!NDK_PATH!
if "!NDK_PATH!"=="" set NDK_PATH=D:\hmosSdk\15\native
:: 目标架构列表(可扩展)
set ARCH_LIST=arm64-v8a x86_64
set MODE_LIST=debug release
:: ----------------------
:: 主逻辑
:: ----------------------
cls
echo [INFO] 当前NDK路径: "%NDK_PATH%"
echo [INFO] 目标架构: %ARCH_LIST%
:: 检查xmake是否存在
where xmake >nul 2>&1
if %ERRORLEVEL% neq 0 (
echo [ERROR] 未找到xmake命令,请确保已添加到PATH
exit /b 1
)
:: 循环构建debug 和release
for %%m in (%MODE_LIST%) do (
:: 循环构建所有架构
for %%a in (%ARCH_LIST%) do (
echo.
echo [INFO] 正在配置架构: %%a
xmake f -p harmony -a %%a -m %%m --sdk="%NDK_PATH%"
if !ERRORLEVEL! neq 0 (
echo [ERROR] 配置架构 %%a 失败
exit /b 1
)
echo [INFO] 正在构建架构: %%a
xmake build
if !ERRORLEVEL! neq 0 (
echo [ERROR] 构建架构 %%a 失败
exit /b 1
)
)
)
echo.
echo [SUCCESS] 所有架构构建完成
endlocal