.Cpp files use in one .cpp file

When we make some .cpp files than what is possibilities to use them in

Page 1 of 1

8 Replies - 1106 Views - Last Post: 14 May 2011 - 07:37 AM Rate Topic: -----

#1 Mangilal Sharma  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 09-December 10

.Cpp files use in one .cpp file

Posted 09 December 2010 - 09:59 AM

Hi Everyone,
i will made some files in .cpp formate thats like stack.cpp, queue.cpp, linked.cpp etc.(all are related to data structure).
i want to use all files in one datastructure.cpp file. Than what are way for it by them my datastructure.cpp file execute than also give outputs of thats file that i used in program.

i.e. codes of the all other files that are i joint to my .cpp file that alls Output of codes is done by only including them.
my e-mail id is **************
please can any one help me fast to fast.

This post has been edited by NickDMax: 09 December 2010 - 10:06 AM
Reason for edit:: Email address removed: This is not how a forum works.


Is This A Good Question/Topic? 0
  • +

Replies To: .Cpp files use in one .cpp file

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: .Cpp files use in one .cpp file

Posted 09 December 2010 - 10:05 AM

You should not "include" .cpp files in .cpp files. You should add the files to your project or makefile to be compiled together.


Jim

This post has been edited by jimblumberg: 09 December 2010 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: .Cpp files use in one .cpp file

Posted 09 December 2010 - 10:08 AM

Sorry Mangilal Sharma -- I had to remove your email address as this is NOT how forums work. If you really do not with to check back with this thread you could always have the forum send you emails when there are responses to the thread.

BTW: It is really a very bad idea to post you email address as it is a sure way to get spam and emails from creepy people.
Was This Post Helpful? 1
  • +
  • -

#4 Mangilal Sharma  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 09-December 10

Re: .Cpp files use in one .cpp file

Posted 14 May 2011 - 05:30 AM

Hello frnds,
I got the solution of my problem.
here i describe this....might be it is usefull also for you-

yes, it is possible that we have include another .cpp files in a .cpp file as follows-
#include "STACK.CPP"
#include "QUEUE.cpp"
#include "D_QUEUE.CPP"
#include "C_QUEUE.CPP"
#include "P_QUEUE.CPP"
#include "SINGLY.CPP"
#include "DOUBLY.CPP"
#include "C_SINGLY.CPP"
#include "C_DOUBLY.CPP"
#include "BINARY.CPP"

these are use as we include header files.
The included files must not contain the main() function....this is only required in main file in which we includes the files....and call functions of included files. for example stack() funcion exist in stack.cpp file then call as stack(); simply where is required in main .cpp file.......hope u will understand it...bye...for further reply here
Was This Post Helpful? -2
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: .Cpp files use in one .cpp file

Posted 14 May 2011 - 05:33 AM

While it is POSSIBLE to do what you say it is NOT the right way to do it! You simply compile all the cpp files together, and use header files to expose the function contained within them!
Was This Post Helpful? 1
  • +
  • -

#6 Mangilal Sharma  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 09-December 10

Re: .Cpp files use in one .cpp file

Posted 14 May 2011 - 05:50 AM

View PostJackOfAllTrades, on 14 May 2011 - 05:33 AM, said:

While it is POSSIBLE to do what you say it is NOT the right way to do it! You simply compile all the cpp files together, and use header files to expose the function contained within them!

thats possible...then why it is not right way...plz explain in few detail...
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: .Cpp files use in one .cpp file

Posted 14 May 2011 - 06:33 AM

The right way to do it will depend on your development environment. Might consist of adding the C++ files and headers to your project. Might just be compiling them on the command line.

g++ -g3 -Wall -pedantic -o myprogram programfile1.cpp programfile2.cpp main.cpp

Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: .Cpp files use in one .cpp file

Posted 14 May 2011 - 06:44 AM

There are many reasons including source (c, cpp) files is considered bad practice. One reason is that when you include a file it is copied directly into the including file. So if you include this file in more than one file you will have multiple copies of this file.

Also when including source files in a source file your compile times will usually be considerably longer. One change in any of these files will usually cause all the files to be recompiled.

Jim
Was This Post Helpful? 2
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: .Cpp files use in one .cpp file

Posted 14 May 2011 - 07:37 AM

Yes it is possible to #include .cpp files. All the #include directive does is paste in the contents of a file into the current compilation unit (what the compiler actually sees). HOWEVER, the reason you don't do this is because the compiler usually compiles each .cpp file separately and creates object files for each source file.

The linker will take the various object files and "link" them together to form the final product (an exe file, or some kind of library file).

So for example say I have a program myprog.cpp and it uses a stack class implmented in stack.h/stack.cpp

well myprog.cpp will #include stack.h, and iostream and other headers. stack.h #includes anything needed to USE the stack like the class definition for the stack class.

So the compiler will first compiler stack.cpp (which will include stack.h) and produce stack.obj, then the compiler will compile myprog.cpp (which will include stack.h) and produce myprog.obj. Then it will pass these object files to the linker which will link together stack.obj, myprog.obj and the library files and produce myprog.exe.

Now if we #include stack.cpp then we have a problem. the myprog.obj will contain all of the same information as stack.obj and so when the linker tries to link together myprog.obj, and stack.obj it will get confused because it has duplicates of function code -- you will receive a bunch of errors about unresolved symbols.

SO! what if you want to #include some code? Well don't name the file .cpp or .c or .cc -- rather name the file .inc or .fnc etc. -- however in general it is just best to stick to the standard naming conventions and use the linker to combine different object files.

The exceptions where you might want to use .inc files or .h files are if you have something that will be included in multiple files (like declarations which is the classic use of .h files), or preprocessor macros OR when you have content/data that will be maintained separately. For example you might put the text for a game into a separate file that is updated by a non-programmer game designer, menus and options that need to be reviewed and approved by third parties etc., X-Macro tables are often #included.

So there ARE reasons to put code into include files, but you SHOULD NOT make these .cpp files and generally they should not contain functions/methods/static variables since these are better placed inside of .cpp files and linked in from the compile object file.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1