PDA

View Full Version : Problem in file including.



Niamita
27th July 2011, 10:20
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.

stampede
27th July 2011, 10:48
Is this function declared as extern in header ?
Is the .c file with function definition compiled and .o linked ?

Niamita
27th July 2011, 10:50
no it is not declared with "extern".

stampede
27th July 2011, 11:31
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 ?

Niamita
27th July 2011, 11:46
ya .c file compiled succesfully.

stampede
27th July 2011, 12:15
Can you show some relevant code ?
Simple code using function defined in .c file in .cpp file:


// cfunc.h
#ifndef _FUNC_H_
#define _FUNC_H_

void func();

#endif




// cfunc.c
#include <stdio.h>
#include "cfunc.h"

void func(){
printf("works?\n");
}



// main.cpp
extern "C"{
#include "cfunc.h"
}

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

Compile & run:


gcc -c cfunc.c
g++ main.cpp cfunc.o -o main.exe
main.exe // prints works ?