Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

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

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2011
    Posts
    26

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

    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:

    Code :
    ERROR: 0:1: '' : syntax error #version

    This is what my shader looks like:

    Code :
    #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!

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by Blakeasd View Post
    I am trying to compile a very simple vertex shader and I got this error
    Quote Originally Posted by Blakeasd View Post
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2011
    Posts
    26
    Thanks for the response. Here is my code to load and compile the shaders:

    Code :
     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!

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    It really amazes me just how few C++ programmers know something as simple as how to read a text file with iostreams. 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.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2011
    Posts
    26
    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:

    Code :
         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:

    Code :
    ERROR 0.8 'P' : error syntax error

    Any help is appreciated!
    Thanks!

  6. #6
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    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.

  7. #7
    Junior Member Newbie
    Join Date
    Sep 2011
    Posts
    26
    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:

    Code :
     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!

  8. #8
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    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.

  9. #9
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by Alfonse Reinheart View Post
    It really amazes me just how few C++ programmers know something as simple as how to read a text file with iostreams. 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):
    Code :
    std::string read(std::istream& s)
    {
        std::stringbuf buf(std::ios::out);
        s >> &buf;
        return buf.str();
    }

  10. #10
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by Blakeasd View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •