One annoyance that I have with GLSL shaders is that the vertex and fragment shader files must be placed into the directory with the binaries (or some other path accessible by the program). There are alternatives that involve using a resource manager but for smaller programs this seems like overkill.

One solution is to make a string out of the shader and include it with the code. The major problem with this is that editing the shader becomes a bit more tedious. One solution which I will discuss here involves converting the shader at compile time into a header and including it into the binary/library. I am using CMake for my build system which makes it trivial to do this.

### References

Cmake Embed Resources

Using these two posts you should be able to figure everything out but I wanted to describe in detail the full process.

## Cmake

Cmake takes care of the heavy lifting and determines whether not not the header file needs to be generated. In my build system I have the shaders stored in a “resource” folder in my ${CMAKE_SOURCE_DIR} and the header files will be generated in the ${CMAKE_BINARY_DIR}.

### embedfile.cpp

This awesome little piece of code was written by the user ScottManDeath from this post. I made a small modification and got rid of the debug wait for user input and made the char array a const.

The program takes three arguments:

### CmakeLists.txt

Here is the cmake code that uses embedfile.cpp to generate an executable

This code uses the executable to compile the shader.vert file into vertex_shader.h where the code is stored in a string called vertex_shader .

Add the vertex_shader.h to your list of sources/headers, that way Cmake knows to execute the custom command before trying to compile the code.

The same can be done for the fragment shader. Then simply include the generated headers into your source code and treat the variable like a const char *.

### Notes:

Make sure that the shader files are using unix line endings otherwise the embedfile code will not work. Also you will need to add \${CMAKE_BINARY_DIR} to your INCLUDE_DIRECTORIES so that it finds the generated include files.

Input:

Output: