Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File inclusion errors (C++): undefined reference to ______

Whenever I compile something that #includes a user-defined class, I get these compilation errors that always look like: main.cpp: undefined reference to Complex::Complex(double, double)

I've reduced the problem to a set of three extremely bare files: main.cpp, and for example, Complex.h and Complex.cpp. I still get undefined reference errors. I'm developing in Code::Blocks on Windows but I get the same thing using g++ in Ubuntu. Why does this happen? I've tried building Complex.cpp before main.cpp in Code::Blocks, and I've tried g++ main.cpp Complex.cpp as much as I've tried just g++ main.cpp. Same errors every time.

/*======== main.cpp ========*/
#include "Complex.h"

int main()
{
    Complex A(1.0, 1.0);
    return 0;
}

/*======== Complex.h ========*/
#ifndef _COMPLEX_H
#define _COMPLEX_H

class Complex
{
    public:
    double x, y;
    Complex(double real, double imag);
};

#endif

/*======== Complex.cpp ========*/
#include "Complex.h"

Complex::Complex(double real, double imag)
{
    x = real;
    y = imag;
}

ed: now I get different errors so I must be doing something completely wrong. Using the same code as above, I get:

main.cpp: in function 'int main()':
main.cpp:5:5: error: 'Complex' was not declared in this scope
main.cpp:5:13: error: expected ';' before 'A'

This is bizarre. Everything worked earlier when I had the class in a .cpp file, but that's "bad practice" so I moved my class definitions into .h files and kept the implementation in .cpp files, and now nothing works.

like image 870
thko Avatar asked Jan 04 '12 23:01

thko


People also ask

How do you fix undefined reference error in C?

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. So, we used to show(), i.e., small case names to go further.

How do you fix undefined references to Main?

To fix this error, correct the spelling of the main() function.

How do you fix a undefined reference in C++?

When we compile these files separately, the first file gives “undefined reference” for the print function, while the second file gives “undefined reference” for the main function. The way to resolve this error is to compile both the files simultaneously (For example, by using g++).

What causes unresolved external symbol?

Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.


1 Answers

That's not a compilation error, it's a link error. You need to make sure to link all of your objects together. You can do that in a couple ways:

g++ main.cpp Complex.cpp

Should work fine (and does here when I tried with your example). You can also do it in steps:

g++ -c main.cpp
g++ -c Complex.cpp
g++ main.o Complex.o
like image 181
Carl Norum Avatar answered Oct 04 '22 21:10

Carl Norum