For example, suppose I have the following files: hello.cpp, hello.h, and main.c
In hello.cpp say I have the following:
#include "hello.h"
extern "C" void test_func(int &a, int b){
some stuff
}
In hello.h I have the following:
#ifdef __cplusplus
extern "C" {
#endif
void test_func(int& a, int b);
#ifdef __cplusplus
}
#endif
Here is where I am confused. If I have the following in main.c:
#include "hello.h"
extern void test_func(int*, int);
then this will not compile properly. It tells me that I have errors in my hello.h file, I assume this is because C does not support pass by reference? I noticed that if I change my hello.h file to read "void test_func(int*a, int b)" then this will compile properly.
However, If I do not have #include "hello.h" in my main.c file, then it will also compile properly. And, I am able to call test_func from main.c even without including hello.h. Is declaring the function prototype enough? And why? If I wanted to include hello.h would that mean I have to make it C compatible and not have any functions that pass by reference?
Very new to all this so thanks in advance for anyone's help.
int, and have no argument prototypes-- meaning you can pass them anything without error checking. Nice and minimal.int. If these conditions are not met (and there may be function implementations that are not possible to call correctly without a prototype) then undefined behavior will be the result. One of the possible behaviors of UB is the appearance that things work as you expect. But that's a fragile result.