top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How do you link a C++ program to C functions?

+1 vote
113 views
How do you link a C++ program to C functions?
posted Jan 6, 2015 by Emran

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

By using the extern "C" linkage specification around the C function declarations. Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions.

answer Jan 8, 2015 by Mohammed Hussain
Similar Questions
+1 vote

I am trying to execute the following Graphics program using C++, but facing linking error (undefined reference). Please help?

#include "GL/glut.h"
#include"stdlib.h"
#include"windows.h"
#define  adjust_r  3

static int year = 0, day = 0, moon = 0;
static int animation = 0;

GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 };
GLfloat red[] = { 1.0, 0.4, 0.3, 1.0 };
GLfloat blue[] = { 0.2, 0.4, 1.0, 1.0 };
GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
  glLightfv(GL_LIGHT1, GL_POSITION, light1pos);

  glPushMatrix();
  glRotated((GLdouble)year/adjust_r, 0.0, 1.0, 0.0);
  glTranslatef (2.0, 0.0, 0.0);
  glRotated((GLdouble)day/adjust_r, 0.0, 1.0, 0.0);
  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
  glutWireSphere(0.2, 10, 8);

  glRotated((GLfloat)moon/adjust_r, 0.0, 1.0, 0.0);
  glTranslatef (0.4, 0.0, 0.0);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, white);
  glutWireSphere(0.08, 8, 6);
  glPopMatrix();

  glPushMatrix();
  glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow);
  glutSolidSphere(0.7, 20, 16);
  glPopMatrix();

  glutSwapBuffers();

  if(animation == 1){
    if (glutLayerGet(GLUT_NORMAL_DAMAGED) == 0) {
      day = (day + 5) % (360*adjust_r);
      moon = (moon + 10) % (360*adjust_r);
      if (year++ >= (360*adjust_r)) {
    year = 0;
    animation = 0;
    glutIdleFunc(0);
      }
    }
  }
}

void idle(void)
{
  glutPostRedisplay();
}

void resize(int w, int h)
{
  glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(2.5, 0.0, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard(unsigned char key, int x, int y)
{
  switch (key) {
  case '\33':
  case 'q':
  case 'Q':
    exit(0);
  case 'd':
    day = (day + 10) % (360*adjust_r);
    glutPostRedisplay();
    break;
  case 'D':
    day = (day - 10) % (360*adjust_r);
    glutPostRedisplay();
    break;
  case 'y':
    year = (year + 5) % (360*adjust_r);
    glutPostRedisplay();
    break;
  case 'Y':
    year = (year - 5) % (360*adjust_r);
    glutPostRedisplay();
    break;
  case 'm':
    moon = (moon + 10) % (360*adjust_r);
    glutPostRedisplay();
    break;
  case 'M':
    moon = (moon - 10) % (360*adjust_r);
    glutPostRedisplay();
    break;
  default :
    break;
  }
}

void mouse(int button, int state, int x, int y)
{
  switch (button) {
  case GLUT_LEFT_BUTTON:
    if (state == GLUT_UP){
      animation = 1;
      glutIdleFunc(idle);
    }
    break;
  case GLUT_MIDDLE_BUTTON:
    break;
  case GLUT_RIGHT_BUTTON:
    if (state == GLUT_UP){
      animation = 0;
      glutIdleFunc(NULL);
    }
    break;
  default:
    break;
  }
}

void init(void)
{
  glClearColor (0.0, 0.0, 0.0, 0.0);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
  glCullFace(GL_BACK);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
  glEnable(GL_LIGHT1);
  glLightfv(GL_LIGHT1, GL_DIFFUSE, blue);
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowPosition(200, 50);
  glutInitWindowSize(500, 300);
  glutCreateWindow(argv[0]);

  init();

  glutDisplayFunc(display);
  glutReshapeFunc(resize);

  glutKeyboardFunc(keyboard);
  glutMouseFunc(mouse);

  glutMainLoop();
  return 0;
}

showing error :

mingw32-g++.exe  -o bin\Release\OpenGLFirstProgram.exe obj\Release\main.o  -s  
"C:\Program Files (x86)\CodeBlocks\MinGW\lib\libopengl32.a" 
"C:\Program Files (x86)\CodeBlocks\MinGW\lib\libglu32.a" "C:\Program Files (x86)\CodeBlocks\MinGW\lib\libglut32.a"
obj\Release\main.o:main.cpp:(.text.startup+0x2e): undefined reference to `_imp____glutInitWithExit@12'
obj\Release\main.o:main.cpp:(.text.startup+0x82): undefined reference to `_imp____glutCreateWindowWithExit@8'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
...