Generating a C++ header containing OpenGL function definitions I'm currently working with Win32 and OpenGL a little bit and I needed a way to get the pointers of the OpenGL functions that aren't included in the Windows gl/GL.h header file. Rather than spending hours going through the OpenGL specification and manually typing out each OpenGL function definition in a header file, I created the following Python script which will automatically generate that header file for me.

INITIAL_HEADER_STRING_ONE = """
#ifndef OPENGL_FUNCTIONS_H_
#define OPENGL_FUNCTIONS_H_

#include &lt;Windows.h&gt;
#include &lt;gl/GL.h&gt;
#include &lt;gl/GLU.h&gt;
#include &lt;gl/glext.h&gt;
#include &lt;gl/wglext.h&gt;

"""

INITIAL_HEADER_STRING_TWO = """

void* GetGLProcPointer(const char* name)
{
    void* procPointer = (void*)wglGetProcAddress(name);
    if(
        procPointer == 0 ||
        procPointer == (void*)0x1 ||
        procPointer == (void*)0x2 ||
        procPointer == (void*)0x3 ||
        procPointer == (void*)-1
    )
    {
        HMODULE module = LoadLibraryA("opengl32.dll");
        procPointer = (void*)GetProcAddress(module, name);
    }
    return procPointer;
}

void GenerateGLProcPointers()
{
"""


def generate_header_string(function_names_file_name):
    header_string = INITIAL_HEADER_STRING_ONE

    with open(function_names_file_name, "r") as function_names:
        for line in function_names.readlines():
            stripped_line = line.rstrip("\n");
            stripped_line_upper = stripped_line.upper();
            header_string += "PFN{0}PROC {1};\n".format(stripped_line_upper, stripped_line)
        header_string += INITIAL_HEADER_STRING_TWO

    with open(function_names_file_name, "r") as function_names:
        for line in function_names.readlines():
            stripped_line = line.rstrip("\n");
            stripped_line_upper = stripped_line.upper();
            header_string += "\t{1} = (PFN{0}PROC)GetGLProcPointer(\"{1}\");\n".format(stripped_line_upper, stripped_line)
        header_string += "}\n"
        header_string += "\n#endif"

    return header_string


def generate_header_file(generated_header_string):
    with open("opengl_functions.h", "w+") as opengl_functions_header:
        opengl_functions_header.write(generated_header_string)


def main():
    generated_header_string = generate_header_string("OpenGL_Functions.txt");
    generate_header_file(generated_header_string)


if __name__ == "__main__":
    main()


The text file linked here contains a list of all OpenGL 4.5 function names.
In python it is not necessary to end a line with a ;.

Your function generate_header_file is probably better named write_to_header_file.

I would read the file only once and do everything in one pass in generate_header_string:

from itertools import chain

def generate_header_string(function_names_file_name):
    header_string1 = [INITIAL_HEADER_STRING_ONE]
    header_string2 = []

    def_string1 = "PFN{}PROC {};"
    def_string2 = "\t{1} = (PFN{0}PROC)GetGLProcPointer(\"{1}\");"

    with open(function_names_file_name, "r") as function_names:
        for line in function_names.readlines():
            line = line.rstrip("\n")
            line_upper = line.upper()
            header_string1.append(def_string1.format(line_upper, line))
            header_string2.append(def_string2.format(line_upper, line))
        header_string1.append(INITIAL_HEADER_STRING_TWO)
        header_string2.append("}")
        header_string2.append("\n#endif")

    return "\n".join(chain(header_string1, header_string2))


Here I changed the accumulation variable to lists which I join in the end. This gives some memory boost, because string addition is very costly, especially if the strings become bigger (and your INITIAL_HEADER_STRING_X is already quite big). The lists also get rid of having to manually write \n in the strings, because we can just use it as joining variable at the end.

You will want to give the two variables better names (like header_declaration and header_initialization or whatever fits their use case; I don't know any OpenGL syntax, so I'm not exactly sure).

I also declared the strings you format once before the loop to avoid having to redeclare them every iteration. They could also do with better names, probably something similar to the header lists.