Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8021329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:56:50+00:00 2026-06-04T21:56:50+00:00

I use extern variable for my application class so i can forward class function

  • 0

I use extern variable for my application class so i can forward class function to glutDisplayFunction(funcPtr).

main.cpp:

#include "main.hpp"

int main(int argc, char** argv)
{
  gApp = new GameApp();
  return 0;
}

main.hpp:

#ifndef MAIN_HPP
#define MAIN_HPP
  #include "GameApp.hpp"
#endif

GameApp.hpp:

#include <GL/gl.h>
#include <GL/freeglut.h>

class GameApp
{
  public:
  int running;

  GameApp();
  virtual ~GameApp();
  void resize(int width, int height);
  void init(int argc, char** argv, int width, int height);
  void draw();
  void update();
  void key_input(unsigned char key, int x, int y);
};

extern GameApp *gApp;

void display_clb()
{
  if (!gApp)
  {
    return;
  }

  gApp->draw();
}

This is the output:

g++     -o dist/Debug/GNU-Linux-x86/gravity build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/GBody.o build/Debug/GNU-Linux-x86/GameApp.o build/Debug/GNU-Linux-x86/GBodyList.o -lm -lGL -lglfw -lGLU -lglut 
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/viktor/Documents/cpp/Gravity/main.cpp:6: undefined reference to `gApp'
/home/viktor/Documents/cpp/Gravity/main.cpp:7: undefined reference to `gApp'
/home/viktor/Documents/cpp/Gravity/GameApp.cpp:13: undefined reference to `gApp'
/home/viktor/Documents/cpp/Gravity/GameApp.cpp:18: undefined reference to `gApp'
build/Debug/GNU-Linux-x86/GameApp.o: In function `display_clb()':
/home/viktor/Documents/cpp/Gravity/GameApp.cpp:23: undefined reference to `gApp'
build/Debug/GNU-Linux-x86/GameApp.o:/home/viktor/Documents/cpp/Gravity/GameApp.cpp:28: more undefined references to `gApp' follow
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/gravity] Error 1
make[2]: Leaving directory `/home/viktor/Documents/cpp/Gravity'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/viktor/Documents/cpp/Gravity'
make: *** [.build-impl] Error 2

I expected gApp to be visible in my main.cpp and in GameApp class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:56:51+00:00Added an answer on June 4, 2026 at 9:56 pm

    That’s not a compile error, it is a link error. You variable declaration is visible just fine in main.cpp, but you haven’t defined it anywhere – i.e. you don’t allocate space for that variable anywhere.

    You’ll need one (and exactly one) C++ file that defines that variable. Possibly your main.cpp:

    GameApp *gApp;
    

    (You could initialize it too right there, but that is not necessary in this case.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> static i = 5; int main() { extern int i; printf(%d\n,i); return
Can anyone please tell me is there any special requirement to use either EXTERN
I try to use C++ DLL function in my Delphi application. I've the problem
In errno.h , this variable is declared as extern int errno; so my question
How to declare and use global variable with extern in Objective C and other
Use case: Wanna insert custom annotation to fields in java class generated by JAXB
Use case: class MyTask(Task): queue = 'default_queue' def run(self): # do work Normally I
Say I declare a header file with a variable: int count; Then in the
I'm trying to use the Google Closure Compiler to split my application code based
I am trying to set the sys exit call to a variable by extern

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.