Headers

Hi,

Another silly question (but I'm newish to C++ Windows forms).

I have written a program that has quite a number of labels, groupboxes, radiobuttons etc. Zhuge suggested the following link in my previous post: http://www.cplusplus.com/forum/articles/10627/ which I truely suggest to any newbies to programming.

Anyway, all of my code is written in Form1.h and as a consequence, compile time is quite long. But the program runs fine. The above article suggests to have a number of header files for specific 'ideas' of code to reduce compile time. I see where to do this for my program but I'm unsure how to write a fresh header that works for Windows Form Apps.

So here's a simplified question that should help lead me in the right direction.

Assume Form1.h is blank and its code is standard for VC++ 2010.

Imagine I was to make a header file callled button.h and this contained the code for a single functioning 'Exit' button which appeard on the form of Form1.h. I understand that Form1.h needs to include button.h. But how would the code look in button.h so that Form1.h and the source file functioned properly?... That is, when I run the program, a form loads with an exit button that exits from the program when clicked. I hope this makes sense.

Thanks in advance.
Last edited on
Windows Forms means that you are using a Microsoft proprietary language called C++/CLI which is in no way the same thing as C++ language you seen on this website.
Just putting source in another file is not enough to reduce compile time.

The compiler compiles .cpp files one by one. So, if you want to reduce compile time you have to do two things.

First:
Put the actual code (functions / member functions of classes) in a new .cpp file.
Create a header (.h) file. Put prototypes for the functions or the class declaration (if you have created any classes) in that header.

Second:
Include that header in your newly created .cpp file AND in the Form1.h.

There are some problems with that, especially when you've already written a lot of code.
For example: if all your code is part of the class 'Form1', it might be hard to pull that apart.
You might have to separate code into multiple classes.

.... damn, I wanted to write an example, but I have to go ... maybe that text above helps already.
If not, ask, I'll be back later (tomorrow-ish).
That article is a bit misleading, if you read Disch's first bullet point without the preceding paragraph and only in the context of that section header then it appears to say that putting your code into header files will speed up your compile time; but that is false. If you take into account the first paragraph after the header, the one ending in a colon, then you see that he is saying those points apply to source code files, i.e. files with the '.c\.cpp' extension. This is because header files are prepended to the source files that they are included in, so by the time the compiler sees the code from them they are part of a huge lump of data. This is different from source code files which are linked into object code files individually for the project.

Open one of your projects. Some where in that directory you will find a collection of files ending in a '.o' extension. There will be one for each source code file, but not for any of the headers you included in your project. Not having to regenerate the code in those files is where your speed boost comes from.

modoran is correct by the way, CLI is NOT C++. But there are enough similarities that the information I provided still applies.

SIDE_NOTE: On Windows 7 & 8 there is a weird issue with linking where it takes twice as long to do as it did on previous versions of Windows. The site for MingW points out that this issue does not occur on partitions that are formatted for FAT. So if you break up your drive (right click on computer -> Manage -> Disk Management -> Right click on the drive and shrink volume -> right click on the now unallocated space -> New Simple Volume -> Next, Pick a letter, Next, select exFAT from the drop down beside "File System", Next, Finish) so that your projects reside there then you can speed up your linking time.
closed account (z05DSL3A)
modoran is correct by the way, CLI is NOT C++
That is C++/CLI is not C++ ;0)

http://www.ecma-international.org/publications/standards/Ecma-372.htm
Thanks for the constructive comments, people.

I get the C++ / CLI issue now but a misunderstanding like this is natural for new programmers (like me) ;)

I'm still looking for advice on how to have 'buttons' for example appear on say Form1 but coded (e.g. its function such as 'exit' from program) from another header... See original post if this doesn't make sense.

I've since managed to have 'text' and its function code originate from a separate header but appear in labels on From1 when the program is run.

I've tried to mirror this code to suit an exit button function but can't seem to get it to work....

Thanks again for all the good advice.
closed account (z05DSL3A)
sambos wrote:
I'm still looking for advice on how to have 'buttons' for example appear on say Form1 but coded (e.g. its function such as 'exit' from program) from another header... See original post if this doesn't make sense.
You will probably not like this advice but... Do not bother with C++/CLI and Winforms together. According to the VC++ team (the link escapes me at the moment) C++/CLI was primarily intended to provide for great interop solutions, and not another full-featured .NET language.

Winforms has been dropped from the visual C++ CLR templates list in Visual Studio 2012 and on. Microsoft s advise would seem to be if you want to do Windows Desktop UI code, use C# (or VB.NET).
Winforms has been dropped from the visual C++ CLR templates list in Visual Studio 2012 and on.

Nooooooooooooooooooooo!!!
I just got used to WinForms with C++. Damn you Microsoft. I don't like C#.
Good time for me to have a look at Qt, I guess.
Topic archived. No new replies allowed.