Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler/linker error "undefined reference"

Hi I am just starting to learn C++. I bought this big C++ for Dummies book and have been going through it. Its been really interesting so far but now I am stuck. I have been googling this problem, but to no avail. I am using I am using codeblocks 10.05 with GNU GCC.

I keep getting an error that says:

In function 'main':
undefined reference to 'SafeCracker(int)'

The code isn't complicated. I am just new and am extremely frustrated. I don't want to skip over this part; I want to know what is going on.

Main:

#include <iostream>
#include "safestuff.h"

using namespace std;

int main()
{
  cout << "Surprise, surprise!" << endl;
  cout << "The combination is (once again)" << endl;
  cout << SafeCracker(12) << endl;
  return 0;
}

Function:

#include <iostream>

using namespace std;

string SafeCracker(int SafeID)
{
    return "13-26-16";
}

Header:

using namespace std;

#ifndef SAFESTUFF_H_INCLUDED
#define SAFESTUFF_H_INCLUDED

 string SafeCracker(int SafeID);

#endif // SAFESTUFF_H_INCLUDED
like image 732
Tetrah Avatar asked Oct 26 '12 00:10

Tetrah


People also ask

How do you resolve an undefined reference?

The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call.

How do you fix a linker error?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

What are linker errors in C++?

Linker Errors: These error occurs when after compilation we link the different object files with main's object using Ctrl+F9 key(RUN). These are errors generated when the executable of the program cannot be generated. This may be due to wrong function prototyping, incorrect header files.

How do you fix undefined symbol in C++?

You need to pass the values to the function Calculate. Variables x, y, z and function are not accessible outside the class and also u need a return type to the function so that you can get the output from the function Calculate.


1 Answers

You are not compiling the second file you listed along with the first one. Try compiling directly with gcc to understand this.

assuming your files are named:

  • main.cpp
  • SafeCracker.cpp
  • safestuff.h

This is what you are doing

gcc main.cpp

While you should be doing this

gcc main.cpp SafeCracker.cpp

Also, SafeCracker.cpp should be including the header file as well, just for clarity. Any reasons why you have them separated?

On another note, from seeing Daniel Hu's answer, <iostream> is automatically including <string> for you. You should not depend on this functionality, and should instead include <string> in each file that uses strings.

(From comment below) You're probably trying to build your main.cpp as a stand-alone file. This will leave SafeCracker.cpp uncompiled. What you need is create a project in Codeblocks and add all three files to it (both *.cpp files as well as the *.h file).

like image 162
jett Avatar answered Sep 23 '22 19:09

jett