"Undefined Reference" linker errors Topic is solved
- ColleenKobe
- Earned some good credits
- Posts: 109
- Joined: Mon Aug 31, 2015 3:47 pm
"Undefined Reference" linker errors
I'm having a problem that I don't know how to fix. wxWidgets compiles all my source files without any problem, but then I get a list of error messages saying "undefined reference to 'x'," where 'x' is the name of a user-defined procedure or function in one of my *.h files. See the attached print-screen.
Overview. I have five user-defined classes in this project. One displays a wxFrame; three display a wxDialog; and the last one, which is the one giving me troubles, does not display a page at all.
The troublesome class is called Global_Data_Class. It contains a storehouse of values that can be "Set" or "Get" from any of the other classes. Any class (file) calling those procedures or functions simply adds the
#include "Global_Data.h"
compiler command at the top of the file. All of the source files compile with zero errors.
All the *.cpp and *.h and the make files reside in the same directory on my computer, and the linker finds all of those files.
To try to solve this problem, I've clicked Build, Clean Workspace, and then Build, Rebuild Workspace. Fail. I've tried switching to wxCrafter and clicking "Generate Code," and then cleaning and rebuilding again. Fail.
I've also searched the wxWidgets forum for "undefined reference," but the hits I got were mainly for system-type missing files, not user-defined procedures.
I'm open to suggestions for places to look to fix this problem.
Thank you in advance!
Software Versions
CodeLite 10.0.4
gcc 4.9.3
tdm-gcc 5.1.0.2
Windows 10 Pro, 64-bit
wxWidgets 3.1.0
wxCrafter 2.6
Target platform 32-bit
Target build debug
Overview. I have five user-defined classes in this project. One displays a wxFrame; three display a wxDialog; and the last one, which is the one giving me troubles, does not display a page at all.
The troublesome class is called Global_Data_Class. It contains a storehouse of values that can be "Set" or "Get" from any of the other classes. Any class (file) calling those procedures or functions simply adds the
#include "Global_Data.h"
compiler command at the top of the file. All of the source files compile with zero errors.
All the *.cpp and *.h and the make files reside in the same directory on my computer, and the linker finds all of those files.
To try to solve this problem, I've clicked Build, Clean Workspace, and then Build, Rebuild Workspace. Fail. I've tried switching to wxCrafter and clicking "Generate Code," and then cleaning and rebuilding again. Fail.
I've also searched the wxWidgets forum for "undefined reference," but the hits I got were mainly for system-type missing files, not user-defined procedures.
I'm open to suggestions for places to look to fix this problem.
Thank you in advance!
Software Versions
CodeLite 10.0.4
gcc 4.9.3
tdm-gcc 5.1.0.2
Windows 10 Pro, 64-bit
wxWidgets 3.1.0
wxCrafter 2.6
Target platform 32-bit
Target build debug
Re: "Undefined Reference" linker errors
My best guess would be that all those methods are declared (in the header file), but they have no body. Are they defined in the .CPP file?
Use the source, Luke!
- ColleenKobe
- Earned some good credits
- Posts: 109
- Joined: Mon Aug 31, 2015 3:47 pm
Re: "Undefined Reference" linker errors
Thank you for replying, doublemax.doublemax wrote:My best guess would be that all those methods are declared (in the header file), but they have no body. Are they defined in the .CPP file?
Yes, the procedures and functions are defined in both the .h file and the .CPP body. Otherwise, I'm pretty sure the code would have generated compiler errors. I've attached Global_Data.h and Global_Data.cpp. They're pretty long, and I don't expect you to scrutinize them. But maybe something will be obvious to you that isn't obvious to me.
Re: "Undefined Reference" linker errors
Code: Select all
void Initialize (Global_Data_Class* GM_Ptr)
{
Local_GM_Ptr = GM_Ptr; // Get a copy of the pointer to the
// global data class. Use the global
// data pointer throughout this class to
// update global memory values.
Code: Select all
void Global_Data_Class::Initialize (Global_Data_Class* GM_Ptr)
{
Local_GM_Ptr = GM_Ptr; // Get a copy of the pointer to the
// global data class. Use the global
// data pointer throughout this class to
// update global memory values.
Basically you just created some global functions instead of class methods.
The syntax without the "Global_Data_Class::" only works inside the actual class declaration.
You only didn't get any compiler errors, because you were not accessing any member variables, just global variables.
Use the source, Luke!
- ColleenKobe
- Earned some good credits
- Posts: 109
- Joined: Mon Aug 31, 2015 3:47 pm
Re: "Undefined Reference" linker errors
Thank you for your quick response. I figured something obvious would be wrong.doublemax wrote:Should be:Code: Select all
void Initialize (Global_Data_Class* GM_Ptr) { Local_GM_Ptr = GM_Ptr; // Get a copy of the pointer to the // global data class. Use the global // data pointer throughout this class to // update global memory values.
Same for the other methods.Code: Select all
void Global_Data_Class::Initialize (Global_Data_Class* GM_Ptr) { Local_GM_Ptr = GM_Ptr; // Get a copy of the pointer to the // global data class. Use the global // data pointer throughout this class to // update global memory values.
Basically you just created some global functions instead of class methods.
The syntax without the "Global_Data_Class::" only works inside the actual class declaration.
You only didn't get any compiler errors, because you were not accessing any member variables, just global variables.
Actually, I did start by adding the
Code: Select all
Global_Data_Class::
Code: Select all
error: prototype for 'void Global_Data_Class::Initialize(Global_Data_Class*) does not match any in class 'Global_Data_Class'
error: candidate is: virtual void Global_Data_Class::Initialize()
error: 'Initialize' was not declared in this scope
How do I modify the declaration(s) in the h file so that I eliminate those errors?
Re: "Undefined Reference" linker errors
In the declaration "Initialize" doesn't take a parameter:
They must be a perfect match, so you have to change one of them.
Code: Select all
virtual void Initialize (void);
Use the source, Luke!
- ColleenKobe
- Earned some good credits
- Posts: 109
- Joined: Mon Aug 31, 2015 3:47 pm
Re: "Undefined Reference" linker errors
doublemax, I do understand that the declarations in the h and cpp files must be identical. I changed the declarations in the cpp file, made a temp copy of the file, and ran a macro to strip out all the lines in the temp file that were not procedure/function declarations. Then I appended semicolons at the end of each declaration, deleted the original procedure/function declarations in the h file, and merged the new procedure and function declarations into the h file.
So the declarations in both places are identical, EXCEPT that there may be extra spaces between words in the h file that don't appear in the cpp file.
Now I'm getting "extra qualification" errors when I compile Global_Data.h. See the screen dump.
I must be missing something else. I did take out the "virtual" command. Do I need to add it back in?
Thank you for your patience.
So the declarations in both places are identical, EXCEPT that there may be extra spaces between words in the h file that don't appear in the cpp file.
Now I'm getting "extra qualification" errors when I compile Global_Data.h. See the screen dump.
I must be missing something else. I did take out the "virtual" command. Do I need to add it back in?
Thank you for your patience.
Re: "Undefined Reference" linker errors
Don't use the "Global_Data_Class::" in the header file. These lines are inside the class declaration already.
The "virtual" is not related to any of the errors. It just means that these methods can be overridden by classes deriving from "Global_Data_Class". But looking at the purpose of the class, i don't think that will happen. So you can remove the "virtual" everywhere if you want. But it doesn't hurt either.I did take out the "virtual" command. Do I need to add it back in?
- Attachments
-
- Global_Data.h
- (18.93 KiB) Downloaded 133 times
-
- Global_Data.cpp
- (24.41 KiB) Downloaded 127 times
Use the source, Luke!
- ColleenKobe
- Earned some good credits
- Posts: 109
- Joined: Mon Aug 31, 2015 3:47 pm
Re: "Undefined Reference" linker errors
That worked, doublemax! Thank you!!!