Discussion:
"PORTING C" > Non inclusion should generate errors ???
(too old to reply)
Robby
2009-12-29 00:45:03 UTC
Permalink
Hello,

Sorry for the abundance of questions lately !

I don't understand why the program is still able to see a function prototype
(called API_CreateWindow()) declared in a header when I am not including that
header in the .c file which has the calling function of API_CreateWindow(). I
am supposed to get an error but I don't. I was getting ready to post a
different question when I was faced with this problem... so excuse me if my
sample is longer than it should be.

The problem strikes me at lines 45 and 59 and 65.

Please view the following sample code:

==========================KERNEL.h (L1)
#ifndef KERNEL_H
#define KERNEL_H

typedef long *LRESULT;
typedef long WPARAM;
typedef long LPARAM;

typedef struct tagHwnd {
long caption_msg;
} HWND;

typedef struct tagMsg {
HWND hwnd;
long msg;
WPARAM wParam;
LPARAM lParam;
long time;
} KM_MSG;

typedef LRESULT(*callBack)(HWND hwnd, long message, WPARAM w, LPARAM l);
#endif // KERNEL_H //

==========================KERNEL.c (L24)
#include <stdio.h>
#include "KERNEL.h"
#include "WP_INTRO.h"
#include "MAIN.h"
callBack cb[1];

int main(void)
{
KM_MSG m; HWND hwnd;
hwnd.caption_msg = 1; m.msg = 1; m.lParam = 2; m.wParam = 3;
cb[0] = CALLBACK_WP_INTRO;
f1();
CALLBACK_WP_INTRO(hwnd, m.msg, m.wParam, m.lParam);
}

==========================MAIN.h (L40)
#ifndef MAIN_H
#define MAIN_H

#include "KERNEL.h"
//#include "API.h" // <<< here! API.h not included!!!! But MAIN.c is
still sees
// API_CreateWindow () function ????
#include "WP_INTRO.h"

void f1(void);

#endif // MAIN_H //

==========================MAIN.c L(52)
#include "MAIN.h"
callBack cb[6];

void f1(void)
{int y = 0;

HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size);
}

==========================API.h (L59)
#ifndef API_H
#define API_H

#include "KERNEL.h"
// Function Declaration never included in MAIN.h ???
HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size);

#endif // API_H //

==========================API.c (L76)
#include "API.h"

HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size)
{int i; HWND h;
//......other code
return h; }

==========================WP_INTRO.h (L91)
#ifndef WP_INTRO_H
#define WP_INTRO_H

#include "KERNEL.h"

LRESULT CALLBACK_WP_INTRO(HWND hwnd, long message, WPARAM wParam, LPARAM
lParam);

#endif // WP_INTRO_H //

==========================WP_INTRO.c (L101)
#include "WP_INTRO.h"

LRESULT CALLBACK_WP_INTRO(HWND hwnd, long message, WPARAM wParam, LPARAM
lParam)
{
return 0;
}

================================== (L110)

In summary, doesn't MAIN.h have to include API.h so to include the following
function declaration:

=================================API.h
HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size);
=========================

so that MAIN.c can call it ?
--
Best regards
Roberto
Igor Tandetnik
2009-12-29 01:47:32 UTC
Permalink
Post by Robby
I don't understand why the program is still able to see a function prototype
(called API_CreateWindow()) declared in a header when I am not including that
header in the .c file which has the calling function of API_CreateWindow().
==========================MAIN.c L(52)
#include "MAIN.h"
callBack cb[6];
void f1(void)
{int y = 0;
HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size);
}
You are not calling API_CreateWindow here - you are declaring it yet again. You don't actually call API_CreateWindow anywhere in your code.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Barry Schwarz
2009-12-29 04:04:34 UTC
Permalink
On Mon, 28 Dec 2009 20:47:32 -0500, "Igor Tandetnik"
Post by Igor Tandetnik
Post by Robby
I don't understand why the program is still able to see a function prototype
(called API_CreateWindow()) declared in a header when I am not including that
header in the .c file which has the calling function of API_CreateWindow().
==========================MAIN.c L(52)
#include "MAIN.h"
callBack cb[6];
void f1(void)
{int y = 0;
HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size);
}
You are not calling API_CreateWindow here - you are declaring it yet again. You don't actually call API_CreateWindow anywhere in your code.
He was so concerned with a missing error that he didn't notice the
linker error about multiple definitions of cb. And they are
inconsistent to boot.
--
Remove del for email
Robby
2009-12-30 17:08:01 UTC
Permalink
ooff! I have one thing to say!

I don't believe I posted this!
--
Robert
Ron Francis
2009-12-29 08:26:31 UTC
Permalink
Post by Robby
I don't understand why the program is still able to see a function prototype
(called API_CreateWindow()) declared in a header when I am not including that
header in the .c file which has the calling function of API_CreateWindow(). > #include <stdio.h>
==========================MAIN.c L(52)
#include "MAIN.h"
callBack cb[6];
void f1(void)
{int y = 0;
HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size);
}
==========================API.h (L59)
#ifndef API_H
#define API_H
#include "KERNEL.h"
// Function Declaration never included in MAIN.h ???
HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size);
#endif // API_H //
==========================API.c (L76)
#include "API.h"
HWND API_CreateWindow
(
int WND_ID,
long caption_msg,
int i_x_pos,
int i_x_size,
int i_y_pos,
int i_y_size)
{int i; HWND h;
//......other code
return h; }
Robby,
You have three function prototypes there.
You should only have one and that should probably be in API.h
To use the function in MAIN.c, it would look like this:

==========================MAIN.c L(52)
#include "MAIN.h"
#include "API.c"

callBack cb[6];

void f1(void) {
int y = 0;

API_CreateWindow
(
WND_ID,
caption_msg,
i_x_pos,
i_x_size,
i_y_pos,
i_y_size
);
}

Note that those parameters in the call have to be defined somewhere.
The prototype doesn't define them, it just tells the funtion what parameters types to expect.
In fact, as long as the parameter types are correct, you could pass it any variable names you want.

Ron.
Continue reading on narkive:
Loading...