{"id":1038,"date":"2022-08-30T15:11:21","date_gmt":"2022-08-30T15:11:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/linker-error-undefined-reference-to-symbol-glortho-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:21","modified_gmt":"2022-08-30T15:11:21","slug":"linker-error-undefined-reference-to-symbol-glortho-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/linker-error-undefined-reference-to-symbol-glortho-collection-of-common-programming-errors\/","title":{"rendered":"Linker error : undefined reference to symbol &#39;glOrtho&#39;-Collection of common programming errors"},"content":{"rendered":"<p>I installed OpenGL packages in Ubuntu 13.04 ( <code>*mesa-common-dev freeglut3-dev*<\/code> ) and tried to run a sample program.<\/p>\n<pre><code>#include \"GL\/freeglut.h\"\n#include \"GL\/gl.h\"\n\n\/* display function - code from:\n     http:\/\/fly.cc.fer.hr\/~unreal\/theredbook\/chapter01.html\nThis is the actual usage of the OpenGL library. \nThe following code is the same for any platform *\/\nvoid renderFunction()\n{\n    glClearColor(0.0, 0.0, 0.0, 0.0);\n    glClear(GL_COLOR_BUFFER_BIT);\n    glColor3f(1.0, 1.0, 1.0);\n    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);\n    glBegin(GL_POLYGON);\n        glVertex2f(-0.5, -0.5);\n        glVertex2f(-0.5, 0.5);\n        glVertex2f(0.5, 0.5);\n        glVertex2f(0.5, -0.5);\n    glEnd();\n    glFlush();\n}\n\n\/* Main method - main entry point of application\nthe freeglut library does the window creation work for us, \nregardless of the platform. *\/\nint main(int argc, char** argv)\n{\n    glutInit(&amp;argc, argv);\n    glutInitDisplayMode(GLUT_SINGLE);\n    glutInitWindowSize(500,500);\n    glutInitWindowPosition(100,100);\n    glutCreateWindow(\"OpenGL - First window demo\");\n    glutDisplayFunc(renderFunction);\n    glutMainLoop();    \n    return 0;\n}\n<\/code><\/pre>\n<p>However, I encountered this error and don&#8217;t know what to make of it.<\/p>\n<pre><code>ved@vedvals:~\/Desktop\/p1$ g++ p1.cpp -lglut\n\/usr\/bin\/ld: \/tmp\/ccgGdeR2.o: undefined reference to symbol 'glOrtho'\n\/usr\/bin\/ld: note: 'glOrtho' is defined in DSO \/usr\/lib\/x86_64-linux-gnu\/mesa\/libGL.so.1 so try adding it to the linker command line\n\/usr\/lib\/x86_64-linux-gnu\/mesa\/libGL.so.1: could not read symbols: Invalid operation\ncollect2: error: ld returned 1 exit status\n<\/code><\/pre>\n<p>I had a look at<br \/>\nOpenGL hello.c fails to build using CMake<br \/>\nas the error is similar but I am not using CMake<\/p>\n<p>IS my code wrong or do I need to include\/change\/tweak some settings?<\/p>\n<p>I referred this site for installation and code :<\/p>\n<p>Setting up an OpenGL development environment in Ubuntu Linux<\/p>\n<ol>\n<li>\n<p>You need to link the OpenGL library:<\/p>\n<pre><code>g++ p1.cpp -lglut -lGL\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You have not linked the OpenGL lib, where <code>glOrtho()<\/code> is defined. To make it work, compile\/link with <code>g++ p1.cpp -lglut -lGL<\/code>. Mind the order of linking libs, as it is important in <code>ld<\/code> (linker used by g++). The GLUT library depends on OpenGL, and so <code>-lGL<\/code> HAS TO go after <code>-glut<\/code>. This is because <code>ld<\/code> only makes one cycle through the libraries, and thus if you linked <code>-lGL -lglut<\/code>, references from lglut to lGL will not be defined, thus making a linking error. Sorry for such a long answer, but I hope you&#8217;ll learn something from it.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:17:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I installed OpenGL packages in Ubuntu 13.04 ( *mesa-common-dev freeglut3-dev* ) and tried to run a sample program. #include &#8220;GL\/freeglut.h&#8221; #include &#8220;GL\/gl.h&#8221; \/* display function &#8211; code from: http:\/\/fly.cc.fer.hr\/~unreal\/theredbook\/chapter01.html This is the actual usage of the OpenGL library. The following code is the same for any platform *\/ void renderFunction() { glClearColor(0.0, 0.0, 0.0, 0.0); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1038","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1038"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1038\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}