0

A very simple question. I was trying to call a c++ function from a c file. I always seem to get "undefined reference to the function" error.

C file: test.c

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


int main(){   
    print();
    return 0;
}

C++ file: print.cpp

#include <iostream>
#include "link.h"

extern "C"
void print(){
   std::cout<<"hello from cpp";
} 

Header file: link.h

 #ifndef HEADER_FILE
 #define HEADER_FILE

 #ifdef __cplusplus
     extern "C" {
 #endif
         void print();
 #ifdef __cplusplus
     }
 #endif

 #endif

I don't know what's wrong with the above code. Also, how do I compile the above in terminal? I'm assuming I need to use the gcc compiler (gcc -o test test.c link.h)

Any inputs will be highly appreciated. Thanks,

0 Answers0