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

Home Posts Topics Members FAQ

extern "C"

200 New Member
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 2489
weaknessforcats
9,208 Recognized Expert Moderator Expert
Do you know what extern "C" is for??
Feb 6 '08 #2
RedSon
5,000 Recognized Expert Expert
Do you know what extern "C" is for??
I think we can all answer that for the OP...
Feb 6 '08 #3
George2
200 New Member
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 New Member
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 Recognized Expert Expert
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 New Member
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 New Member
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 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

4
4260
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 6.0-7.1).. Note, that basically I don't need an "extern C" linkage, I just need to define a type to cast a resolved function address.. Any ideas? Basically , I need something like this..
1
2652
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
6474
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) The whole "function declaration Vs object definition" fiasco, which results in the following (temporary creating) syntax: Blah poo = Blah();
8
2185
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 file naming convention and renamed the files with the legacy C functions from .c to .cxx. Lo and behold we started getting linker errors left and right. When we reverted back to the old naming convention, the problem went away. Also, the...
1
1467
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
10
6202
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*));
12
2730
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 linkage? Not much on this in the FAQ or tutorials.
19
3867
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 storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
4
4761
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 global function with the extern "C" prefix. I was leaning toward using the static method approach until I saw a posting that said compatibility between static C++ functions and C is not guaranteed in the respective language standards. This made me...
4
6260
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 functions written in the "SomeLanguage". But I am quite confused what information does the linkage directive tells the compiler.The "generated" function name? The way the arguments are ordered?Or something else? And I am still wondering why extern...
0
9708
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
9588
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
10589
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
10340
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
10327
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
5527
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...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
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.