LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C++, building a binary, file inclusion... (http://www.linuxquestions.org/questions/programming-9/c-building-a-binary-file-inclusion-945887/)

Netooo 05-19-2012 09:14 AM

C++, building a binary, file inclusion...
 
Hi, I wonder if someone can help me with this, it's a simple but yet important problem.

I'm trying to learn C++ and now I'm studying multiple file codes. I've learned that I can use pre-processor to deal with multiple file inclusions and stuff (#ifndef #define #endif), but I have noticed something and I don't know how to solve it.

Let's say that we have 3 files: MyClass.hpp (containing prototypes), MyClass.cpp (containing class definition), and main.cpp

Obviously both main.cpp and MyClass.cpp should have: #include "MyClass.hpp".

Here's the problem, I'm learining to compile to obtain object files (*.o) and then link them together but I noticed that binary (exe) files are larger when doing this than when compiling to exe directly a single file with all the code inside (all code in main.cpp). I think this is because prototypes are only compiled once when doing this, but when using multiple files both object codes main.o and MyClass.o will have compiled an "instance" of the MyClass.hpp prototype file since include guards only protect from redefining in each file individually (or at least the way I'm doing it, compiling each file individually like this: g++ -c file.cpp) and then linking them together: g++ file1.o file2.o file3.o...

Please help.

smallpond 05-19-2012 11:24 AM

Your .hpp files should not be creating any storage at all, just defining things for the .cpp files. If you have code or data defined in the .hpp files, then move it to a cpp where it will only be created once.

If that's not it, then maybe you are including duplicate library code into both .o files. That would mean that your compiler options are wrong.

If THAT's not it then maybe it is only a little bit larger? When linking two separate .o files there may be some glue involved to do long jumps between separate compilations and to store external symbols. That's ok.

pan64 05-19-2012 11:37 AM

actually it depends on the content of the files, so right now I cannot decide. Theoretically splitting a source file into two parts will not modify the result (or just a bit), but there are so many tricks, so many possibilities.

Doc CPU 05-19-2012 11:41 AM

Hi there,

Quote:

Originally Posted by smallpond (Post 4682708)
Your .hpp files should not be creating any storage at all, just defining things for the .cpp files. If you have code or data defined in the .hpp files, then move it to a cpp where it will only be created once.

right, and actually generating code or data from header files is considered bad style. Header files should only contain symbol definitions and declarations.

Quote:

Originally Posted by smallpond (Post 4682708)
If that's not it, then maybe you are including duplicate library code into both .o files. That would mean that your compiler options are wrong.

If THAT's not it then maybe it is only a little bit larger? When linking two separate .o files there may be some glue involved to do long jumps between separate compilations and to store external symbols. That's ok.

Or the thread starter has debugging info included in his object and executable files (e.g. symbol tables or line number/code address xref tables). If there's information about external symbols as well, that info may get duplicated.

[X] Doc CPU

Netooo 05-19-2012 01:08 PM

My hpp contains only a class: vars and function prototypes inside the class body.

How can I tell the compiler to don't put the debugging info in the binary files?

pan64 05-19-2012 02:11 PM

that is the -g flag
-g means you want to add debugging info, so you need to remove it.


All times are GMT -5. The time now is 11:04 PM.