首页 > 解决方案 > 在函数返回类型之前“定义”

问题描述

在研究 Dear ImGui 的代码时,我偶然发现了函数声明,例如

IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL);

其中 IMGUI_API 只是从其他地方定义的:

#ifndef IMGUI_IMPL_API
#define IMGUI_IMPL_API              IMGUI_API
#endif

我写了一个测试程序,但除了 C++ 支持它之外什么也没学到:

#include <iostream>

#define DEF

DEF bool func() {
    return true;
}

int main()
{
    std::cout << func() << std::endl;
}

所以我的问题是那个东西叫什么,目的是什么?

标签: c++c-preprocessor

解决方案


它们用于导入和导出功能。定义被声明imconfig.h但被注释掉;如果他们想要或需要在 DLL 上下文中使用 Dear Imgui,打算让用户取消注释:

imconfig.h 的前 25 行:

//-----------------------------------------------------------------------------
// COMPILE-TIME OPTIONS FOR DEAR IMGUI
// Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure.
// You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions.
//-----------------------------------------------------------------------------
// A) You may edit imconfig.h (and not overwrite it when updating imgui, or maintain a patch/branch with your modifications to imconfig.h)
// B) or add configuration directives in your own file and compile with #define IMGUI_USER_CONFIG "myfilename.h"
// If you do so you need to make sure that configuration settings are defined consistently _everywhere_ dear imgui is used, which include
// the imgui*.cpp files but also _any_ of your code that uses imgui. This is because some compile-time options have an affect on data structures.
// Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts.
// Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using.
//-----------------------------------------------------------------------------

#pragma once

//---- Define assertion handler. Defaults to calling assert().
//#define IM_ASSERT(_EXPR)  MyAssert(_EXPR)
//#define IM_ASSERT(_EXPR)  ((void)(_EXPR))     // Disable asserts

//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows.
//#define IMGUI_API __declspec( dllexport )
//#define IMGUI_API __declspec( dllimport )

//---- Don't define obsolete functions/enums names. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names.
//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS

推荐阅读