I am an amateur at c, c++ programming.
I guess am trying to stay with C++ because all of the other parts of this project code is C++.
The code below compiles and runs as a c program.
When I copied the code into a Code Blocks C++ console project I get “undefined reference to `glutInit’” and all other functions in main.
obj/Debug/main.o||In function main':| /home/gary/Documents/screen1/main.cpp|138|undefined reference to
glutInit’|
/home/gary/Documents/screen1/main.cpp|139|undefined reference to glutInitDisplayMode'| /home/gary/Documents/screen1/main.cpp|140|undefined reference to
glutInitWindowSize’|
/home/gary/Documents/screen1/main.cpp|141|undefined reference to glutInitWindowPosition'| /home/gary/Documents/screen1/main.cpp|142|undefined reference to
glutCreateWindow’|
/home/gary/Documents/screen1/main.cpp|144|undefined reference to glutDisplayFunc'| /home/gary/Documents/screen1/main.cpp|145|undefined reference to
glutMainLoop’|
||=== Build finished: 7 errors, 0 warnings ===|
both files are below.
I have add header files to the C++ file trying to resolve the problem.
1.) How do I resolve error?
2.) Why does it work in C and not C++?
----------------------C file---------------------------
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glx.h> /* this includes the necessary X headers /
#include <GL/gl.h>
#include <X11/X.h> / X11 constant (e.g. TrueColor) */
#include <X11/keysym.h>
#include <math.h>
using namespace std;
const float DEG2RAD = 3.14159/180;
void disp_rect(float x1, float y1, float x2, float y2)
{
glColor3f (1.0, 0.0, 0.0);
/glRectf(50.0, 50.0, 800.0, 600.0);/
glRectf(x1,y1,x2,y2);
return;
}
void disp_line(float x1, float y1, float x2, float y2)
{
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1,y1,0.0);
glVertex3f(x2,y2,0.0);
glEnd();
return;
}
void disp_point(float x1, float y1)
{
glColor3f (0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(x1,y1,0.0);
glEnd();
return;
}
void G02drawCircle(float radius)
{
glBegin(GL_LINES);
// glBegin(GL_LINE_LOOP);
int i;
for (i=0; i < 360; i++)
{
float degInRad = i*DEG2RAD;
glVertex2f((cos(degInRad)*radius)+200.00,(sin(degInRad)*radius)+200);
}
glEnd();
}
void G03_arc (float x1, float y1, float x2, float y2, float I, float J)
{
float x_offset;
float y_offset;
float radius;
x_offset = x1 + I;
y_offset = y1 + J;
return;
}
void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2f(820.0,400.0);
glEnd();
disp_rect(300.0,50.0,800.0,600.0);
disp_line(20.0,30.0,70.0,400.0);
int i;
for (i=0;i<300;i=i+1)
{
disp_point(100.0 + i,200.0 +i);
}
G02drawCircle(100);
G03_arc (104.8569, 2.7599, 104.8372, 2.7796, -.0197, 0.0);
/* don’t wait! float x1, float y1, float x2, float y2, float I, float
-
start processing buffered OpenGL routines
*/glFlush ();
}
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,1000, 500);
glOrtho(00.0,1000.0, 00.0, 800.0, -1.0, 1.0);
}
/*
- Declare initial window size, position, and display mode
- (single buffer and RGBA). Open window with “hello”
- in its title bar. Call initialization routines.
- Register callback function to display graphics.
- Enter main loop and process events.
/
int main(int argc, char* argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (1000, 800);
glutInitWindowPosition (100, 100);
glutCreateWindow (“hello”);
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}
------------------------end C file that works-----------------------
------------------------c++ file that will not run------------
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glx.h> /* this includes the necessary X headers /
#include <GL/gl.h>
#include <stdlib.h>
#include <X11/X.h> / X11 constant (e.g. TrueColor) */
#include <X11/keysym.h>
#include <math.h>
using namespace std;
const float DEG2RAD = 3.14159/180;
void glutInit(int *argcp, char **argv);
void disp_rect(float x1, float y1, float x2, float y2)
{
glColor3f (1.0, 0.0, 0.0);
/glRectf(50.0, 50.0, 800.0, 600.0);/
glRectf(x1,y1,x2,y2);
return;
}
void disp_line(float x1, float y1, float x2, float y2)
{
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1,y1,0.0);
glVertex3f(x2,y2,0.0);
glEnd();
return;
}
void disp_point(float x1, float y1)
{
glColor3f (0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(x1,y1,0.0);
glEnd();
return;
}
void G02drawCircle(float radius)
{
glBegin(GL_LINES);
// glBegin(GL_LINE_LOOP);
int i;
for (i=0; i < 360; i++)
{
float degInRad = i*DEG2RAD;
glVertex2f((cos(degInRad)*radius)+200.00,(sin(degInRad)*radius)+200);
}
glEnd();
}
void G03_arc (float x1, float y1, float x2, float y2, float I, float J)
{
float x_offset;
float y_offset;
float y_offseta;
float radius;
x_offset = x1 + I;
y_offset = y1 + J;
y_offseta = y2 +x2 + J;
radius = 1;
if (I == 0)
{
cout << radius ;
}
cout << radius ;
return;
}
void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2f(820.0,400.0);
glEnd();
disp_rect(300.0,50.0,800.0,600.0);
disp_line(20.0,30.0,70.0,400.0);
int i;
for (i=0;i<300;i=i+1)
{
disp_point(100.0 + i,200.0 +i);
}
G02drawCircle(100);
G03_arc (104.8569, 2.7599, 104.8372, 2.7796, -.0197, 0.0);
/* don’t wait! float x1, float y1, float x2, float y2, float I, float
-
start processing buffered OpenGL routines
*/glFlush ();
}
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,1000, 500);
glOrtho(00.0,1000.0, 00.0, 800.0, -1.0, 1.0);
}
/*
- Declare initial window size, position, and display mode
- (single buffer and RGBA). Open window with “hello”
- in its title bar. Call initialization routines.
- Register callback function to display graphics.
- Enter main loop and process events.
/
int main(int argc, char* argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (1000, 800);
glutInitWindowPosition (100, 100);
glutCreateWindow (“hello”);
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}