函数返回类型之前的“定义”

时间:2019-09-09 17:25:30

标签: c++ c-preprocessor

当我学习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;
}

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

1 个答案:

答案 0 :(得分:1)

它们用于导入和导出功能。该定义在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