Problem in file including.
Hi
I have problem in including file in a class. I have a fuction declared in .h file and define in .c file when i am trying to call it in my .cpp file then it gives error
error: undefined reference to `packet_formation(unsigned char, unsigned char)'
while i am including its header file.
But when by mistake i include its .c file then it is running accurately.
What is the problem i am not understanding, can we include .c(source file) , is it not unlogical including a source file while the header file has the declaration of function.
Please help me to understand this.
Re: Problem in file including.
Is this function declared as extern in header ?
Is the .c file with function definition compiled and .o linked ?
Re: Problem in file including.
no it is not declared with "extern".
Re: Problem in file including.
Ok, if I remember correctly "extern" is not required for functions (only for variables existing in another file), so forget about it.
What about my second question ?
Re: Problem in file including.
ya .c file compiled succesfully.
Re: Problem in file including.
Can you show some relevant code ?
Simple code using function defined in .c file in .cpp file:
Code:
// cfunc.h
#ifndef _FUNC_H_
#define _FUNC_H_
void func();
#endif
Code:
// cfunc.c
#include <stdio.h>
#include "cfunc.h"
void func(){
printf("works?\n");
}
Code:
// main.cpp
extern "C"{
#include "cfunc.h"
}
int main(){
func();
return 0;
}
Compile & run:
Quote:
gcc -c cfunc.c
g++ main.cpp cfunc.o -o main.exe
main.exe // prints works ?