Use Xmake-API to build the project

foreword

 In the previous article, we introduced the basic commands of Xmake. Using Xmake commands to build a project is basically equivalent to using g++ to build directly, so we can choose to use commands to build only when the project is relatively simple, but when the project is more complex, you can Use xmake.lua to configure the project's build process.

1. Introduction to common APIs

 For beginners of Xmake, it is very convenient to configure our project as long as you learn a few simple APIs. Among them, the most commonly used APIs are

  • set_kind: set target type
  • add_files: Add source files/static libraries that need to be compiled
  • add_links: Add the dynamic library that needs to be linked
  • add_includedirs: add header file search path
  • add_linkdirs: Add dynamic library search path
  • add_ldflags: add link parameter
  • add_cxxflags: Add C++ compile option
  • add_cflags: Add C compiler options
  • add_cxflags: Add C/C++ compilation option

2. API use

 In the newly created project using Xmake, Xmake directly creates a xmake.lua, the content of which is as follows:

add_rules("mode.debug", "mode.release")

target("hello")
    set_kind("binary")
    add_files("src/*.cpp")

 Among them, add_rulesit is to set the build mode, where the debug mode and release mode are set

set_kindHere it is specified that the compiled target is a binary file, and the program can be executed

add_filesAdd all cpp file headers under src, add_filessupport wildcards

The following describes how the requirements in our construction are realized through these APIs

1. Add compiled source files

 As shown in the above example, when we need to add source files to the compilation scope, we only need to add_filesadd the files. add_filesThe supported file types are as follows:

Supported source file types Description
.c/.cpp/.cc/.cxx c++ file
.s/.S/.asm assembly files
.m/.mm objc file
.swift swift file
.go grow file
.o/.obj object File
.a/.lib static library files, will automatically merge the library to the target program
.rc msvc resource file
.manifest windows manifest file
.dll windows export file
.ld/.lds linker scripts file for gcc/clang
.map/.ver version script file for gcc/clang

 Among them, the wildcard * means to match files in the current directory, and ** means to match files in multi-level directories.

2. Add header file search path

 When adding the search path of the header file, you can use it add_includedirs. Of course, you can also directly set it through the add_cxflagsor add_mxflagsinterface, which is also possible.

3. Add dynamic library

 If you need to link the dynamic library, you can add_linksadd the dynamic library through to. The dynamic library that needs to be added here needs to remove the beginning lib and the suffix .so.

4. Add dynamic library search path

add_linkdirsAdd the search path of the dynamic library by passing, and add the header file search path by  passing add_includedirs.

5. Add external packages

 Xmake supports add_requiresadding dependent packages through, we can limit the version of the package by adding the version number, or use the latest version without writing the version number, for example, usually we don’t need its too complicated usage

add_requires("tbox 1.6.*", "pcre 8.x", "libpng ^1.18")
add_requires("libpng ~1.16", "zlib 1.1.2 || >=1.2.11 <1.3.0")
add_requires("tbox", "libpng", "zlib")

6. Inheritance of Xmake configuration

 The inheritance mentioned here means that if a folder contains several projects managed by xmake, then the xmake configuration in the parent directory will be inherited to the xmake project in the subdirectory, for example, by adding the header in the parent add_includedirsdirectory The file search path, then the items in the subdirectory do not need to be added repeatedly. More advanced usage includes defining global variables, macro definitions, etc., which greatly facilitates project management.

hello
├── src
│   ├── main.cpp
│   └── subhello
│       ├── src
│       │   └── main.cpp
│       └── xmake.lua
└── xmake.lua

 Beginner Xmake users can complete the construction of an ordinary project through the API described above. If the project is more complex, it may require more advanced usage. You can refer to the official documentation of Xmake to study more complex usage.

Guess you like

Origin blog.csdn.net/dddgggd/article/details/129019373