0

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,

8
  • Please add the error message. Commented Jan 8, 2015 at 6:58
  • Why is there a trailing { on your #define EXTERNC? It seems to never be closed. Commented Jan 8, 2015 at 6:59
  • externc.o: In function 'test1' externc.c: (.text+0x2d) : undefined reference to 'example_fun' collect2: error: ld returned 1 exit status Commented Jan 8, 2015 at 7:03
  • Di you really mean C+C+? Commented Jan 8, 2015 at 7:06
  • Typo questions are off-topic on Stack Overflow. Commented Jan 8, 2015 at 7:17

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ew, those are errors you can stare at for hours without seeing them. Good catch!
Yes that was the error after making that change i got the required result thanks to all. Thanks @AndreyT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.