Sign in to follow this  
lordcorm

Dev-C++ linking Question?

Recommended Posts

Hey everyone, I was wondering how do you link files in Dev-C++ with different functions? Like say i make one file wiht the funtion "town ()" in it and i wannted to link it with the "main()" file how would i do that and do i have to declare the "town ()" function in the main file still?

Share this post


Link to post
Share on other sites
Declare the town function in a header file with the code in a cpp file.
Then include the include that town is in in your main source file.

Share this post


Link to post
Share on other sites
Firstly, you're going to need a project, and you're going to have to have all of your files in that project. Now, you write functions and later classes into any .cpp/.c file that you want to. So, you might have "town.cpp" that you define town() in.

Now, if you want to use a function or class that is defined in another file, you'll have to give a declaration of that function or class. So, in main.cpp you could put "int town();" and that would be the declaration of the function town(), if town returns an integer (if not, just put its return type there instead of the int). There's no need to link the function or town.cpp, just as long as you tell it that there is a town() function, it's fine.

However, this is tedious and bloats your files. Instead, you could make a .h file and put the declaration in that. So, town.h might have "int town();" in it. Then you could put "#include "town.h"" at the top of your main.cpp file (before you start main()).

Also, search around in the article section of this site, you'll find some nice articles about this. Hope that helps.

Share this post


Link to post
Share on other sites
Here an example


town.h

#ifndef TOWN_H_
#define TOWN_H_

void town();

#endif





then in main.cpp

#include "town.h"
//other includes

int main()
{
std::cout<<"in main...not in town.";
}

void town()
{
std::cout<<"in town...";
}





Hope that helps.

Chad


EDIT: TO LATE

Share this post


Link to post
Share on other sites
wouldnt it be easyer to make a genaric header file like this:

// towns.h
// header file for towns

void town_1 ();
void town_2 ();
void town_3 ();

then make the town-1.ccp,ect....
then the main file

// main.ccp
#include <iosteam>
#include "towns.h"
using namespace std;

int main (){
town_1 {};
}


Whould that work?

Share this post


Link to post
Share on other sites
Quote:
Original post by lordcorm
wouldnt it be easyer to make a genaric header file like this:

// towns.h
// header file for towns

void town_1 ();
void town_2 ();
void town_3 ();

then make the town-1.ccp,ect....
then the main file

// main.ccp
#include <iosteam>
#include "towns.h"
using namespace std;

int main (){
town_1 {};
}


Whould that work?

Yes, as long as you still give definitions for town_1(), town_2(), etc. in some .cpp file.

Also, put include guards on your header:

#ifndef HEADER_NAME_H
#define HEADER_NAME_H

// bla bla

#endif


This will solve problems you will have with headers in the future.

Share this post


Link to post
Share on other sites
I know this is a little off topic, but along the same lines of his question. Is it possible to have one header file that includes all the functions you may use already declared. That way you can simply "#include functionsdeclared.h", and than simply define them in the .cpp files that call them?

Share this post


Link to post
Share on other sites
Yes you can, however if you change that file (eg add another function) you will be forced to recompile all the source files that include this header file. It is best practise to only put function prototypes together in one file if they are related in some way (eg graphic functions).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this