Explanation of code

I have encountered a section of open source code while working on a project that i do not understand. If anyone could explain this to me I would greatly appreciate it. Thank you in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
** autostart.cpp
** This file contains the heads of lists stored in special data segments
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
**    derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
** The particular scheme used here was chosen because it's small.
**
** An alternative that will work with any C++ compiler is to use static
** classes to build these lists at run time. Under Visual C++, doing things
** that way can require a lot of extra space, which is why I'm doing things
** this way.
**
** In the case of PClass lists (section creg), I orginally used the
** constructor to do just that, and the code for that still exists if you
** compile with something other than Visual C++ or GCC.
*/
#include "autosegs.h"
#if defined(_MSC_VER)
#pragma comment(linker, "/merge:.areg=.data /merge:.creg=.data /merge:.greg=.data /merge:.sreg=.data /merge:.wreg=.data")
#pragma data_seg(".areg$a")
void *ARegHead = 0;
#pragma data_seg(".creg$a")
void *CRegHead = 0;
#pragma data_seg(".greg$a")
void *GRegHead = 0;
#pragma data_seg(".sreg$a")
void *SRegHead = 0;
#pragma data_seg()
// We want visual styles support under XP
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#elif defined(__GNUC__)
void *ARegHead __attribute__((section(AREG_SECTION))) = 0;
void *CRegHead __attribute__((section(CREG_SECTION))) = 0;
void *GRegHead __attribute__((section(GREG_SECTION))) = 0;
void *SRegHead __attribute__((section(SREG_SECTION))) = 0;
#elif
#error Please fix autostart.cpp for your compiler
#endif 


There is a matching segment of code that says it's the tails. My first question is, is the heads/tails a programming term and what are special data segments?
I also do not understand the #if, #pragma, #else, #elif, and #endif.
I understand the standard use of if, else, endif, and i'm guessing the elif is a version of elseif. What i do not understand is why they are preceded with the # sign. I have no clue what the #pragma is about. Also if someone can give me a basic description of what this code does and point me to an example of the alternative mentioned above
Is there any part you do understand?

Those #if and #else statements aren't if-then-else control structures (well they are, but not in the way you think). Everything preceeded by a # is a preprocessor directive. Check this link for more info:
http://www.cplusplus.com/doc/tutorial/preprocessor/

To be honest, if you can't use this code as it is, I'd ignore it (for now).
I'm not really all that great with these myself so I'll tell you what I know...

Things that are preceded with '#' are preprocessor definitions, this mean that they don't actually get executed by the compiler (I think) though get sent to the compiler as messages. e.g. #include is obviously including a file, so #include <iostream> replaces that line with the whole of iostream. #error means that it will give you an error and won't compile unless that error goes (I think) As for the #if defined stuff...somehow there are these macros that are defined (you can define your own using #define which will replace whenever you put that item in with the content. .e.g. #define print cout << would mean that you could then just say print "Hello; and to the compiler that would look like cout << "hello";. ) Anyway...back to the #if thing...so somehow there are these things defined in the system, e.g. I think there is one for C++ that is something like (I can't remember exactly) #if defined _cplusplus means that you are using a c++ compiler. lastly, the #pragma is one that I REALLY don't understand....it has something to do with sending a more direct message to the compiler...


EDIT: Yes...better still, look at the preprocessor tutorial, they will get it all right :D
Last edited on
Thank you Gaminic that link helped a lot. I feel like a retard for not finding that on my own. Just for clarification it is not a matter of not being able to use it, as I not at the process of compiling anything yet, I am going through the existing source code for the project I am working on and am trying to learn.
Topic archived. No new replies allowed.