C C + + calls issues with each other

Source: Internet
Author: User

1. C + + calls the C. language function

C Language Header file C_header.h

#ifndef __c_header_h_#define __c_header_h_#ifdef __cplusplusextern "C" {#endifint c_add (int a, int b); void C_print_data ( int x); #ifdef __cplusplus} #endif #endif

C Language Implementation file c_realize.c

#include <stdio.h> #include "c_header.h" int c_add (int a, int b) {return a+b;} void C_print_data (int x) {printf ("int c_print_data, X is:%d\n", x);}

C + + Test file Cpp_test.cpp

#include <iostream> #include "c_header.h" using namespace Std;void main () {int A=23;int b=32;c_add (A, b); C_print_ Data (a);}


2. C language calls C + + code

C + + header file Cpp_header.h

#ifndef _cpp_header_h_#define _cpp_header_h_int cpp_add (int a, int b); void cpp_print_data (int x); #endif

C + + implementation file Cpp_realize.cpp

#include <iostream> #include "c_header.h" using namespace std; #ifdef __cplusplusextern "C" {#endifint cpp_add (int a , int b) {return a+b;} void Cpp_print_data (int x) {cout<< "int the Cpp_print_data, X is:" <<X<<ENDL;} #ifdef __cplusplus} #endif

C Language test file c_test.c

#include <stdio.h> #include "cpp_header.h" void Main () {int a=4;int b=23;printf ("result is:%d\n", Cpp_add (A, b)); Cpp_print_data (a);}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C C + + calls issues with each other

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.