c++ extern variable not visible - c++

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.

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.)

This tells the compiler there is a variable named gApp but it is defined somewhere else:
extern GameApp *gApp;
because that definition does not exist, the linker fails.
Add the following to another (and only one) source file:
GameApp *gApp;

With extern, you tell the compiler that the variable exists, but it is located somewhere else. The compiler believes you the variable exists, and
All you have to do is create the actual variable somewhere in the source. You can do this by simply adding something like GameApp *gApp; somewhere. For example in your cpp file.

Same as the previous answers from other guys, you announced the existence of gApp, but you did not actually provide it.
Add one more word: I suggest you put the definition of gApp in a "GameApp.cpp" file(not GameApp.hpp), and put the declaration of it in a "GameApp.h" file.

Related

Simply including <opencv2/opencv.hpp> results in linking error

Simply including the OpenCV header results in linking error. Why is that?
// test.cpp
#include <opencv2/opencv.hpp>
int foo();
int bar();
int main() {
}
If I compile the file with g++ test.cpp, the following linking error occurs:
/tmp/ccugmQl4.o: In function `cv::String::~String()':
test.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccugmQl4.o: In function `cv::String::operator=(cv::String const&)':
test.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
If I compile with g++ test.cpp -lopencv_core, it works all right.
My question is:
It seems to me that there's no need to resolve undefined symbols if I do not use it, like the functions foo and bar. There's no definition for them but the compile-link process works alright.
I don't use any OpenCV functions either. Why is there linking error only for OpenCV functions?
And what kinds of stuff defined in headers can cause such a linking error?
If you tweak your example a little bit
// test.cpp
int foo();
int bar() {
foo();
}
int main() {
}
You would notice that it'd stop working because linker won't be able to understand what is foo();
The same thing happens when you include opencv header - there are references to functions which are declared but since you never link opencv itself - linker can't figure what those functions are and where to get them.

C++ Cygwin Undefined reference to constructor

While trying to build my small test I encounter an error I don't understand why it shouldn't work. I'm using Eclipse and Cygwin.
The Header and the Source files are separated into different Folders and I put them also into the Cygwin include folders.
Console log
16:05:41 **** Incremental Build of configuration Debug for project Testarea ****
make all
Building target: Testarea.exe
Invoking: Cygwin C++ Linker
g++ -o "Testarea.exe" ./Source/lint.o ./Source/tester.o
./Source/tester.o: In function `main':
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4: undefined reference to `lint::lint()'
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4:(.text+0x20): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `lint::lint()'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: Testarea.exe] Error 1
16:05:41 Build Finished (took 348ms)
tester.cpp
#include <iostream>
#include <lint.h>
int main(){
lint* a = new lint();
std::cout << "hallo";
return 0;
}
lint.cpp
class lint{
private:
int* a;
public:
lint(){
a = new int();
};
lint(int b){
a = new int(b);
};
lint(lint& b){
a = new int(b.value());
};
int value(){
return *a;
};
};
lint.h
#ifndef HEADER_LINT_H_
#define HEADER_LINT_H_
class lint{
public:
lint();
lint(int b);
lint(lint& b);
int value();
};
#endif
Your problem is that you've got 2 classes there. One called lint and the other called lint but only available in the lint.cpp file.
Implementations are done:
#include "lint.h"
lint::lint() {}
and so forth.

Creating a Makefile for C++ and C source code [duplicate]

I'm getting an undefined referenced error, not knowing the reason why.
So I have 2 files which makes a static lib : keyboard_input.c, keyboard_input.h
Here's the content of the .h file:
#ifndef __MOD_KBINPUT__
#define __MOD_KBINPUT__
int kbInit();
int kbWait();
int kbTest();
#endif
And the CMakeLists.txt file looks like this:
FILE(
GLOB_RECURSE
sources
*.c
)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/utils/kbreader")
ADD_LIBRARY(keyboardReader ${sources})
Compiling this lib gives some warnings:
src/utils/kbreader/keyboard_input.c: In function ‘kbInit’:
src/utils/kbreader/keyboard_input.c:13:14: warning: assignment from incompatible pointer type [enabled by default]
src/utils/kbreader/keyboard_input.c: In function ‘kbWait’:
src/utils/kbreader/keyboard_input.c:21:55: warning: passing argument 4 of ‘fread’ from incompatible pointer type [enabled by default]
/usr/include/stdio.h:708:15: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘struct FILE *’
Now, for my main executable (main.cpp):
#include <keyboard_input.h>
int main()
{
kbTest();
return 0;
}
Processed by the following CMakeLists.txt file:
include_directories("${PROJECT_SOURCE_DIR}/include/utils/kbreader")
file(
GLOB_RECURSE
srcs
*.cpp
)
add_executable(
PEM
${srcs}
)
target_link_libraries(PEM keyboardReader)
Ends up getting that error:
CMakeFiles/PEM.dir/main.cpp.o: In function `main':
main.cpp:(.text+0xb): undefined reference to `kbTest()'
collect2: ld returned 1 exit status
make[2]: *** [src/PEM/main2/PEM] Error 1
make[1]: *** [src/PEM/main2/CMakeFiles/PEM.dir/all] Error 2
The libkeyboardReader.a is created, and the kbTest() function doesn't do anything except
{return 0; }
If I set the definition of kbTest() in the header file, it works.
But there's something i don't get, when i type: make keyboardReader here is the output:
[ 73%] Building C object src/utils/kbreader/CMakeFiles/KeyboardReader.dir/keyboard_input.c.o
[Warning explained above]
Linking C static library ../../../lib/libKeyboardReader.a
Is there something wrong? Does the note error message makes my lib omit the keyboard_input.c file?
You're mixing C and C++ files. To make that work, you just have to tell the C++ compiler that it's calling a C function, by changing the header file like so:
#ifndef MOD_KBINPUT
#define MOD_KBINPUT
/* note I also fixed the macro so you aren't using a system-reserved name */
#if __cplusplus
/* this is the important part */
extern "C" {
#endif
int kbInit();
int kbWait();
int kbTest();
#if __cplusplus
}
#endif
#endif
Otherwise the C++ compiler assumes the function will be given a C++ internal name (which encodes all the type information in the signature, this is what lets the linker distinguish between overloaded functions) and then the linker doesn't find it.

Unexpected undefined reference

I'm getting an undefined referenced error, not knowing the reason why.
So I have 2 files which makes a static lib : keyboard_input.c, keyboard_input.h
Here's the content of the .h file:
#ifndef __MOD_KBINPUT__
#define __MOD_KBINPUT__
int kbInit();
int kbWait();
int kbTest();
#endif
And the CMakeLists.txt file looks like this:
FILE(
GLOB_RECURSE
sources
*.c
)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/utils/kbreader")
ADD_LIBRARY(keyboardReader ${sources})
Compiling this lib gives some warnings:
src/utils/kbreader/keyboard_input.c: In function ‘kbInit’:
src/utils/kbreader/keyboard_input.c:13:14: warning: assignment from incompatible pointer type [enabled by default]
src/utils/kbreader/keyboard_input.c: In function ‘kbWait’:
src/utils/kbreader/keyboard_input.c:21:55: warning: passing argument 4 of ‘fread’ from incompatible pointer type [enabled by default]
/usr/include/stdio.h:708:15: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘struct FILE *’
Now, for my main executable (main.cpp):
#include <keyboard_input.h>
int main()
{
kbTest();
return 0;
}
Processed by the following CMakeLists.txt file:
include_directories("${PROJECT_SOURCE_DIR}/include/utils/kbreader")
file(
GLOB_RECURSE
srcs
*.cpp
)
add_executable(
PEM
${srcs}
)
target_link_libraries(PEM keyboardReader)
Ends up getting that error:
CMakeFiles/PEM.dir/main.cpp.o: In function `main':
main.cpp:(.text+0xb): undefined reference to `kbTest()'
collect2: ld returned 1 exit status
make[2]: *** [src/PEM/main2/PEM] Error 1
make[1]: *** [src/PEM/main2/CMakeFiles/PEM.dir/all] Error 2
The libkeyboardReader.a is created, and the kbTest() function doesn't do anything except
{return 0; }
If I set the definition of kbTest() in the header file, it works.
But there's something i don't get, when i type: make keyboardReader here is the output:
[ 73%] Building C object src/utils/kbreader/CMakeFiles/KeyboardReader.dir/keyboard_input.c.o
[Warning explained above]
Linking C static library ../../../lib/libKeyboardReader.a
Is there something wrong? Does the note error message makes my lib omit the keyboard_input.c file?
You're mixing C and C++ files. To make that work, you just have to tell the C++ compiler that it's calling a C function, by changing the header file like so:
#ifndef MOD_KBINPUT
#define MOD_KBINPUT
/* note I also fixed the macro so you aren't using a system-reserved name */
#if __cplusplus
/* this is the important part */
extern "C" {
#endif
int kbInit();
int kbWait();
int kbTest();
#if __cplusplus
}
#endif
#endif
Otherwise the C++ compiler assumes the function will be given a C++ internal name (which encodes all the type information in the signature, this is what lets the linker distinguish between overloaded functions) and then the linker doesn't find it.

'class X' has no member 'Y'

This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited