MercurySnail
MercurySnail

Reputation: 11

undefined reference to self-written function

I know it has to be a dumb question, but I can't solve this problem for 5 hours now and it's killing me: I'm learning C++ and doing everything as I was told to, but I can't build my function, because the compiler just wouldn't recognize my function. I tried using an absolutely simple one, but the problem remains. I'm using Code Blocks. The main.cpp, ezmukszik.cpp and ezmukszik.h are in the same project library and I checked in Settings/Compiler/Build Options to explicitly add the working directory to the compiler search dirs. The error message is the following:

in function main
/in line 8/ undefined reference to 'ezmukszik()'

The header:

#ifndef EZMUKSZIK_H_INCLUDED
#define EZMUKSZIK_H_INCLUDED

#include <iostream>

int ezmukszik();

#endif // EZMUKSZIK_H_INCLUDED

The source code:

#include <iostream>

using namespace std;

int ezmukszik()
{
    std::cout << "Hello world!" << std::endl;
    return 0;
}

And the main:

#include <iostream>
#include "D:\BME suli\C++\Programok\ezmukszik\ezmukszik.h"

using namespace std;

int main()
{
    ezmukszik();
    return 0;
}

Any help would be greatly appreciated!

Upvotes: 1

Views: 716

Answers (2)

MercurySnail
MercurySnail

Reputation: 11

I finally managed to solve the problem, it was an annoying solution: I added the source file in Code Blocks from the new file window, and it was displayed under the project sources, but for some reason it still didn't compiled. Adding it with a right click in the rightmost window solved the problem.:) Thank you all for your help!

Upvotes: 0

berkus
berkus

Reputation: 1563

The error is a LINKER error and means your ezmukszik.cpp file is not linked together with the main.o after being compiled to ezmukszik.o.

Check the linking command line.

Upvotes: 1

Related Questions