954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

#include custom files

Hello, in 'cpp' code if I wish to include a header file its .H...ie

//function definitions are located in function.h . The implementation is loaded in functions.cpp, however some of the functions take parameters created in main. Therefore rather than including the following:

#include ... function.h function.cpp ... int main()...

that works but isnt the corect / best way is it? But when I create a link in the .h to the .cpp and vice versa it errors...any ideas?

Is the quote way the only way to fix it?

Cheers

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

never never include *.cpp files inside othere *.cpp files. The *.cpp files should be compiled separately and both linked together. The header.h file should be included in both *.cpp files

// header.cpp
//
// system includes
#include <iostream>
// local includes
#include "header.h"

// header implmentation code
//
// blaba
// main.cpp
// header.cpp
//
// system includes
#include <iostream>
// local includes
#include "header.h"

int main()
{
   // blabla
}

now depending on the compiler you should have a project that includes both *.cpp files and links them both along with other system libraries to produce the final executable program.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok then this aint working right...at the moment i have...

in main.cpp

#include <iostream>
using namespace std;

#include functions.cpp
#include funct.h


the reason I've done it because its compiles... if I did this:

main.cpp

#include <iostream>
using namespace std;

#include funct.h 

-----

funct.h 

#include functions.cpp

-------

and try and compile that I get over 73 error messages since some varibles that main create are used in the functions........

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

DUH!! taking a break and comming back helps! I had 2 .H files that both needed including in the .cpp file! Thanks lads for your help, most appricated :D

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You