-1

How do I compile mutlitple .h files within a .cpp file in linux terminal using g++ in the terminal. I tried compiling two different .h files with one .cpp file and it didn't work. How can I get this to work?

Sean
  • 957
  • 2
  • 11
  • 15
  • 2
    What do you mean by 'link'? Typically only object files are linked. – M.Babcock Jan 01 '12 at 04:30
  • You don't "link" .h files, you `#include` them. The .h files may or may not declare external classes and methods in other .cpp files or libraries that you need to include in your link/bind operation used to create the final executable, but that's entirely separate from the .h files themselves. – Hot Licks Jan 01 '12 at 04:33
  • @M.Babcock edited hopefully now it makes more sense. – Sean Jan 01 '12 at 04:37
  • 4
    No, it still doesn't make sense. Post your source code. Or your command line that doesn't work. Something. Post it. – jmucchiello Jan 01 '12 at 04:39
  • @Sean - I assume Johnny's answer below will meet your requirements. If not please elaborate. – M.Babcock Jan 01 '12 at 04:40
  • What do you mean "doesn't work"? – Brian Neal Jan 01 '12 at 04:48
  • 2
    "it didn't work" is not very descriptive. Please read http://tinyurl.com/so-hints and then provide us with a better description of the problem that you are trying to solve so that we can help. – johnsyweb Jan 01 '12 at 04:51
  • 1
    I assume you are getting errors during the link stage of building the program. I can only guess that you have included the headers but not the proper g++ flags to link in the actual libraries that go with them. What headers (.h files) are you including? – vdbuilder Jan 01 '12 at 04:53
  • @Sean: If the errors are the ones in [your earlier question](http://stackoverflow.com/questions/8691204/g-compile-error-h-file), can we close this as a duplicate question? – johnsyweb Jan 01 '12 at 07:27

4 Answers4

1
#include "header.h"
#include "other_header.h"

Just #include them. I think that's what you mean by link.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Will Coughlin
  • 673
  • 1
  • 7
  • 17
0

I don't know what you mean by "link".

You #include the .h files from the .cpp file.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
0

You would have to have seperate #include "path-to-file.h" for both .h files in your .cpp file.

Johnrad
  • 2,637
  • 18
  • 58
  • 98
0

Are you by chance using precompiled headers of some sort? If so, you may need to update the precompiled headers to include changes. I've only had experience with using Microsoft's Visual Studio with precompiled headers, so I'm not sure of the exact way it's done with g++.

Your question is very unclear, and your terminology shows that you don't really understand the system very well. While that's not a problem in and of itself (we were all once beginners), you need to be extra careful to really pay attention to people that are trying to help you. Posting that "How to Answer" was, in my opinion, an insult to those trying to help; the insult is that you are telling them to answer what the question is asking for, but you are not making it clear what you are actually asking for in the first place.

Marty Fried
  • 487
  • 1
  • 5
  • 12