1

I have two files:

hello.h and hello.cpp

hello.h

#ifndef __HELLO_H__
#define __HELLO_H__

using namespace std;

void PrintMessage();

#endif

hello.cpp

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

using namespace std;

void PrintMessage()
{
     cout << "I want to be displayed!";
}

Now, I want to use PrintMessage() in a new .cpp file, but I keep getting an error message. This is what I'm doing:

printingmessage.cpp

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

using namespace std;

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

Am I just doing something blatantly wrong? I do have all of them in the same folder; I assume it has something to do with Dev-C++ (what I'm using to write/compile/run), but I can't figure it out. Anyone have any ideas?


I created a folder on my desktop, put all three files inside, and I tried to compile my printingmessage.cpp file exactly as it is. This is the error I'm getting:

[Linker error] Undefined reference to 'PrintMessage()' 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Bob John
  • 3,688
  • 14
  • 43
  • 57
  • 4
    All identifiers with adjacent underscores are [reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for the implementation. Keep that in mind when using header guards (or any other names, but those are the most common violation). Even changing them to one underscore still violates another rule (starting with _), so be sure to keep that in mind as well. Also, `using namespace std;` is something you definitely **do not** want in the global space of your header files. It can cause chaos for whoever includes it. – chris Sep 08 '12 at 22:19
  • 2
    As for the error, is it a linker error (*undefined reference to PrintMessage*)? If so, your `hello.cpp` needs to be built into your executable as well. – chris Sep 08 '12 at 22:22
  • I should have stated that I have no control over the .h and .cpp files. I can also only do #include "hello.h" in my main file. – Bob John Sep 08 '12 at 22:24
  • You're not telling us why you think you're doing something blatantly wrong. Are you observing something blatantly wrong? Does it compile? Does it link? Does it run? – Drew Dormann Sep 08 '12 at 22:25
  • 2
    Do you know how to *link* when using Dev-C++? – Beta Sep 08 '12 at 22:25
  • Did you put all three files into a project together? Typically IDEs will link them correctly for you if you do, but not if you don't. – chris Sep 08 '12 at 22:26
  • currently you're only saying "an error message": you are ***WASTING PEOPLE'S TIME***. i almost downvoted. but maybe this is your first question or something, so, look at the [first posting FAQ](http://www.parashift.com/c++-faq-lite/posting-code.html); even if it is about posting to Usenet it works fine for posting to SO also. – Cheers and hth. - Alf Sep 08 '12 at 22:28
  • 1
    Food for the wolf: where do you think PrintMessage() is being compiled to ? Perhaps an object code file (.o or .obj) ? Maybe link that into your output and it will help. – WhozCraig Sep 08 '12 at 22:38

2 Answers2

0

i don't know dev C++ , but i would strongly advise if you do any serious coding to learn/move to the terminal and use make files, or a newer IDE such as visual studios.

heres a short script you can run save it as bash.sh something like this

g++ hello.cpp -O2 -g -c 
g++ hello.o printmessage.cpp -Wall -O2 -o print

then run it with ./print

pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • Yes, you have the bliss of not having it be the thing given to you by the class environment, but you don't have to make projects in Dev - you can compile and run just one file. While a time and space saver for things that aren't actual projects, you have to link them yourself if such linking is required. – chris Sep 08 '12 at 22:31
0

I assume it has something to do with Dev-C++ (what I'm using to write/compile/run), but I can't figure it out.

I guess so, too. Behind the scenes, the following things have to happen:

  1. Both files get compiled. This creates a *.obj file for every *.cpp file, and uses the header.
  2. The object files are linked against one another and possibly against required libraries.

Your problem lies in the “one another” part of the second step: the code compiles all right, but linking fails. The header file is irrelevant at that point. More precisely, the linker invocation for printingmessage.obj contains a reference to a function which isn't defined in that object file or any of the default libraries. Most likely the problem is due to the *.cpp files not being part of the same project. You need to create a multi-source-file project where you can link multiple object files. How you do that with Dev-C++ is probably somewhere in their manuals.

MvG
  • 57,380
  • 22
  • 148
  • 276