473,796 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Including source

Regarding either of the following:

1. Including a .c file with #include,
2. Including source code contained in a .h header file with #include

I have 2 questions.

1. Is the reality of most programmers not doing this because of
popular habit, or is it officially deprecated in one of the C
standard(s)?
2. What are the practical virtues of not doing it? I can think of
reasons *to* do it.

Oct 21 '07 #1
8 1648
sq************@ hotmail.co.uk wrote:
Regarding either of the following:

1. Including a .c file with #include,
2. Including source code contained in a .h header file with #include

I have 2 questions.

1. Is the reality of most programmers not doing this because of
popular habit, or is it officially deprecated in one of the C
standard(s)?
Headers usually get included into multiple translation units. Such
multiple inclusion of a .c file is likely to be an error.
2. What are the practical virtues of not doing it?
If you find yourself needing to include code sequences into multiple
files, you might consider multiple modules and linking. It greatly
reduces chances for conflict and code size. Any errors can be corrected
in one place instead of in umpteen files.
I can think of reasons *to* do it.
Okay, what are they?

Oct 21 '07 #2
On 21 Oct, 19:54, santosh <santosh....@gm ail.comwrote:
I can think of reasons *to* do it.

Okay, what are they?
I was speaking from presumption; I'd never actually gone and tried
it. I was thinking that including a conditional to check if the file
had already been included would work, but on reflection it obviously
wouldn't. I was thinking that it would be more modular and easy to
deal with if a "library" could be included as one file, without having
to also think about linking to anything.

Oct 21 '07 #3
sq************@ hotmail.co.uk wrote:
On 21 Oct, 19:54, santosh <santosh....@gm ail.comwrote:
I can think of reasons *to* do it.

Okay, what are they?

I was speaking from presumption; I'd never actually gone and tried
it. I was thinking that including a conditional to check if the file
had already been included would work, but on reflection it obviously
wouldn't.
It would work. That's exactly the mechanism that headers use. However
then you've defeated your own purpose.
I was thinking that it would be more modular and easy to
deal with if a "library" could be included as one file, without having
to also think about linking to anything.
A library by definition is called from several files and several
programs. You usually _don't_ want to include a fresh copy of a
function in each module that uses it. That's why multiple modules and
linking were developed.

The whole issue proceeds from the observation that, in C at least, you
generally need a separate declaration in each place it's needed, but
usually one definition of an object or function suffices. In fact for
external objects or functions, you cant have more than one active
definition.

A prominent exception to this observation are macros.

Oct 21 '07 #4
sq************@ hotmail.co.uk wrote:
Regarding either of the following:

1. Including a .c file with #include,
2. Including source code contained in a .h header file with #include

I have 2 questions.

1. Is the reality of most programmers not doing this because of
popular habit, or is it officially deprecated in one of the C
standard(s)?
The way we use a function multiple places, is by placing the prototype
of the function in a header file, e.g. like this:

extern void function(void);

If you rather include the source of the function multiple places, this
leads to bloat, slower compile and possibly hard to maintain code.

2. What are the practical virtues of not doing it? I can think of
reasons *to* do it.
In 20 years of C programming, I have never needed or wanted to do it. :)

The only reason I can think of right now, is if I somehow wanted to
avoid using a function pointer as a parameter to an interface, for
tuning purposes. It sound like a bad idea...

--
Tor <torust [at] online [dot] no>

"Technical skill is mastery of complexity, while creativity is mastery
of simplicity"
Oct 21 '07 #5
santosh <sa*********@gm ail.comwrote:
sq************@ hotmail.co.uk wrote:
1. Including a .c file with #include,
[ ... ]
I can think of reasons *to* do it.
Okay, what are they?
I'm not the OP but I found it useful to #include source files when
compiling unit tests. It's a bit idiosyncratic and is not done
when compiling the production code.

So it works, there is no prohibition against it, but it's not
normal.
--
pa at panix dot com
Oct 21 '07 #6
sq************@ hotmail.co.uk writes:
1. Including a .c file with #include,
[...]
2. What are the practical virtues of not doing it? I can think of
reasons *to* do it.
One good reason is a social one: programmers do not expect .c
files to be #included, so if you #include .c files, you will
surprise other programmers reading your code, making it more
difficult for them to maintain and enhance it.
--
Ben Pfaff
http://benpfaff.org
Oct 21 '07 #7
sq************@ hotmail.co.uk wrote:
Regarding either of the following:

1. Including a .c file with #include,
2. Including source code contained in a .h header file with #include

I have 2 questions.

1. Is the reality of most programmers not doing this because of
popular habit, or is it officially deprecated in one of the C
standard(s)?
2. What are the practical virtues of not doing it? I can think of
reasons *to* do it.
I've #included *.c files, but only for one specialized use: creating
wrappers for existing functions that modify the normal behavior of those
functions, and need to be link compatible with code that calls those
functions by name. I usually use this when creating unit test drivers.

wrapper.c:

#define origfunction myfunction
// file.c defines origfunction(). With the above #define, it actually
// ends up defining myfunction()
#include "file.c"
#unddef origfunction

long origfunction(in t a, double b, char *c)
{
if(special_hand ling)
{
// implement special handling
}
else
{
return myfunction(a, b, c);
}
}
I prefer this over creating a modified copy of file.c, because I don't
need to make changes to wrapper.c, just because file.c changes, unless
the interface to origfunction() changes. This is particularly helpful
when origfunction() is part of a 3rd party library delivered as source
code, which describes all of the 3rd party libraries we link to in my group.
Oct 22 '07 #8
On Oct 21, 9:36 pm, santosh <santosh....@gm ail.comwrote:
squaretrian...@ hotmail.co.uk wrote:
I was thinking that it would be more modular and easy to
deal with if a "library" could be included as one file, without having
to also think about linking to anything.

A library by definition is called from several files and several
programs. You usually _don't_ want to include a fresh copy of a
function in each module that uses it. That's why multiple modules and
linking were developed.
I do use it (but very rarely) for libraries. I have a library that
interfaces to a device driver, and can be used on several platforms
with different drivers for the same device. Most of the code is
generic, but part of it is platform dependent. I only want the defined
interface to be visible outside of the library, and not my internal
functions, so I want to define these functions as static.
I have four options to do this AFAIK:
I can create a number of copies of the generic code for each of the
platforms, which adds to the maintenance.
Or I can add the code for all the platform to the original file, and
use #ifdef to select the code I need for a platform.
Or I can create a source-file per platform that is included for a
specific platform only, also using #ifdef
Or do not define the functions as static.

We are using the third option, as this option is easier to maintain
than the first 2. The last one contradicts the "for internal use only"
rule for these functions, so this is a no-op.

Kind regards,
Johan Borkhuis

Oct 22 '07 #9

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

Similar topics

1
1330
by: Andres Baravalle | last post by:
Hi, I am quite new with python and I'm developing a package that is including a binary file (eggtrayiconmodule.so) that is open source. My software is developed in part under linux, and now I would like to make it fully portable to other unix-like platforms. Therefore I need to provide the source and integrate eggtrayicon compiling in my setup.py. At this point I don't know how go on.
11
1944
by: cppaddict | last post by:
Say that your CustomClass.h header files requires #include <string> Now say that your CustomClass.cpp file also requires string. Is it good form to repeat the <string> include to make the dependency explicit, or do just allow the include to be make implicitly through the .h include? That is, should the header of your .cpp file be: #include "CustomClass.h"
31
3610
by: Joseph Wakeling | last post by:
Hello all, I'm writing some programs that will be using modules from the GNU Scientific Library (GSL). Include commands within the GSL modules have commands such as, #include <gsl/gsl_rng.h> (for example). Yet these header files aren't actually in the gsl
8
3714
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box and save it to a file. This step is not to hard however the contents of the textarea is mostly latex source so it contains just about every special character you can imagine. My question is this, how do I save an exact copy of the textarea...
4
1519
by: nospam4314 | last post by:
I am trying to include a C header file in C++, but the C header has some datastructures where some variable names are "namespace" and "this". Obviously C++ complains about the use of these reserved words. I was wondering if there is a way around it besides changing the source code (mainly because I don't have access to the source code, so changing the header files will only create more trouble). I tried extern "C", but it didn't work. ...
13
2258
by: sachin | last post by:
Hi, Is it possible to do something like this: unsigned char arr = { #include "cFile.c" } I need that C source file cFile.c to compile and its binary output to include in array.
3
2576
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c files). I know that there are two ways of specifying include path of header files
8
1688
by: J Kenneth King | last post by:
Hey everyone, I'm working on a python extension wrapper around Rob Hess' implementation of a SIFT feature detector. I'm working on a computer-vision based project that requires interfacing with Python at the higher layers, so I figured the best way to handle this would be in C (since my initial implementation in python was ungodly and slow). I can get distutils to compile the extension and install it in the python path, but when I go...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
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...
1
10173
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
10006
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
6788
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();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
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
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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.