Author Topic: Linking problem?  (Read 2677 times)

Bananskrue

  • Guest
Linking problem?
« on: August 29, 2012, 12:53:50 am »
This may just be me making a rookie mistake since I haven't programmed in a long, long time, but I'm getting some weird problems when I'm trying to divide my program into several source/header files.

The current program code looks a little bit something like this:

main.cpp
Code

#include <cstdlib>
#include <iostream>

#include "functions.h"

using namespace std;

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


functions.h

Code

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

void hello();

#endif // FUNCTIONS_H_INCLUDED

Hello.cpp
Code

#include <cstdlib>
#include <iostream>

#include "functions.h"

using namespace std;

void hello()
{
cout << "Hello";
}

The error I am getting is: "Undefined reference to hello();" when it is called in the main.cpp file. If I include Hello.cpp in the header file, everything works fine and dandy, but that's obviously not the way to go. Am I missing something crucial here? All files are under the same project that I made in code::blocks. My SINCEREST apologies if I've either coded something wrong or this is as stated in the rules related to gnu cpp, however I thought all files under the project were supposed to be linked, whereas it does not seem this is the case?

Thanks!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Linking problem?
« Reply #1 on: August 29, 2012, 01:02:52 am »
Read this: http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

It will tell you how to enable full logging and then you can see what is compiled and what is linked.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]