I am currently taking a course and one of the example programs showed
a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if the same
function prototype is used in more than one source file in a project,
that the linker will complain.
He explained to me that this was exactly the same as when you have a
global variable scoped in several source files, that where ever the
global variable is defined it does not need to be explicitly defined
as extern, but that in any other files that it needs to be in scope,
the global variable must be declared extern. This part I know to be
true from my own experience. It seemed to make sense, so I believed
him.
When I got back home I tried to verify what was stated above about
extern and function prototypes, but try as I might, using Visual
Studio Express 8, I could not generate any linking errors by leaving
out the extern keyword in function prototypes. BTW, I tried with both
a .c project (all the source files were .c files and I set the
compiler option to "Compile as C code" - the /TC option) and a .cpp
project (all the source files were .cpp files).
Can anyone give me an example where the extern keyword is actually
required? Or is Microsoft up to it's old tricks and "helping me"
without my knowledge and I would have had linking errors on other
IDEs ?
Bob 7 3537
* blangela:
I am currently taking a course and one of the example programs showed
a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if the same
function prototype is used in more than one source file in a project,
that the linker will complain.
He explained to me that this was exactly the same as when you have a
global variable scoped in several source files, that where ever the
global variable is defined it does not need to be explicitly defined
as extern, but that in any other files that it needs to be in scope,
the global variable must be declared extern. This part I know to be
true from my own experience. It seemed to make sense, so I believed
him.
When I got back home I tried to verify what was stated above about
extern and function prototypes, but try as I might, using Visual
Studio Express 8, I could not generate any linking errors by leaving
out the extern keyword in function prototypes. BTW, I tried with both
a .c project (all the source files were .c files and I set the
compiler option to "Compile as C code" - the /TC option) and a .cpp
project (all the source files were .cpp files).
Can anyone give me an example where the extern keyword is actually
required?
'extern' is the default for C++ function declarations, so ordinarily you do not
need it.
You need it only where it's not the default, and/or where you want to specify
"C" linkage.
An example where it's not default: the default for constants is internal
linkage, so if you declare a constant in a header file and define it in an
implementation file you need to use 'extern', like
<code file="x.h">
extern double const pi;
</code>
<code file="x.cpp">
#include "x.h"
double const pi = 3.14;
</code>
Or is Microsoft up to it's old tricks and "helping me"
without my knowledge and I would have had linking errors on other
IDEs ?
You mean compilers.
An IDE is not a compiler: it just gives you easy access to a compiler, and other
relevant tools.
Cheers, & hth.,
- Alf
On Apr 16, 11:00*pm, "Alf P. Steinbach" <al...@start.no wrote:
* blangela:
I am currently taking a course and one of the example programs showed
a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if the same
function prototype is used in more than one source file in a project,
that the linker will complain.
He explained to me that this was exactly the same as when you have a
global variable scoped in several source files, that where ever the
global variable is defined it does not need to be explicitly defined
as extern, but that in any other files that it needs to be in scope,
the global variable must be declared extern. *This part I know to be
true from my own experience. It seemed to make sense, so I believed
him.
When I got back home I tried to verify what was stated above about
extern and function prototypes, but try as I might, using Visual
Studio Express 8, I could not generate any linking errors by leaving
out the extern keyword in function prototypes. BTW, I tried with both
a .c project (all the source files were .c files and I set the
compiler option to "Compile as C code" - the */TC option) and a .cpp
project (all the source files were .cpp files).
Can anyone give me an example where the extern keyword is actually
required?
'extern' is the default for C++ function declarations, so ordinarily you do not
need it.
You need it only where it's not the default, and/or where you want to specify
"C" linkage.
An example where it's not default: the default for constants is internal
linkage, so if you declare a constant in a header file and define it in an
implementation file you need to use 'extern', like
<code file="x.h">
extern double const pi;
</code>
<code file="x.cpp">
#include "x.h"
double const pi = 3.14;
</code>
Thanks for your reply. If I undertand you correctly, the use of
extern as I explained it in my question is _NEVER_ necessary in a C++
application?
What about in a C application? Is the same answer true?
*Or is Microsoft up to it's old tricks and "helping me"
without my knowledge and I would have had linking errors on other
IDEs ?
You mean compilers.
An IDE is not a compiler: it just gives you easy access to a compiler, andother
relevant tools.
To be honest, the reason I used IDE, was as a short cut for compiler
and linker, since I am not sure if it is the MS compiler or MS linker
that is the problem. Does that make sense?
Thanks again for your reply!
On Apr 17, 7:26 am, blangela <Bob_Langel...@ telus.netwrote:
I am currently taking a course and one of the example programs
showed a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if
the same function prototype is used in more than one source
file in a project, that the linker will complain.
That's not strictly true. A function declaration that is not a
definition is implicitly "extern".
In theory, at least, it's probably better to be consistent, and
to use it. But there's so much code around that doesn't that
most programmers are used to not seeing it, so it isn't that
important. (Given the inconsistent rules concerning linkage in
C++, one could argue that you should always specify it. But of
course, people---myself included---don't.)
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
* James Kanze:
On Apr 17, 7:26 am, blangela <Bob_Langel...@ telus.netwrote:
>I am currently taking a course and one of the example programs showed a function prototype something like:
>extern void func();
>I asked why the "extern" was necessary.
>Someone in the class (not the instructor) explained that if the same function prototype is used in more than one source file in a project, that the linker will complain.
That's not strictly true. A function declaration that is not a
definition is implicitly "extern".
A function declaration that is a definition is also "extern" by default.
I think you meant "by default", not "implicitly ".
All of the above with the understanding that we're not talking about member
functions in a local class, because a local class doesn't formally have linkage
as of the current standard, and of course also that there hasn't been some
previous declaration of the function, which would have established the linkage.
Cheers,
- Alf (nit-picking, but I think the "that is not a" could be misleading)
On 17 Apr, 07:16, blangela <Bob_Langel...@ telus.netwrote:
On Apr 16, 11:00*pm, "Alf P. Steinbach" <al...@start.no wrote:
* blangela:
I am currently taking a course and one of the example programs showed
a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
<snip>
'extern' is the default for C++ function declarations, so ordinarily youdo not
need it.
<snip>
Thanks for your reply. *If I undertand you correctly, the use of
extern as I explained it in my question is _NEVER_ necessary in a C++
application?
What about in a C application? *Is the same answer true?
yes
<snip>
--
Nick Keighley
On Apr 17, 1:09*am, James Kanze <james.ka...@gm ail.comwrote:
On Apr 17, 7:26 am, blangela <Bob_Langel...@ telus.netwrote:
I am currently taking a course and one of the example programs
showed a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if
the same function prototype is used in more than one source
file in a project, that the linker will complain.
That's not strictly true. *A function declaration that is not a
definition is implicitly "extern".
In theory, at least, it's probably better to be consistent, and
to use it. *But there's so much code around that doesn't that
most programmers are used to not seeing it, so it isn't that
important. *(Given the inconsistent rules concerning linkage in
C++, one could argue that you should always specify it. *But of
course, people---myself included---don't.)
Does that mean that some compilers do need the explicit use of extern
for function prototypes?
Bob
On 17 avr, 10:33, "Alf P. Steinbach" <al...@start.no wrote:
* James Kanze:
On Apr 17, 7:26 am, blangela <Bob_Langel...@ telus.netwrote:
I am currently taking a course and one of the example programs
showed a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if
the same function prototype is used in more than one source
file in a project, that the linker will complain.
That's not strictly true. A function declaration that is not a
definition is implicitly "extern".
A function declaration that is a definition is also "extern"
by default.
So it is. I don't know what I was thinking of. (Actually, I do
know what I was thinking of: of the old days, when you said
"global" to export a symbol, and "extern" to import it. But
that's not really relevant to C++, given as how it was
assembler, and pre-dated even C.)
I think you meant "by default", not "implicitly ".
I think the two terms mean more or less the same thing in this
context. But I'm not really sure. (Not getting enough rest
these days to be sure of anything, really.)
All of the above with the understanding that we're not talking
about member functions in a local class, because a local class
doesn't formally have linkage as of the current standard, and
of course also that there hasn't been some previous
declaration of the function, which would have established the
linkage.
Yes. I took it for granted that we were really only talking
about functions at namespace scope. You can't apply extern to a
member function, since its visibility is that of the class
(which as you say, is also "extern", except for local classes).
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mark A. Gibbs |
last post by:
I have a question about mixing C and C++.
In a C++ translation unit, I want to define a function with internal
linkage and C calling convention. Here's a sample of what I want to do:
// main.cpp
// This is defined in a C module
extern "C" void fake_qsort(void*, std::size_t, std::size_t,
int (*compare)(const void*, const void*));
|
by: tweak |
last post by:
What's the best way to use extern when using multiplefiles that is
easiest to maintain?
Is it best to declare:
extern int a;
in a header file and include the header file in all files except
where it's defined.
|
by: siliconwafer |
last post by:
Hi all,
I wanted to know that is use of extern keyword mandatory in case of
global variables and functions used in other source files?
i.e consider a following piece of code from MSDN explaining extern
storage class:
/******************************************************************
SOURCE FILE ONE
*******************************************************************/
extern int i; /* Reference to i, defined below */
|
by: Tapeesh |
last post by:
I would like to know what is the expected behaviour of C compilers when
an extern decleration is intialized.
When the following code is compiled using gcc
//File extern.c
int arr ;
int a ;
|
by: pauldepstein |
last post by:
#include <iostream>
using namespace std;
int main()
{
extern "C" int f(int, int);
| |
by: djhong |
last post by:
Hi,
BSD code has following hcreate function definition in hsearch.c
under lib/libc/db/hash/hsearch.c.
Here, why is 'extern' used ? What's the purpose of it? How come
in infront of functions definition ? ( Usually 'extern' is placed in
front of function declaration....)
extern int
hcreate(nel)
|
by: quarkLore |
last post by:
As this link points it out one can use extern instead of a function
prototype. Are they one and the same in terms of standard,
maintainability?
Has some one seen an advantage of one over the other?
When should one use simple prototype and when should one use an
extern?
http://www.eskimo.com/~scs/cclass/notes/sx5b.html
|
by: polas |
last post by:
I have a quick question - I have used extern quite a bit in relation
to variables, however was wondering if its applicable to function
prototypes too?
For instance, I have a file called a.c - a small snapshot of it
contains
extern int a;
void hello(int);
|
by: Lew Pitcher |
last post by:
On November 14, 2008 15:00, in comp.lang.c, Nomen Nescio (nobody@dizum.com)
wrote:
Overkill
Try
|
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...
|
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,...
| |
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |