How do I create header files?

Need some help with creating header files and linking them..

I have been trying this simple example:

Simple.h:

1
2
3
4
5
6
#ifndef Simple_h
#define Simple_h
const double pi=3.149;
double area_of_circle(int);
#endif


Simple.cpp:

1
2
3
4
5
#include"Simple.h"
double area_of_circle(int r)
{
  return pi*r*r;
}


SimpleTest.cpp:

1
2
3
4
5
6
7
#include"Simple.h"

int main()
{
    double area=area_of_circle(10);
    return 0;
}


I get the following Linker Error:
[Linker error]undefined reference to 'area_of_circle(int)'
Id returned 1 exit status.

I have been trying to figure out whats wrong with my code.
Using Dev C++ 4.9.9.2
on 32 bit windows machine

Thanks For reading
closed account (o3hC5Di1)
Hi there,

I dont' think it's the code - but I think you're not compiling SimpleTest.cpp and Simple.cpp together.

I have no experience with Dev C++ or the compiler it uses, but for instance on gcc one woul do:

$ g++ -Wall --ansi -o simpletest.exe SimpleTest.cpp Simple.cpp


The compiler needs both files in order to create object code of them, if you only feed it one file the second won't be compiled and hence the linker will complain that it can't find the function in that file.

All the best,
NwN
It might work if you put all the files in the same project in your IDE.
@NwN Thank you for sharing that knowledge Sir.

I don't knw how to compile them together in Dev C++.
@Peter87,

Sir, all these files are in same directory..
It worked by putting them in the project....
So what does a project do? does it causes all files to be compiled at the same time?
Thank you Everyone.
closed account (o3hC5Di1)
Hi there,

Yes, it causes all the files in the project to be compiled for you.
Some compilers even have functions to check if a file was modified and if not, it won't recompile it again, resulting in faster compilation while develloping.

On a sidenote, Dev C++ is really unpopular as an IDE because it's quite old and apparently has many flaws, though note that I've never used it myself.
As an alternative, I hear many good things about Code::Blocks - though I don't use that myself either.

Just for your information :)

All the best,
NwN
Dev-C++: it has over 300 bugs that have not been fixed, it has not had its MinGW updated in something like 10 years, if you look in the articles section, there is a rant about it.

Code::Blocks: it is new, its interface is shiny, it has daily bug fixes published, and it is just as free as Dev-C++.

When in Code::Blocks (just dump Dev-C++), you make a project, make a new header and .cpp file besides main.cpp (it will automatically prompt you and ask if you want to add it to the porject--you do), and then compile the project (which, as a side note, is the only thing you can compile while a project is open, so be careful).
First of all, Thank you Everyone for sharing this useful info.

I know dev C++ is outdated, but I only use it because its easy to use. I tried to use Microsoft Virtual C++ Ide but I could not figure out how to use it. I also tried using eccipse for C\C++ but it keeps on giving me some error like ''could not find binary''. while learning 'C' I used to work with Turbo C++,it was a nice ide. I have'nt tried code::blocks but will surely try it.. Thanks again.
Topic archived. No new replies allowed.