Cpp Linker Error Undefined Reference To
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
C++ Linker Error Undefined Reference To
About Us Learn more about Stack Overflow the company Business Learn more about c++ linker error undefined reference to function hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join dev c++ linker error undefined reference to winmain@16' the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Linker Error C++ “undefined reference
Undefined Reference To C++ Error
” [duplicate] up vote 8 down vote favorite 2 Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I fix it? Trying to compile my program via g++ -o prog1 main.cpp -std=c++0x I get the error: /tmp/cc1pZ8OM.o: In function `main': main.cpp:(.text+0x148): undefined reference to `Hash::insert(int, char)' collect2: error: ld returned 1 exit status main.cpp #include
Undefined Reference To Function C++
error message (or “unresolved external symbol, for Visual C++ users). This is not actually a message from the compiler, but is emitted by the linker, so the first thing to do is to understand what the linker is, and what it does. Linker c++ undefined reference to class 101 To understand the linker, you have to understand how C++ programs are built. For all
C++ Undefined Reference To Constructor
but the very simplest programs, the program is composed of multiple C++ source files (also known as “translation units”). These are compiled separately, using the undefined reference to c++ static variable C++ compiler, to produce object code files (files with a .o or a .obj extension) which contain machine code. Each object code file knows nothing about the others, so if you call a function from one object file that exists http://stackoverflow.com/questions/14557657/linker-error-c-undefined-reference in another, the compiler cannot provide the address of the called function. This is where the the linker comes in. Once all the object files have been produced, the linker looks at them and works out what the final addresses of functions in the executable will be. It then patches up the addresses the compiler could not provide. It does the same for any libraries (.a and .lib files) you may be using. And finally it writes the executable file out https://latedev.wordpress.com/2014/04/22/common-c-error-messages-2-unresolved-reference/ to disk. The linker is normally a separate program from the compiler (for example, the GCC linker is called ld) but will normally be called for you when you use your compiler suite’s driver program (so the GCC driver g++ will call ld for you). Traditionally, linker technology has lagged behind compilers, mostly because it’s generally more fun to build a compiler than to build a linker. And linkers do not necessarily have access to the source code for the object files they are linking. Put together, you get a situation where linker errors, and the reasons for them, can be cryptic in the extreme. Undefined reference Put simply, the “undefined reference” error means you have a reference (nothing to do with the C++ reference type) to a name (function, variable, constant etc.) in your program that the linker cannot find a definition for when it looks through all the object files and libraries that make up your project. There are any number of reasons why it can’t find the definition – we’ll look at the commonest ones now. No Definition Probably the most common reason for unresolved reference errors is that you simply have not defined the thing you are referencing. This code illustrates the problem: int foo(); int main() { foo(); } Here, we have a declaration of the function foo(), which we call in main(), but no definition. So we get the error (slightly edited for clarity): a.cpp:(.text+0xc): und
30 Days In the Last 90 Days In the Last 6 Months Technical Support Overview Search http://www.keil.com/support/docs/3136 Contact Assistance Request Feedback Support Resources Support Knowledgebase Article Index Top 10 Articles Product Manuals Application Notes Downloads Product Updates Discussion Forum Books Product Information Software & Hardware http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka11215.html Products ARM Development ToolsC166 Development ToolsC51 Development ToolsC251 Development ToolsDebug AdaptersEvaluation Boards Product Brochures Newsletters Home/Technical Support GCC: LINKER ERROR (UNDEFINED REFERENCE WITH C++) Information in this article undefined reference applies to: GNU C Compiler for ARM All Versions SYMPTOM The GNU linker gives error messages while linking an application which contains class declarations and class instances. CAUSE The class declaration specifies a constructor and/or destructor, but the constructor/destructor function is missing. class clf { public: clf(); // Constructor (ctor) ~clf(); // Destructor (dtor) int n1, n2, undefined reference to n3; }; clf clf1; // class object int main (void) { return (0); } The linker gives the error messages which look like the following ones: .\obj\blinky.o(.text+0x40): In function '__static_initialization_and_destruction_0': /cygdrive/c/Keil/ARM/GNU/Examples/Blinky/blinky.cpp(92): error: undefined reference to 'clf::~clf [in-charge]() 'blinky.o' (.text+0x44):blinky.cpp:92: undefined reference to 'clf::clf[in-charge]()' RESOLUTION Add the constructor(s) and/or destructor(s) as shown below: class clf { public: clf(); // Constructor (ctor) ~clf(); // Destructor (dtor) int n1, n2, n3; }; clf::clf () { // define ctor n1 = n2 = n3 = 0; } clf::~clf() { // define dtor } clf clf1; // class object int main (void) { return (0); } SEE ALSO µVision DEBUGGER: SOURCE LEVEL DEBUGGING WITH GNU ARM Last Reviewed: Monday, December 10, 2012 Did this article provide the answer you needed? Yes No Not Sure Products Development Tools ARM C166 C51 C251 µVision IDE and Debugger Hardware & Collateral ULINK Debug Adaptors Evaluation Boards Product Brochures Device Database Distributors Downloads MDK-ARM C51 C166 C251 File downloads Support Knowledgebase Discussion F
messages while linking an application which contains class declarations and class instances. CAUSE The class declaration specifies a constructor and/or destructor, but the constructor/destructor function is missing. class clf { public: clf(); // Constructor (ctor) ~clf(); // Destructor (dtor) int n1, n2, n3; }; clf clf1; // class object int main (void) { return (0); } The linker gives the error messages which look like the following ones: .objlinky.o(.text+0x40): In function `__static_initialization_and_destruction_0': /cygdrive/c/Keil/ARM/GNU/Examples/Blinky/blinky.cpp(92): error: undefined reference to `clf::~clf [in-charge]() 'blinky.o' (.text+0x44):blinky.cpp:92: undefined reference to `clf::clf[in-charge]()' RESOLUTION Add the constructor(s) and/or destructor(s) as shown below: class clf { public: clf(); // Constructor (ctor) ~clf(); // Destructor (dtor) int n1, n2, n3; }; clf::clf () { // define ctor n1 = n2 = n3 = 0; } clf::~clf() { // define dtor } clf clf1; // class object int main (void) { return (0); } Article last edited on: 2006-07-10 18:41:27 Rate this article Disagree? Move your mouse over the bar and click Did you find this article helpful? Yes No How can we improve this article? Submit Rating and Feedback Link to this article Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential Home