c / c ++ mixed

/* head.h */
#ifndef __SUM_H__ #define __SUM_H__ #ifdef __cplusplus extern "C" { #endif int add(int a, int b); int sub(int a, int b); #ifdef __cplusplus } #endif #endif /* __SUM_H__ */

sum.cpp

#include <iostream>

#include "head.h"

using namespace std;

int main ()
{
    int a, b;
    cout << " Enter two numbers: " ;
    
    cin >> a >> b;

    cout << "相加:" << add(a, b) << endl;
    cout << "相减:" << sub(a, b) << endl;
}

test.c

#include <stdio.h>
#include "head.h"

int add(int a, int b)
{
	return a+b;
}
int sub(int a, int b)
{
	return a-b;
}

 Compile the test: g ++ sum.cpp test.c

Or c .o file compiled file: gcc -c test.c generated test.o then: g ++ sum.cpp test.o

Guess you like

Origin www.cnblogs.com/porkerface/p/12088032.html