C/C ++ mixed programming -- extern "C" Usage

Source: Internet
Author: User

2012-01-03 wcdj

Question:

What's the theory behind this: # ifdef _ cplusplus extern "C "{
I have been meeting the following in some C/C ++ codes but I don't understand the theory behind it:
 
# Ifdef _ cplusplus
Extern "C "{
# Endif
 
I want to understand how it works. I know about the preprocessing but I don't know what that extern "c" in the Code does. someone teach me please!

ANSWERS 1:

The C ++ compiler does something called name mangling which facilitates function overloading.
The extern "C" syntax tell the C ++ compiler not to perform function overloading.
The code snippet that you posted first checks if the code being compiled is using a C ++ compiler and if so starts a block in which name mangling will not be done.

Answer 2:

That typically encloses DLL's exported functions.
You shoshould know, C language and C ++ one use different approach to function's name mangling (I. e. altering the name of The funtion in object-or library-files), more specifically, C ++ provided des In the mangled (or decorated) Name info about the Function argument
Types (this allows, function overloads, a C ++ feature, not available in C language ).
The mechanic allows the same header to be used by both the C and C ++ Compiler (_ cpluspls macro is defined only by the C ++ one) and works this way:
The C compiler, ignores the extern "C" (_ cplusplus is not defined) directive both when building the DLL and when including the DLL header inside an application.
The C ++ compiler, according with the extern "C" (_ cplusplus is defined) Directive:
(1) produces a standard c dll (I. e. the function use the C language mangling Scheme) when building the DLL.
(2) consider the library as a c dll when the header is encoded in a application (You know C ++ compiler is able to link with C libraries ).
Hope it helps.

When calling the functions implemented in the C file in the CPP file (if CPP is used to call C, C can also call CPP), use extern "c" to declare the function, otherwise, CPP will find the function according to the name modified function name and cannot find it.

An example of a function that calls C in C ++:

// cFile.h#ifndef _C_FILE_H_#define _C_FILE_H_extern int add(int x, int y);#endif// cFile.c#include "cFile.h"int add(int x, int y){return x+y;}// cppFile.h#ifndef _CPP_FILE_H_#define _CPP_FILE_H_#ifdef __cplusplusextern "C"{ #endif#include "cFile.h"#ifdef __cplusplus}#endif#endif/* _CPP_FILE_H_ */// cppFile.cpp#include "cppFile.h"#include <stdio.h>/*extern "C"{#include "cFile.h"}*/int main(int argc, char* argv[]){printf("add = %d\n", add(2, 3));return 0;}

Then compile the above Code:

# Makefile, wcdjCC = gccCXX = g++CFLAGS= -g -Wall -ansiINCLUDE = . LIBPATH = PUBLIBS = LIBS = $(PUBLIBS)BIN_SERVER = myappOBJS_SERVER = cppFile.o cFile.oBINS = $(BIN_SERVER)OBJS = $(OBJS_SERVER)all: $(BIN_SERVER) $(BIN_SERVER):$(OBJS_SERVER)$(CXX) -o $@ $^ $(LIBPATH) $(LIBS)install:@echo "nothing to install"clean:@-rm -f $(OBJS) $(BINS)@echo "clean over"

Make
./MyApp
In C ++, you can successfully call the function defined in C.

Refer:

[1] for instructions on _ cplusplus, refer to: C ++ 2003 standard, 16.8 predefined macro names
[2] for instructions on extern "C", refer to: C ++ 2003 standard, 7.5 linkage specifications
[2] Q &

Http://www.codeproject.com/Messages/3327432/Whats-the-theory-behind-this-sharpifdef-__cplusplu.aspx

[3] extern "C" function http://blog.csdn.net/weiqubo/article/details/4681813

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.