LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2012, 09:14 AM   #1
Netooo
LQ Newbie
 
Registered: Jun 2010
Location: Guadalajara, Jalisco, Mexico
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
Question 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.
 
Old 05-19-2012, 11:24 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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.
 
1 members found this post helpful.
Old 05-19-2012, 11:37 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
1 members found this post helpful.
Old 05-19-2012, 11:41 AM   #4
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by smallpond View Post
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 View Post
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
 
1 members found this post helpful.
Old 05-19-2012, 01:08 PM   #5
Netooo
LQ Newbie
 
Registered: Jun 2010
Location: Guadalajara, Jalisco, Mexico
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 0
Question

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?

Last edited by Netooo; 05-19-2012 at 01:09 PM.
 
Old 05-19-2012, 02:11 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
that is the -g flag
-g means you want to add debugging info, so you need to remove it.
 
1 members found this post helpful.
  


Reply

Tags
compilation, optimize



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Header file inclusion jayadhanesh Linux - Software 2 05-15-2012 08:38 AM
Help: Opening GenICs binary file, writing to binary file for GADGET-2 (in C) parallax147 Programming 0 10-05-2010 11:06 AM
PHP file inclusion warning chus_84 Programming 5 07-18-2008 08:44 AM
File inclusion vulnerabilities coolb Linux - Security 1 05-20-2006 08:51 PM
.h File Inclusion in C++ Hady Programming 6 05-31-2005 08:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration