<p><s>Yes, </s> <a href="https://opengl.gpuinfo.org/listreports.php?extension=GL_ARB_shading_language_include" rel="nofollow noreferrer">With a certain OpenGL extension</a>, that is possible (thanks to NicolBolas for pointing that out). <s>It is pretty much the same as in C++. Let's assume you have a file util.glsl, vertex.glsl and fragment.glsl, all in the same folder.</s> <span class="math-container">$^1$</span></p>
<pre><code>//in util.glsl
float add(float a, float b)
{
    return a + b;
}
</code></pre>
<p>Then you can call that function, as if it were in your shader file</p>
<pre><code>//in either vertex.glsl or fragment.glsl
#include &quot;util.glsl&quot;

void main()
{
    float result = add(1, 2);
    ...
}
</code></pre>
<p>Your result would then be 3. Of course, if you have the file in a different folder, you need to adjust the path, but again, that is pretty much the same as in C++.</p>
<p>The only caveat is with debugging. Without having tested it right now, as far as I remember compiler errors will not display your util.glsl as the source of your error. Rather it will put the contents of util.glsl at the position, where your include call is and then say you have an error in that line in vertex.glsl / fragment.glsl, so this might be a bit confusing</p>
<p><span class="math-container">$^1$</span>Actually, this is incorrect (by default at least). In a project we used it like that, but since I did not do that setup myself, I assume it was either setup to work like that by a collegue or by OpenSceneGraph, which we used. In any case, I cannot (yet) answer this myself. I want to add this part, as soon as I understand it myself. Until then, I will simply link to a similar question with answer from NicolBolas, which hints at what you have to do: <a href="https://stackoverflow.com/questions/10754437/how-to-using-the-include-in-glsl-support-arb-shading-language-include">https://stackoverflow.com/questions/10754437/how-to-using-the-include-in-glsl-support-arb-shading-language-include</a></p>

CC BY-SA 4.0
2021-02-23T10:08:54.227Z
So in the opengl side of creating shaders there are no changes?
Sync it
CC BY-SA 4.0
2021-02-23T10:11:37.393Z
No, unless you want to create a shader out of modular functions on the fly. You can do that as well, but then you don't need the include in your shader file anymore. You could for example create a shader out of first the util.glsl and then the fragment.glsl, OpenGL "merges" it into one source code and compiles that. It is not all that different, you get more control about which code to use but you also have more shaders then, which (probably) means you have to switch shaders more often in your render pipeline.
Tare
CC BY-SA 4.0
2021-02-23T11:32:33.310Z
But when [creating](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glCreateShader.xhtml) this utility shader what type should it be?. A vertex shader, a fragment shader? What constant do I specify?
Sync it
CC BY-SA 4.0
2021-02-24T11:24:01.160Z
@NicolBolas thanks for the hint. I was not aware of that. I'll adjust my answer.
Tare
CC BY-SA 4.0
2021-02-23T09:14:31.653Z
2021-02-24T12:06:58.557Z
2021-02-24T12:06:58.557Z
Tare
<p>We can create header files in C/C++ which we can include in our project using #include directive but when I use this keyword in my shader it gives the error  reserved keyword. If let's say I create an simple text file for adding two numbers and I want to include this file &amp; code in my vertex/fragment shader is it possible?</p>

<opengl><glsl>
Opengl : GLSL utility files
Sync it
# Opengl : GLSL utility files

(Asked by: username_0 on 2021-02-23)

We can create header files in C/C++ which we can include in our project using #include directive but when I use this keyword in my shader it gives the error reserved keyword. If let's say I create an simple text file for adding two numbers and I want to include this file & code in my vertex/fragment shader is it possible?

**Tags:** opengl, glsl


---

## 1 answers

### Answer by username_1 on 2021-02-23. Score: 1 | Accepted answer

~~Yes,~~  [With a certain OpenGL extension](https://opengl.gpuinfo.org/listreports.php?extension=GL_ARB_shading_language_include), that is possible (thanks to NicolBolas for pointing that out). ~~It is pretty much the same as in C++. Let's assume you have a file util.glsl, vertex.glsl and fragment.glsl, all in the same folder.~~ $^1$

```
//in util.glsl
float add(float a, float b)
{
    return a + b;
}

```

Then you can call that function, as if it were in your shader file

```
//in either vertex.glsl or fragment.glsl
#include "util.glsl"

void main()
{
    float result = add(1, 2);
    ...
}

```

Your result would then be 3. Of course, if you have the file in a different folder, you need to adjust the path, but again, that is pretty much the same as in C++.

The only caveat is with debugging. Without having tested it right now, as far as I remember compiler errors will not display your util.glsl as the source of your error. Rather it will put the contents of util.glsl at the position, where your include call is and then say you have an error in that line in vertex.glsl / fragment.glsl, so this might be a bit confusing

$^1$Actually, this is incorrect (by default at least). In a project we used it like that, but since I did not do that setup myself, I assume it was either setup to work like that by a collegue or by OpenSceneGraph, which we used. In any case, I cannot (yet) answer this myself. I want to add this part, as soon as I understand it myself. Until then, I will simply link to a similar question with answer from NicolBolas, which hints at what you have to do: <https://stackoverflow.com/questions/10754437/how-to-using-the-include-in-glsl-support-arb-shading-language-include>

> **Comments:**
> **username_0**: So in the opengl side of creating shaders there are no changes?  
> **username_1**: No, unless you want to create a shader out of modular functions on the fly. You can do that as well, but then you don't need the include in your shader file anymore. You could for example create a shader out of first the util.glsl and then the fragment.glsl, OpenGL "merges" it into one source code and compiles that. It is not all that different, you get more control about which code to use but you also have more shaders then, which (probably) means you have to switch shaders more often in your render pipeline.  
> **username_0**: But when [creating](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glCreateShader.xhtml) this utility shader what type should it be?. A vertex shader, a fragment shader? What constant do I specify?  
> **username_1**: @NicolBolas thanks for the hint. I was not aware of that. I'll adjust my answer.  

---

