0

well I want to understand linking with headers and other .cpp (functions for example) so my quastion is why I get "undefined reference to 'afis(). There are sample example and I want to clarify this. Also sorry for my bad english :D.

There is main:

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

using namespace std;
int main()
{
    afis();
    return 0;
}

There is an function named function.cpp:

#include <iostream>

using namespace std;

void afis(){
    cout <<"yehe";
}

And there is the header :

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

void afis();

#endif // FUNCTIONS_H_INCLUDED
zVoxty
  • 39
  • 6

1 Answers1

2

While the C++ compiler automatically "pulls in" referenced header files, it can't do that for the actual .cpp code files.

Instead of calling

CXX/clang++/g++ main.cpp -o hello

you need to manually include all relevant code files:

CXX/clang++/g++ main.cpp functions.cpp -o hello

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • Hi Mahmoud, where to include this line, I'm using Codeblocks and I don't have "make files" file . – zVoxty Mar 04 '16 at 19:25
  • @zVoxty You might find [this](http://stackoverflow.com/questions/24715864/problems-importing-libraries-to-my-c-project-how-to-fix-this) helpful. Just assure that `functions.cpp` is part of your codeblocks project. – πάντα ῥεῖ Mar 04 '16 at 19:40
  • @πάνταῥεῖ thanks for stepping in. I probably haven't touched CodeBlocks since 2006! – Mahmoud Al-Qudsi Mar 04 '16 at 19:49