Cpp Undefined Reference Error
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 About Us Learn more about Stack cpp undefined reference to vtable Overflow the company Business Learn more about hiring developers or posting ads with us cpp undefined reference to function Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community
Cpp Undefined Reference To Main
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 ” [duplicate] up vote 8 down vote favorite 2 Possible Duplicate:
Cpp Undefined Reference To Constructor
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
tried to compile it and
Undefined Reference To Function C++
I got this error: Undefined reference to 'intro()' Undefined c++ undefined reference to class reference to 'menu(int' I looked into the code and I can't find any obvious undefined reference to c++ constructor errors, so I googled around and didn't find anything that would fix my error... Here's my code: Main.cpp 1 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 https://latedev.wordpress.com/2014/04/22/common-c-error-messages-2-unresolved-reference/ is to understand what the linker is, and what it does. Linker 101 To understand the linker, you have to understand how C++ programs are built. For all 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 C++ compiler, to produce object code files (files with a .o or a .obj extension) which undefined reference contain machine code. Each object code file knows nothing about the others, so if you call a function from one object file that exists 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 undefined reference to 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 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
2
3
4
5
6
7
8
9
#include "headers.h" int main() http://stackoverflow.com/questions/14557657/linker-error-c-undefined-reference { intro(); menu(0); } headers.h 1
2
3
4
5
6
7
8
9
10
11
12
13
14
//all the #includes: #include
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "headers.h" //intro function:: http://www.cplusplus.com/forum/beginner/77999/ pretty simpple stuff: void intro() { cout << endl; cout << endl; cout << endl; cout << endl; cout << " A Game By Mrcerimo" << endl; Sleep(1500); cout << " The Match the pairs game" << endl; Sleep(1500); } //menu function: //it contains two kind of menus: //1.menu is the menu that you get in when you strat the program //2. menu is the menu you can accsess while playing the game , thus the option to continue:: void menu(int x) // takes in int x, so we will know wich menu to choose: { if (x == 0) // first menu { int y; cout << endl; cout << endl; cout << endl; cout << endl; cout << " MENU" << endl; cout << endl; cout << " 1.new game" << endl; cout << " 2.instructions"