473,804 Members | 3,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

include .cpp file instead of .h file?

I have a main.cpp file that will call a single function. For the sake of
curiousity I would like to place this function in a separate file called
func.cpp and include it in my main.cpp with: #include "func.cpp". But it
gives a multiple definitions error.

Do I always have to make a .h file for a .cpp file if I want to include
the functionality in another module?
Apr 24 '07 #1
4 39434
On Tue, 24 Apr 2007 14:23:00 +0200, desktop wrote:
I have a main.cpp file that will call a single function. For the sake of
curiousity I would like to place this function in a separate file called
func.cpp and include it in my main.cpp with: #include "func.cpp". But it
gives a multiple definitions error.
What are you actually compiling? If you compile both func.cpp and
main.cpp (to object files) then, since main.cpp includes func.cpp, there
will indeed be definitions for your function in both object files and the
linker will complain. If you compile just main.cpp it should work.

But that's not generally the way to go...
Do I always have to make a .h file for a .cpp file if I want to include
the functionality in another module?
Not essential, but it's common practice to write function *definitions*
in .cpp files and function *declarations* in header files, which are then
included where appropriate. An exception might be for inline functions,
where it is conventional to write both declaration and definition in the
same (header) file.

--
Lionel B
Apr 24 '07 #2
desktop wrote:
I have a main.cpp file that will call a single function. For the sake of
curiousity I would like to place this function in a separate file called
func.cpp and include it in my main.cpp with: #include "func.cpp". But it
gives a multiple definitions error.

Do I always have to make a .h file for a .cpp file if I want to include
the functionality in another module?
There's nothing inherently wrong about including a .cpp file since the
compiler doesn't care what the extension is on an included file.
However, you may be missing the bigger picture here. C++ supports
compilation of multiple separate files ("translatio n units" to be
precise). Among these, there are certain elements which may appear in
multiple translation units (e.g., function _declarations_) and other
elements which must appear in exactly one translation units (e.g.,
non-inlined function _definitions_). The reason one commonly uses .h
and .cpp files is that the .h files are typically used for the elements
which may appear multiply, and the .cpp files are typically used for the
elements which must appear once.

It sounds like you may be including the definition of your function in
two separate compilations-- for main.cpp and for func.cpp-- and that is
not allowed (the linker should complain). Show us what the files look
like and we can provide more specific advice.

Mark
Apr 24 '07 #3
On Apr 24, 8:23 am, desktop <f...@sss.comwr ote:
I have a main.cpp file that will call a single function. For the sake of
curiousity I would like to place this function in a separate file called
func.cpp and include it in my main.cpp with: #include "func.cpp". But it
gives a multiple definitions error.
Thats because the linker finds the definition in func.o as well as
main.o. Remember ODR ?
Do I always have to make a .h file for a .cpp file if I want to include
the functionality in another module?
Not unless
1. You are linking func.o with main.o
2. You are linking main.o with any other file which also includes
func.cpp.

/P
Apr 24 '07 #4
On Apr 24, 6:00 pm, Lionel B <m...@privacy.n etwrote:
Not essential, but it's common practice to write function *definitions*
in .cpp files and function *declarations* in header files, which are then
included where appropriate. An exception might be for inline functions,
where it is conventional to write both declaration and definition in the
same (header) file.
I'd like to add some more to Lionel's words,
you can use #ifndef, #define, #endif for compiler to avoid any
accidental multiple inclusions of header files.

-
Sukumar R

Apr 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3385
by: aa | last post by:
Is it OK to include an ANSI file into a UTF-8 file?
0
2281
by: modemer | last post by:
Anybody knows if it is possible to use gdb/dbx to debug .pc file instead of generated .c file? Thanks
6
3428
by: Spartanicus | last post by:
For a non essential enhancement of one of my pages I'd like to include a remote html file via javascript (server side is not available to me). The "include" method that I'm familiar with: <script src="include.js" type="text/javascript"></script> with include.js containing: document.write("<table>")
10
4019
by: $kr1p7_k177y | last post by:
-Without using frames. -- .............................................................................. Chase after truth like hell and you'll free yourself, even though you never touch its coat-tails. -Clarence Darrow
8
207675
by: Henri | last post by:
Hi, Is there a way to include a .js file inside a .js file in JavaScript 1.5? Thanx Henri
7
374
by: Joona I Palaste | last post by:
Why is the standard C type for file handles FILE* instead of FILE? AFAIK the type FILE is a pre-defined typedef for some other type anyway. So why not make it instead a typedef for a *pointer* to that type? For one thing, that would stop people trying to poke around at the insides of a FILE. For another, it could allow for cases where the "real" type behind FILE is not a pointer to anything. -- /-- Joona Palaste (palaste@cc.helsinki.fi)...
1
1631
by: Bill | last post by:
How can I include a .cgi file in an .ASP file? I've tried #include, Server.Transfer, and Server.Execute (yes, I know the last two are only for ASP, but I tried it anyway). If I have to, I guess I'll use ASPHTTP to do an http call to the file but first I wanted to see if there was a more elegant way to do it...! Is there an efficient way to include the output of a .cgi file in an ASP file?
5
3482
by: green_tea | last post by:
Hi there, I'd like to know how to include a js file globally in asp.net so all my pages can access js functions in this file. (Only include it once and not include this file one by one to all my pages.) Thanks for your help.
4
6972
by: srikar | last post by:
hi all, I am having a small issue, I am not able to include the header file<std.h> and also <osfcn.h>, these are the headers used to interface C to C++ I am using the compiler version gcc-3,2,3 with libraray as glibc_s_so.1 The error i am getting is unable to include the header file. Is there is any header file which will do the same functionality as std.h & osfcn.h using gcc-3.2.3. help me to resove my issue
2
1555
by: Academia | last post by:
I want to include a button that opens a .chm help file on a usercontrol. I haven't yet looked at how the button is to open the help but before I go there I need to know how to include the .chm file with the usercontrol so that it will be available where ever the control is used. Does anyone know how to include the file? Thanks
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.