c++ - 99% CPU,3.51MB 没有 typedef

标签 c++ c++11

好的,我在标题中定义了大约 500 个函数指针,例如:

void (__stdcall *ptr_glAccum) (GLenum op, GLfloat value);
void (__stdcall *ptr_glActiveTextureARB) (GLenum texture);
void (__stdcall *ptr_glAlphaFunc) (GLenum func, GLclampf ref);
GLboolean (__stdcall *ptr_glAreTexturesResident) (GLsizei n, const GLuint *textures, GLboolean *residences);
void (__stdcall *ptr_glArrayElement) (GLint index);
void (__stdcall *ptr_glBegin) (GLenum mode);
void (__stdcall *ptr_glBindBufferARB) (GLenum target, GLuint buffer);
void (__stdcall *ptr_glBindTexture) (GLenum target, GLuint texture);
void (__stdcall *ptr_glBitmap) (GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, const GLubyte *bitmap);
void (__stdcall *ptr_glBlendFunc) (GLenum sfactor, GLenum dfactor);
void (__stdcall *ptr_glBufferDataARB) (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage);

等等。现在我没有放 typedef 或不想放的原因是因为我可以直接分配和使用上面的指针。但是,如果我使用 typedef,那么我需要创建一个所述类型的变量并分配给它,然后使用它。这只是将我的代码从 500 行增加到 1000 行以上。

现在,当我在每个函数指针的开头添加 typedef 时,我的 dll 为 300kb,编译时间不到 5 秒。但是,如果我删除上面显示的 typedef,它会飙升至 99% cpu编译并输出一个 3.51MB 的 dll 需要 3-4 分钟编译.. 一个关键字引起这么多麻烦真是令人发指。

在 DLL 的 def 文件中,它显示:

ptr_wglUseFontBitmapsA @940 DATA
ptr_wglUseFontBitmapsW @941 DATA
ptr_wglUseFontOutlinesA @942 DATA
ptr_wglUseFontOutlinesW @943 DATA

但是使用 typedef,那个“DATA”部分就消失了。

任何想法是什么让 typedef 如此特别以及为什么没有它的这种行为:S?我正在使用带有代码块 Windows-7 x64 3.7Ghz I7 8Gb Ram 的 Mingw G++ 4.7.2,编译器输出为:

-------------- Clean: Release in OpenGL32 (compiler: GNU GCC Compiler)---------------

Cleaned "OpenGL32 - Release"

-------------- Build: Release in OpenGL32 (compiler: GNU GCC Compiler)---------------

x86_64-w64-mingw32-g++.exe  -O2  -std=c++11 -Wall -DBUILD_DLL  -std=c++11    -c C:\Users\Brandon\Desktop\OpenGL32\Implementations\Exports.cpp -o obj\Release\Implementations\Exports.o
x86_64-w64-mingw32-g++.exe  -O2  -std=c++11 -Wall -DBUILD_DLL  -std=c++11    -c C:\Users\Brandon\Desktop\OpenGL32\main.cpp -o obj\Release\main.o
x86_64-w64-mingw32-g++.exe -shared -Wl,--output-def=bin\Release\libOpenGL32.def -Wl,--out-implib=bin\Release\libOpenGL32.a -Wl,--dll  obj\Release\Implementations\Exports.o obj\Release\main.o   -o bin\Release\OpenGL32.dll -s -static -static-libgcc -static-libstdc++  -luser32 -lgdi32 -lopengl32 -lglu32 
Output size is 3.51 MB
Process terminated with status 0 (2 minutes, 39 seconds)
0 errors, 0 warnings (2 minutes, 39 seconds)

编辑:整个 DLL(仅包含请求的 1/500 func 指针):

Exports.hpp:

#ifndef EXPORTS_HPP_INCLUDED
#define EXPORTS_HPP_INCLUDED

#include <GL/gl.h>
#include <GL/glext.h>
#include "Platform.hpp"


extern Library* OriginalGL;

void (__stdcall *ptr_glAccum) (GLenum op, GLfloat value);

#endif // EXPORTS_HPP_INCLUDED

Exports.cpp:

#include "Exports.hpp"

Library* OriginalGL = nullptr;

bool __stdcall Initialized(void)
{
    char Root[MAX_PATH];
    #if defined _WIN32 || defined _WIN64
        GetSystemDirectoryA(Root, MAX_PATH);
    #ifdef _MSC_VER
        strcat_s(Root, "\\opengl32.dll");
    #else
        strcat(Root, "\\opengl32.dll");
    #endif
    #else
        strcat(Root, "/usr/lib");
        strcat(Root, "/libGL.so");
    #endif

    OriginalGL = new Library(Root);
    return  OriginalGL->FunctionAddress(ptr_glAccum, "glAccum"); //Just a thin class wrapper around GetProcAddress and LoadLibrary.
}

bool __stdcall DeInitialize(void)
{
    if (OriginalGL)
    {
        delete OriginalGL;
        OriginalGL = nullptr;
        return true;
    }
    return false;
}

extern "C" __stdcall void DetourHook_glAccum(GLenum op, GLfloat value)
{
    (*ptr_glAccum) (op, value);
}

Main.cpp:

#include <windows.h>

extern "C" bool __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            break;

        case DLL_PROCESS_DETACH:
            break;

        default:
            break;
    }
    return true;
}

最佳答案

使用 typedef 您的 header 会生成许多新类型,每个类型都是函数指针类型。类型仅对编译过程有用,不会在 DLL 本身中产生任何痕迹。 typedef 产生任何全局变量。

但是,如果没有 typedef,您的 header 会生成一系列全局变量,每个变量都是函数指针。全局变量确实在 DLL 中占据了一个条目,从而增加了文件生成时间及其最终大小。

关于c++ - 99% CPU,3.51MB 没有 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16883579/

相关文章:

c++ - 使用 cin 读取大量输入时程序卡住

C++删除节点链表导致程序崩溃

c++ 头文件,架构 x86_64 的 undefined symbol

c++ - 通过可变参数模板传递右值引用时出现编译器错误

c++ - 为什么存在 decltype 时范围解析会失败?

c++ - condition_variable::wait_until 意外地通过 g++ (9.4.0)

c++ - 访问属性树中的多值键

c++ - 根据模板类型在类中提供/启用方法

c++ - boost/thread.hpp 失败并显示 'clang++ -std=c++11 -stdlib=libc++'

c++ - static_assert 引用封闭模板类