Usina BRAdvPL Guide
Sign in
← All topics
FOUNDATIONPublished

AdvPL compiler directives

#define · #undef · #include · #ifdef · #ifndef · #else · #endif · #command · #translate

Control preprocessing, share definitions, and select which source blocks reach the compiler.

PreprocessorCompilationIncludesConstantsConditional compilation
01 · OVERVIEW

Overview

Directives are handled by the preprocessor before compilation. A useful analogy is preparing a recipe before baking: headers are included, symbols expanded, syntax translated, and blocks selected; the compiler receives only the resulting source. #define and #include are conceptually similar to C/C++, but #include is not a complete modern module system.

02 · SYNTAX

Syntax

#define · #undef · #include · #ifdef · #ifndef · #else · #endif · #command · #translate

Parameters

#define
DiretivaOptional

Defines identifiers, manifest constants, or pseudofunctions expanded by the preprocessor.

#undef
DiretivaOptional

Removes an active definition from the point where the directive appears.

#include
DiretivaOptional

Inserts a header file at the current point using the compiler include path.

#ifdef
DiretivaOptional

Includes a block in compilation only when a given identifier is defined.

#ifndef
DiretivaOptional

Includes a block only when a given identifier is not defined.

#else e #endif
DiretivaOptional

Provides the alternative path and closes conditional compilation blocks.

#command e #translate
DiretivaOptional

Creates syntax-transformation rules applied by the preprocessor.

03 · PRACTICAL EXAMPLE

Constant and conditional compilation

#define LIMITE_ITENS 100
#define LOG_DETALHADO

User Function ExDiretivas()
    Local nItens := 120

#ifdef LOG_DETALHADO
    ConOut("Itens recebidos: " + CValToChar(nItens))
#endif

    If nItens > LIMITE_ITENS
        MsgAlert("Limite excedido")
    EndIf
Return
Expected result

Symbols are resolved before compilation; runtime If remains a runtime decision.

04 · PRACTICAL EXAMPLE

Default value with #ifndef

#ifndef TAMANHO_LOTE
    #define TAMANHO_LOTE 50
#endif

User Function ExLote()
    ConOut("Tamanho do lote: " + CValToChar(TAMANHO_LOTE))
Return
Expected result

The default is used only if no earlier definition exists.

05 · PRACTICAL EXAMPLE

Specific include

#include "TOTVS.ch"
#include "FWMVCDef.ch"

User Function ExCabecalhos()
    // Utilize símbolos do Framework MVC somente após incluir
    // o cabeçalho compatível com o ambiente.
Return
Expected result

Header content participates in source preprocessing.

BEST PRACTICES
  • Directives run before execution; runtime If/Else makes decisions while the routine runs.
  • Use manifest constants to remove magic values, conditional compilation only when needed, and release-compatible includes.
  • Use runtime configuration for values that must change without recompilation.
  • #command and #translate are advanced; application clarity usually matters more than new syntax.
COMMON PITFALLS
  • Never put secrets in #define.
  • Avoid client-specific compilation symbols when runtime parameters or extensions solve the problem.
  • Do not assume every source needs both PROTHEUS.CH and TOTVS.CH.
  • AAdd() is a library function, not a #command invention.

Related content

REFERENCES
  1. TOTVS. #define. TDN. Última alteração em 22 mar. 2019.
  2. TOTVS. Diretivas para o compilador. TDN.
  3. TOTVS. #ifdef. TDN.
  4. TOTVS. #ifndef. TDN.
  5. TOTVS. #include. TDN.
  6. TOTVS. #undef. TDN.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR