# Shader Compile Error: ERROR: 0:1: '' : syntax error #version

**URL:** <https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427>\
**Category:** OpenGL: Basic Coding\
**Created:** [July 24, 2013, 5:30pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427 "2013-07-24T17:30:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blakeasd](https://community.khronos.org/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Blakeasd](https://community.khronos.org/u/Blakeasd)\
**Post date:** [July 24, 2013, 5:30pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/1 "2013-07-24T17:30:40Z")

</div>

Hello,

I am trying to compile a very simple vertex shader and I got this error, however I don’t see anything wrong in my vertex shader. This is the error that I get:

```auto
ERROR: 0:1: '' : syntax error #version

```

This is what my shader looks like:

```auto

#version 150

in vec2 position;

void main()
{
    gl_Position = vec4( position, 0.0, 1.0 );
}

```

Am I missing something here? As far as I can tell there isn’t anything wrong with my shader.

If it matters: I am using the OpenGL 3.2 Core Profile on OS X 10.8.4 using Xcode 4.6.3 as my IDE with glfw as my windowing system for this program.

Thanks!

---

<div class="post-metadata">

**Author:** ![GClements](https://community.khronos.org/letter_avatar_proxy/v4/letter/g/e0b2c6/32.png) [@GClements](https://community.khronos.org/u/GClements)\
**Post date:** [July 24, 2013, 6:07pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/2 "2013-07-24T18:07:09Z")

</div>

> [@Blakeasd;1253066](#):
>
> I am trying to compile a very simple vertex shader and I got this error

> [@Blakeasd;1253066](#):
>
> This is what my shader looks like:

You don’t show the code you’re using to compile it.

If you’re trying to compile it using the IDE, you need to read a tutorial on how to use shaders.

---

<div class="post-metadata">

**Author:** ![Blakeasd](https://community.khronos.org/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Blakeasd](https://community.khronos.org/u/Blakeasd)\
**Post date:** [July 24, 2013, 8:31pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/3 "2013-07-24T20:31:05Z")

</div>

Thanks for the response. Here is my code to load and compile the shaders:

```auto

 std::ifstream vertexShaderFile;
    std::string tempString;
    std::string vertexShaderString;
    
    vertexShaderFile.open("VertexShader.glsl");
    
    if(vertexShaderFile.is_open()){
    while (getline(vertexShaderFile, tempString)) {
        vertexShaderString = vertexShaderString + tempString;
    }
    }else{
    
        std::cout << "The vertex shader file is closed" << std::endl;
        exit(1);
    }
    
    std::ifstream fragmentShaderFile;
    std::string tempString2;
    std::string fragmentShaderString;
    
    fragmentShaderFile.open("FragmentShader.glsl");
    
    if (fragmentShaderFile.is_open()) {
        while (getline(fragmentShaderFile, tempString2)) {
            fragmentShaderString = fragmentShaderString + tempString2;
        }
    }else{
        std::cout << "The fragment shader file is closed" << std::endl;
        exit(1);
    }
    
    
    const char* vertexShaderCString = vertexShaderString.c_str();
    const char* fragmentShaderCString = fragmentShaderString.c_str();
    
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderCString, NULL);
    glCompileShader(vertexShader);
    
    GLint status;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
    
    if(status == GL_FALSE){
    
        std::cout << "The vertex shader was compiled with an error(s)." << std::endl;
        char buffer[512];
        glGetShaderInfoLog(vertexShader, 512, NULL , buffer);
        std::cout << buffer << std::endl;
    }
    

```

Hopefully this will provide more clues.

Thanks!

---

<div class="post-metadata">

**Author:** ![Alfonse\_Reinheart](https://community.khronos.org/user_avatar/community.khronos.org/alfonse_reinheart/32/4357_2.png) [@Alfonse\_Reinheart](https://community.khronos.org/u/Alfonse_Reinheart)\
**Post date:** [July 24, 2013, 10:47pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/4 "2013-07-24T22:47:15Z")

</div>

It really amazes me just how few C++ programmers know something as simple as [how to read a text file with iostreams](http://stackoverflow.com/a/195350/734069). It’s really not that difficult, and the fact that you were reading it line-by-line should have been a tip-off that you were doing it wrong.

Really, have you ever seen a programming language where line-by-line was the way you had to read a text file?

In any case, your code is wrong because std::getline doesn’t actually put the endline character in the string. So you’re sticking every statement on a single line, which never works.

---

<div class="post-metadata">

**Author:** ![Blakeasd](https://community.khronos.org/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Blakeasd](https://community.khronos.org/u/Blakeasd)\
**Post date:** [July 25, 2013, 9:23am UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/5 "2013-07-25T09:23:47Z")

</div>

Thanks for the reply, but now I have a different problem. Sometimes the shader will compile, but other times it won’t compile and I’ll get seemingly random shader compilation errors.

Here my new code that loads and compiles the vertex shader:

```auto
     std::ifstream vertexShaderFile("VertexShader.glsl", std::ios::in | std::ios::binary | std::ios::ate);
   
    if (vertexShaderFile.is_open()) {
        long size;
        size = vertexShaderFile.tellg();
        char *vertexShaderCString = new char [size];
        vertexShaderFile.seekg(0, std::ios::beg);
        vertexShaderFile.read(vertexShaderCString, size);
        
        const char *vertexShaderCStringConst = vertexShaderCString;
                
        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &vertexShaderCStringConst, NULL);
        glCompileShader(vertexShader);
        
        GLint status;
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
        
        if (status == GL_TRUE) {
            std::cout << "Vertex Shader compiling went OK" << std::endl;
        }
        
        char buffer[512];
        glGetShaderInfoLog(vertexShader, 512, NULL,buffer);
        printf("%s",buffer);
        
    }

```

The error message changes each time, the last one I got was this:

```auto
ERROR 0.8 'P' : error syntax error

```

Any help is appreciated!  
Thanks!

---

<div class="post-metadata">

**Author:** ![Alfonse\_Reinheart](https://community.khronos.org/user_avatar/community.khronos.org/alfonse_reinheart/32/4357_2.png) [@Alfonse\_Reinheart](https://community.khronos.org/u/Alfonse_Reinheart)\
**Post date:** [July 25, 2013, 10:41am UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/6 "2013-07-25T10:41:01Z")

</div>

That’s my fault for linking you to buggy code; I should have looked at the first example more closely. I have since fixed it over on Stack Overflow.

Though the memory leak you wrote in when you copy-and-pasted the buggy code is your fault. I was kinda hoping that you’d use one of the other alternatives, rather than the one that directly allocates memory for no adequately explained reason.

---

<div class="post-metadata">

**Author:** ![Blakeasd](https://community.khronos.org/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Blakeasd](https://community.khronos.org/u/Blakeasd)\
**Post date:** [July 25, 2013, 11:18am UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/7 "2013-07-25T11:18:27Z")

</div>

There’s still something wrong with my code, but I don’t see anything wrong. I am compiling a fragment shader underneath the vertex shader. Sometimes the vertex shader doesn’t compile, sometimes the fragment shader does compile, and sometimes neither compile – and then sometimes they both compile. This is my code:

```auto

 std::ifstream file ("VertexShader.glsl", std::ios::in|std::ios::binary|std::ios::ate);
    if (file.is_open())
    {
        long size;
        file.seekg(0, std::ios::end);
        size = file.tellg();
        char *contents = new char [size];
       const char *contentsConst = new char [size];
        file.seekg (0, std::ios::beg);
        file.read (contents, size);
        file.close();

        contentsConst = contents;
        
        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &contentsConst, NULL);
        glCompileShader(vertexShader);
        
        GLint status;
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
        
        if (status == GL_TRUE) {
            std::cout << "The vertex shader has been compiled" << std::endl;
        }else{
            std::cout << "Something went wrong" << std::endl;
        
        }
        
        char buffer[512];
        glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
        
        
        delete [] contents;
    }
   
    std::ifstream fileF ("FragmentShader.glsl", std::ios::in|std::ios::binary|std::ios::ate);
    if (fileF.is_open())
    {
        long size;
        fileF.seekg(0, std::ios::end);
        size = fileF.tellg();
        char *contents = new char [size];
        const char *contentsConst = new char [size];
        fileF.seekg (0, std::ios::beg);
        fileF.read (contents, size);
        fileF.close();
        
        contentsConst = contents;
        
        GLuint fragmentShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(fragmentShader, 1, &contentsConst, NULL);
        glCompileShader(fragmentShader);
        
        GLint status;
        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
        
        if (status == GL_TRUE) {
            std::cout << "The fragment shader has been compiled" << std::endl;
        }else{
            std::cout << "Something went wrong" << std::endl;
            
        }
        
        char buffer[512];
        glGetShaderInfoLog(fragmentShader, 512, NULL, buffer);
        
        
        delete [] contents;
    }

```

I don’t see any problems with it ☹ I apologize if my code is weird looking as my “main” language is Objective-C

Thanks!

---

<div class="post-metadata">

**Author:** ![Alfonse\_Reinheart](https://community.khronos.org/user_avatar/community.khronos.org/alfonse_reinheart/32/4357_2.png) [@Alfonse\_Reinheart](https://community.khronos.org/u/Alfonse_Reinheart)\
**Post date:** [July 25, 2013, 12:08pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/8 "2013-07-25T12:08:18Z")

</div>

Have you attempted to, you know, debug the code at all? Did you look at the string you got to see if it was actually your file? And if Objective-C is your “main language”, why are you using iostreams for file loading at all?

And you still didn’t fix the memory leak.

---

<div class="post-metadata">

**Author:** ![GClements](https://community.khronos.org/letter_avatar_proxy/v4/letter/g/e0b2c6/32.png) [@GClements](https://community.khronos.org/u/GClements)\
**Post date:** [July 25, 2013, 12:30pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/9 "2013-07-25T12:30:03Z")

</div>

> [@Alfonse Reinheart;1253075](#):
>
> It really amazes me just how few C++ programmers know something as simple as [how to read a text file with iostreams](http://stackoverflow.com/a/195350/734069). It’s really not that difficult, and the fact that you were reading it line-by-line should have been a tip-off that you were doing it wrong.

While probably less efficient than the linked version, using a stringbuf is simpler and more robust (e.g. it works with streams which don’t support seeking, such as pipes and sockets):

```auto

std::string read(std::istream& s)
{
    std::stringbuf buf(std::ios::out);
    s >> &buf;
    return buf.str();
}

```

---

<div class="post-metadata">

**Author:** ![GClements](https://community.khronos.org/letter_avatar_proxy/v4/letter/g/e0b2c6/32.png) [@GClements](https://community.khronos.org/u/GClements)\
**Post date:** [July 25, 2013, 12:35pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/10 "2013-07-25T12:35:38Z")

</div>

> [@Blakeasd;1253103](#):
>
> There’s still something wrong with my code, but I don’t see anything wrong.

You’re passing NULL for the last parameter (length) of glShaderSource(), which means that the strings must be null terminated, but I don’t see a null terminator being added anywhere.

As you know the length of the string, I’d suggest copying it to a GLint and passing a pointer to that as the last argument.

---

<div class="post-metadata">

**Author:** ![Blakeasd](https://community.khronos.org/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Blakeasd](https://community.khronos.org/u/Blakeasd)\
**Post date:** [July 25, 2013, 2:01pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/11 "2013-07-25T14:01:17Z")

</div>

I’ve checked to see if my C string is my file… and it is. What I mean as Objective-C being my language is that I’ve been programming with Objective-C for years, while I’ve only been using C++ recently. I’m using iostream to read the file because Objective-C doesn’t play nice with glfw (my window management system.) I’m not currently concerned with performance, so I’ve left the leak alone for now.

Passing in the size doesn’t seem to fix the problem. I did this to get the size of the C String for the final parameter:

GLint sizeOfContents = sizeof(contentsConst);

I passed this in as a pointer for the final parameter. (&sizeOfContents)

So I’m still not sure why this doesn’t work. What’s odd to me is that sometimes the shaders will compile and other times the shaders won’t compile.

I’ll try GCements method of loading the shaders and see if that makes a difference.

Thanks!

---

<div class="post-metadata">

**Author:** ![GClements](https://community.khronos.org/letter_avatar_proxy/v4/letter/g/e0b2c6/32.png) [@GClements](https://community.khronos.org/u/GClements)\
**Post date:** [July 25, 2013, 4:43pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/12 "2013-07-25T16:43:08Z")

</div>

[QUOTE=Blakeasd;1253122]Passing in the size doesn’t seem to fix the problem. I did this to get the size of the C String for the final parameter:

GLint sizeOfContents = sizeof(contentsConst);  
[/QUOTE]  
That’s wrong. sizeof(contentsConst) will be the size of a pointer (typically 4 or 8), not the size of the array which it points to. The size of the string can be obtained from the variable “size” which you used to allocate the array.

Alternatively, you could null-terminate the string by allocating size+1 bytes and setting contents[size]=’\0’.

If you do neither, glShaderSource() won’t know where the string ends, and it will try to parse whatever follows it in memory.

---

<div class="post-metadata">

**Author:** ![Blakeasd](https://community.khronos.org/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Blakeasd](https://community.khronos.org/u/Blakeasd)\
**Post date:** [July 25, 2013, 6:35pm UTC](https://community.khronos.org/t/shader-compile-error-error-0-syntax-error-version/70427/13 "2013-07-25T18:35:11Z")

</div>

It worked! Thanks so much for the help GClements and Alfonse! I obtained the length of the C string (not the pointer this time) like this:  
GLint arraySize = (GLint)size;

and passed it in as a reference like so: &arraySize

Thanks Again!
