1

I have heard that we should write the declarations in a header file and the definition in a source file, with both the source and the header having the same name. And then just include the header in the source.

Here is an example myFile.h:

void printer (void);

Here is the implementation of printer in myFile.cpp:

#include "myFile.h"
#include <iostream>
using namespace std;

void printer ()
{
    cout<< "I am a printer";
}

Here is my main.cpp:

#include "myFile.h"

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

Now when I run the program, I get the expected error: undefined reference to printer. But when I see code on github or other projects I see that usually they have included the header file and not the source file. I also tried using the header guards ifndef but still the same error came. The main program is successfully compiled if:

  1. If i include myFIle.cpp in myFile.h

  2. If i include just myFile.cpp in main

What I the general practice while doing the same?

5
  • 1
    Is this your actual code? It should be #include "myFile.h" (i.e. with quotes). Commented Jun 16, 2012 at 10:20
  • 1
    How are you compiling the program? Commented Jun 16, 2012 at 10:23
  • your problem is at the link phase. you should include the resulting file after compiling myFile.cpp (e.g. myFile.obj) to link with main. Commented Jun 16, 2012 at 10:26
  • This is actually a dummy code, but yes thats the structure that i am using.. I forgot those quotes only here, but i am including them in my program Commented Jun 16, 2012 at 10:40
  • I am using codeblocks ide with gcc/g++ compiler on linux Commented Jun 16, 2012 at 10:55

4 Answers 4

4

You should include your myFile.cpp in the linking process:

g++ myFile.cpp main.cpp

The error message undefined reference to printer is actual a linker error, not a compiler error.

Explanation

If you use only g++ main.cpp compiler won't create code from myFile.cpp. He knows that there should be a function with the signature void printer(void), but he doesn't now yet where this function is. He completely ignores this fact, since you can provide pre-compiled object files ("myFile.o") and link those later:

g++ myFile.cpp -c       # compile myFile.cpp
g++ main.cpp -c         # compile myFile.cpp
g++ myFile.o main.o     # link both files together.

-c will tell g++ only to compile the files, but not link them together to an executable. This is done by a linker (g++ will probably call ld in your configuration). The linker will create an executable which contains all needed libraries like libc++ and actual code.

IDE remarks

If you use an IDE make sure that all needed files are included in the project. This includes all header and source files and linkage options for additional libraries.

Sign up to request clarification or add additional context in comments.

6 Comments

I am using an IDE with g++ on linux!!
@neo1691: Which one? It seems like it doesn't compile all files that are inside of your project. If your IDE doesn't have a project management function you should either change the IDE or have a look at its options. If it's geany then you should write a makefile. Either way you have to tell your IDE that it needs to compile and link those files.
Codeblocks IDE (nightly), I never used those three files in a project workspace, maybe i should try doing the same
Hey the above code worked perfectly when i used the project workspace of the IDE!!SO he project is well configured to do that!!
@neo1691: I don't get your last comment - did this fix your problem? The double exclamation marks "!!" always confuse me…
|
1

When yourself define a header file and want to include it, you should enclose it "", such as :

#include "myFile.h"

2 Comments

Opps that was a typo... I am enclosing the headers in "" . Editing the OP
If you get same error, certainly : 1. you didn't define prototype in *c or *cpp 2. you forgot define in *h 3. don't use -lyourlib @neo1691
1
#include "myFile.h" // would be better.

It seems you forgot the " surrounding the include.

1 Comment

I forgot them here.. While giving an example. Sorry!! But my question still stands
0

You should use

#include "myFile.h"

or

#include <myFile.h>

the later is rather for system libraries. Both forms differ in the way the search the file. You find more details on

http://msdn.microsoft.com/en-us/library/36k2cdd4%28v=vs.71%29.aspx

1 Comment

I updated the main post!! I made a mistake here while asking the question.. I do use those quotes while including headers, either from local directory ("") or from system directory (<>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.