user3631987
user3631987

Reputation: 19

Undefined reference for a C++ function when called from C function

I am trying to call a C++ function from a C function and i see a undefined reference to a function written .cc file, Below is the code. What am i missing?

externcpp.cc

#include <iostream>
 #include "example.h"
 using namespace std;
 int main ()
 {
    cout << "I am " << __func__ << "In File " << __FILE__;
    return 0;
 }
 void example_fun()
 {
    cout << "I am" << __func__ << "in File __FILE__";
 }

externc.c

#include <stdio.h>
#include "example.h"
int test1()
{
    printf(" I am [%s] and from File [%s]\n",__func__,__FILE__);
    printf("Calling C++ Function from C\n");
    example_fun();
    return 0;
}

example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

#ifdef __cpluscplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

EXTERNC void example_fun();
#endif

And used following commands for compilation and linking

g++ -c -o externcpp.o externcpp.cc -Wall
gcc -c -o externc.o externc.c -Wall
g++ -o output externcpp.o externc.o

Regards,

Upvotes: 0

Views: 171

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320411

It's supposed to be #ifdef __cplusplus, not #ifdef __cpluscplus as in your code above. Check your spelling.

Upvotes: 9

Related Questions