473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

extern "C"

200 100+
Hello everyone,


Just to confirm what is the most correct way beyond a just working function.

1.

We need to add extern "C" to both variable/function definition/declaration? Or only need to add to the variable/function declaration?

2.

How about extern? Declaration only or both declaration and definition are required?

BTW: previously, I only add to declaration, but after reading more and more code which add to both declaration and definition, I come to here to ask this question.


thanks in advance,
George
Feb 6 '08 #1
19 2443
weaknessforcats
9,208 Expert Mod 8TB
Do you know what extern "C" is for??
Feb 6 '08 #2
RedSon
5,000 Expert 4TB
Do you know what extern "C" is for??
I think we can all answer that for the OP...
Feb 6 '08 #3
George2
200 100+
Sure, weaknessforcats. Used for stopping mangling name for function and variable, any comments to my original question?


Do you know what extern "C" is for??

regards,
George
Feb 7 '08 #4
George2
200 100+
Thanks RedSon!


Any comments to my post #4?

I think we can all answer that for the OP...

regards,
George
Feb 7 '08 #5
RedSon
5,000 Expert 4TB
Sure, weaknessforcats. Used for stopping mangling name for function and variable, any comments to my original question?





regards,
George
Actually it is more complicated then that. It's true that "extern c" does prevent mangling but that is a side effect of c linking. What compiler are you using?
Feb 7 '08 #6
hdanw
61
Use the extern keyword so that they can be linked outside of its normal frame of reference.

The "c" tells the compiler how to push information on the stack.

The keyword should be added to the Definition of functions, so they can be called by other files.

And declaring extern on a declaration means that the actual declaration exists outside of this file. And should be visible to all linked object files.

Expand|Select|Wrap|Line Numbers
  1. // myfile.cpp
  2. int globalint
  3.  
  4. extern myfunc()
  5. {
  6.   // do some stuff
  7. }
  8.  
Expand|Select|Wrap|Line Numbers
  1. // myfile.hpp
  2.  
  3. extern int globalint;
  4. extern myfunc();
  5.  
Expand|Select|Wrap|Line Numbers
  1. // other.cpp
  2.  
  3. #include "myfile.hpp"
  4.  
  5. globalint = 10;
  6. myfunc();
  7.  
  8.  
Feb 8 '08 #7
George2
200 100+
Thanks RedSon,


I am using Visual Studio 2008 to develop C/C++. Any advice about whether to add extern "C" to declaration only or both declaration and definition?

Actually it is more complicated then that. It's true that "extern c" does prevent mangling but that is a side effect of c linking. What compiler are you using?

regards,
George
Feb 8 '08 #8
George2
200 100+
Thanks hdanw,


Please review whether I am correct.

1. We should add extern to declaration, so what we know we are referring a function/variale from other compile unit;
2. We should also add extern to definition so that this function/variale is visible to other compile unit. In this case, extern is on the other side of static.

Right?


regards,
George

Use the extern keyword so that they can be linked outside of its normal frame of reference.

The "c" tells the compiler how to push information on the stack.

The keyword should be added to the Definition of functions, so they can be called by other files.

And declaring extern on a declaration means that the actual declaration exists outside of this file. And should be visible to all linked object files.

Expand|Select|Wrap|Line Numbers
  1. // myfile.cpp
  2. int globalint
  3.  
  4. extern myfunc()
  5. {
  6.   // do some stuff
  7. }
  8.  
Expand|Select|Wrap|Line Numbers
  1. // myfile.hpp
  2.  
  3. extern int globalint;
  4. extern myfunc();
  5.  
Expand|Select|Wrap|Line Numbers
  1. // other.cpp
  2.  
  3. #include "myfile.hpp"
  4.  
  5. globalint = 10;
  6. myfunc();
  7.  
  8.  
Feb 8 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
I am using Visual Studio 2008 to develop C/C++. Any advice about whether to add extern "C" to declaration only or both declaration and definition?
I just did a Google on "extern "C" " and got 309,000 hits.

Maybe you could read a few of these pages. What you are asking is covered in any C++ reference material.

You are supposed to post questions when you are stuck in the code and not as a substitute for doing your own research.

Next:

All function prototypes and definitions have a default storage class specifier of extern. That is, the functions have external linkage. This means you do not need to specify extern. You can override the default with static. However, using the "C" qualifier will require the extern spcifier.
Feb 8 '08 #10
RedSon
5,000 Expert 4TB
Educate yourself ...
Feb 8 '08 #11
George2
200 100+
Thanks weaknessforcats,


I think we should add extern "C" to both declaration and definition, right?

http://www.glenmccl.com/ansi028.htm

I just did a Google on "extern "C" " and got 309,000 hits.

Maybe you could read a few of these pages. What you are asking is covered in any C++ reference material.

You are supposed to post questions when you are stuck in the code and not as a substitute for doing your own research.

Next:

All function prototypes and definitions have a default storage class specifier of extern. That is, the functions have external linkage. This means you do not need to specify extern. You can override the default with static. However, using the "C" qualifier will require the extern spcifier.

regards,
George
Feb 9 '08 #12
George2
200 100+
Hi RedSon,


I do not think the same in the MSDN page is good enough. When demonstrating extern, it should use more than one cpp files (more than one compile unit) right?

Expand|Select|Wrap|Line Numbers
  1. // specifying_linkage1.cpp
  2. int i = 1;
  3. void other();
  4.  
  5. int main() {
  6.    // Reference to i, defined above:
  7.    extern int i;
  8. }
  9.  
  10. void other() {
  11.    // Address of global i assigned to pointer variable:
  12.    static int *external_i = &i;
  13.  
  14.    // i will be redefined; global i no longer visible:
  15.    // int i = 16;
  16. }
  17.  
Educate yourself ...

regards,
George
Feb 9 '08 #13
RedSon
5,000 Expert 4TB
Hi RedSon,


I do not think the same in the MSDN page is good enough. When demonstrating extern, it should use more than one cpp files (more than one compile unit) right?

regards,
George
The point of providing the link was to give you a starting point into your investigation into the extern keyword. Did you follow any of the links in the see also section?
Feb 10 '08 #14
George2
200 100+
Sure, RedSon.


Any comments to my post #13? I think to demonstrate the usage of extern, we should use more than one compile unit (cpp file) to demonstrate, right?

The point of providing the link was to give you a starting point into your investigation into the extern keyword. Did you follow any of the links in the see also section?

regards,
George
Feb 10 '08 #15
oler1s
671 Expert 512MB
I think to demonstrate the usage of extern, we should use more than one compile unit (cpp file) to demonstrate, right?
Not necessary, if you understand the concepts of linkage. Of course, the assumption is that you do your reading and actually understand the background material. Otherwise, you have to rely on unnecessarily complicated demonstrations to cover up such lack of knowledge.
Feb 10 '08 #16
George2
200 100+
Hi oler1s,


Why it is needed to use extern even if we are in the same cpp file? Any more description please?

(I think extern is useful for cross-compile unit cross-reference usage, if only in one compile unit, is it very helpful?)

Not necessary, if you understand the concepts of linkage. Of course, the assumption is that you do your reading and actually understand the background material. Otherwise, you have to rely on unnecessarily complicated demonstrations to cover up such lack of knowledge.

regards,
George
Feb 11 '08 #17
oler1s
671 Expert 512MB
Why it is needed to use extern even if we are in the same cpp file?
What do you mean by that question? You use extern to specify external linkage. That is you are simply the declaring the existence of something, and indicating that the actual definition is in some other source file or library. You use extern if that's what you need to indicate.

(I think extern is useful for cross-compile unit cross-reference usage, if only in one compile unit, is it very helpful?
Again, let me repeat myself. If you need something to have external linkage, it should either implicity or explicitly be declared as extern. For example, you don't indicate extern with function declarations, because it is implicitly implied. You do with variables, because you need to say so explicitly. IF something doesn't need external linkage, you don't need to use the extern keyword. Just because the extern keyword exists doesn't mean you need to use it, you realize?

But why are you having fundamental issues understanding it, when you declare your understanding on Cboard, continue with a second thread at CodeGuru, and yet another on a Usenet group?
Feb 11 '08 #18
George2
200 100+
Hi oler1s,


Are there any practical benefits if we use extern keyword to qualify a variable defined in the same cpp file? This is my question.

What do you mean by that question? You use extern to specify external linkage. That is you are simply the declaring the existence of something, and indicating that the actual definition is in some other source file or library. You use extern if that's what you need to indicate.

Again, let me repeat myself. If you need something to have external linkage, it should either implicity or explicitly be declared as extern. For example, you don't indicate extern with function declarations, because it is implicitly implied. You do with variables, because you need to say so explicitly. IF something doesn't need external linkage, you don't need to use the extern keyword. Just because the extern keyword exists doesn't mean you need to use it, you realize?

But why are you having fundamental issues understanding it, when you declare your understanding on Cboard, continue with a second thread at CodeGuru, and yet another on a Usenet group?

regards,
George
Feb 12 '08 #19
RedSon
5,000 Expert 4TB
Hi oler1s,

Are there any practical benefits if we use extern keyword to qualify a variable defined in the same cpp file? This is my question.

regards,
George
If you took the time to read about what extern means and try to learn from the text. Then ask questions about the text, for example say, "I don't understand what this text means when it says "blah, blah blah blah, blah", we might be more willing to help you.

You continue to ask the same question that would have been answered if you would have investigated more.

But in answer to your question, it depends on the scope of your variable.
Feb 12 '08 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
111
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1)...
8
by: Generic Usenet Account | last post by:
Our C++ program was linked with some legacy C functions. We had used the extern "C" declaration, and everything was working fine. Then someone thought that it would be better to have a consistent...
1
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
10
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: //...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
4
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a...
4
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.