Creating controls from another sourcefile

Hello guys..

Ive stumbled on a problem that im apparently missing something big in haha.

Im coding the win32api without any "gui tool" and therefore there are alot of code for creating the controls, and i want to move that functions to Another sourcefile, just for the reason that it is easier to read, instead of having all the "createwindow" calls inside WM_CREATE..

Now, im creating a headerfile with all the declarations, and then a new sourcefile with a function that calls createwindow directly with the different Controls..

This doesnt work for several reasons, one is that the linker says that all declarations is "already declared". That is probably because ive included the headerfile in both sourcefiles, i thought i needed that since i use the declarations in both sourcefiles.

Now this is apperently totaly wrong, and any pointers if/how you should do this is greatly appreciated..

I hope you understand my question!

I'd personally recommend leaving all your CreateWindowEx() calls for child window controls within the WM_CREATE handler. Not a necessity, but its what I prefer to do. Consider the WM_CREATE handler as the 'Constructor' for the top level window, so it makes sense to put window construction code there.

If you wish to seperate various calls into seperate *.cpp / *.h files, realize that the compiler/linker must find everything within each set it needs, or you'll get missing declaration errors from the compiler, or unresolved externals from the linker. It helps to use inclusion guards in your headers to prevent multiple inclusions, i.e., ...

#ifndef MyInclude_h
#define MyInclude_h
...
...
...
#endif
Thank you very much for your input!

The reason that i want to move it to another file is that i have so many controls created (maybe 12). so the WM_CREATE is very big as you can imagine :)


Actually the problem i had is that the linker tells me that the definition is already defined (double defined)..

This is probably because i have not the correct skills for doing this. I have only experimented with sourcefiles that contains functions that does not need any "cross" definitions..

So what ive done is to include all definitions in a header.h file, and include it in both cpp files. That is not the correct way haha! :)
It sounds to me like what you really want to do Crosswinds, is modularize your code, i.e., make it more organized and maintainable. Putting WM_CREATE handler code in another source code file in my opinion isn't the best way to go. If indeed that is your goal, i.e., modularizing your code, simply remove all the code within the case labels of your WndProc's switch and place that code in a seperate procedure called within each switch, i.e.,

1
2
3
4
5
6
7
8
long fnWndProc_OnCreate(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 ...
}

case WM_CREATE:
   return fnWndProc_OnCreate(hwnd,wParam,lParam);


That way, your Window Procedure won't grow to gargantuan size, and your code will be more maintainable.
Actually yes, the last part is exactly what i want to do, but it would be great to be able to do this in another sourcefile.. Im very sorry if this wasnt clear before, my english may not be the very best :)
Im sorry, but i really cannot get this to work.

There is something with headers and headerguards that iam not understanding.

Ive created an "create.cpp" and made my function that my createwindow for the Controls is located.. This is the one that is called from the WM_CREATE switch..

I made an .h file with the HWND declarations for the different Control handles..

in that file i have the header guards.

i then have included the header in both main.cpp and create.cpp file. But i ofcourse get the linker error with "already defined" problems..

Output:
1
2
3
4
5
6
7
8
9
1>Telemetry.obj : error LNK2005: "struct HWND__ * hWnd" (?hWnd@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * BRAKESTATE" (?BRAKESTATE@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * FLSPEED" (?FLSPEED@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * FRSPEED" (?FRSPEED@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * RLSPEED" (?RLSPEED@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * RRSPEED" (?RRSPEED@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * ENGSPEED" (?ENGSPEED@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * ENGTEMP" (?ENGTEMP@@3PAUHWND__@@A) already defined in create.obj
1>Telemetry.obj : error LNK2005: "struct HWND__ * EXHTEMP" (?EXHTEMP@@3PAUHWND__@@A) already defined in create.obj
Last edited on
No ideas what im missing?
Topic archived. No new replies allowed.