Sign in Join
Wiki copytemporary

Common problems when converting C header files: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1+## Contents
2+
3+ * 1 h2pas reports an error
4+ * 1.1 h2pas problem: extern "C"
5+ * 1.2 h2pas problem: Empty macro
6+ * 1.3 h2pas problem: Implicit array types
7+ * 1.4 h2pas problem: Makros for 0 pointers
8+ * 1.5 h2pas problem: function types are silently skipped
9+ * 2 Test compilation and add tools to beautify the output
10+ * 2.1 Example: MPICH2
11+ * 3 Common compiler errors on h2pas output
12+ * 3.1 Unit name contains the file path
13+ * 3.2 Missing include files
14+ * 3.3 Forward type not resolved
15+ * 3.4 Removing system types
16+ * 3.5 Empty type/var/const sections
17+ * 3.6 h2pas forgets to add IFDEFs for function bodies
18+ * 3.7 Implicit Types
19+ * 3.8 Array of nothing
20+ * 3.9 Identifier not found
21+ * 3.9.1 The identifier is defined in the unit, but after it is used
22+ * 3.9.2 The identifier is defined in another unit (another .h file)
23+ * 3.9.2.1 Solution 1: Add the unit to the uses section
24+ * 3.9.2.2 Solution 2: Merge the two include files
25+ * 3.9.2.3 Example: MPICH2
26+ * 3.9.3 The identifier is a pointer to an existing type, but h2pas forgot to add it
27+ * 3.9.4 The identifier is not defined anywhere
28+ * 3.9.5 Example: MPICH2
29+ * 3.10 Illegal expression
30+ * 3.10.1 Example: MPICH2
31+ * 4 Common other issues
32+ * 4.1 The pascal file contains a lot of unneccessary ifdef
33+ * 4.1.1 Hints about unneeded directives ($IFDEFs, $DEFINEs, ...)
34+ * 4.1.1.1 Example: MPICH2
35+ * 4.2 The C header files contain redefinitions
36+ * 4.2.1 Example: MPICH2
37+ * 4.3 Alias macros are always translated by h2pas to constants
38+ * 4.3.1 Example: MPICH2
39+ * 4.4 h2pas creates functions for constants
40+ * 4.5 h2pas creates functions for type casts
41+ * 4.6 Some identifiers are used before they are defined
42+ * 4.7 The C header files do not contain parameter names
43+ * 4.8 See also
44+
145# h2pas reports an error
246
3-Here is a list of common C structures, not recognized by h2pas and how to fix them:
47+Here is a list of common C structures, not recognized by h2pas and how to fix them:
448
549## h2pas problem: extern "C"
650
7-Some header files contain the C++ namespace block:
8-
9-``` c
10-#if defined(__cplusplus)
11-extern "C" {
12-#endif
13-...
51+Some header files contain the C++ namespace block:
52+```
1453#if defined(__cplusplus)
15-}
16-#endif
54+ extern "C" {
55+ #endif
56+ ...
57+ #if defined(__cplusplus)
58+ }
59+ #endif
1760```
1861
19-Fix: Add the **Remove C++ 'extern "C"' lines** or the **Pre H2Pas** tool to the **before h2pas** tools.
62+Fix: Add the **Remove C++ 'extern "C"' lines** or the **Pre H2Pas** tool to the **before h2pas** tools.
2063
2164## h2pas problem: Empty macro
2265
23-Some header files contain empty macros used for further extensions:
24-
25-``` c
66+Some header files contain empty macros used for further extensions:
67+```
2668#define MPI_FILE_DEFINED
2769```
2870
29-Fix: Add the **Remove empty C macros** or the **Pre H2Pas** tool to the **before h2pas** tools.
71+Fix: Add the **Remove empty C macros** or the **Pre H2Pas** tool to the **before h2pas** tools.
3072
3173## h2pas problem: Implicit array types
3274
33-C allows implicit arrays in parameter types. For example:
34-
35-``` c
75+C allows implicit arrays in parameter types. For example:
76+```
3677int MPI_Group_range_incl(MPI_Group, int, int [][3], MPI_Group *);
3778```
3879
39-The int \[\]\[3\] is an implicit type, which is not allowed under Pascal. h2pas supports adding pointer types. So, it enough to replace all \[\] with *.
80+The int [][3] is an implicit type, which is not allowed under Pascal. h2pas supports adding pointer types. So, it enough to replace all [] with *.
4081
41-Fix: Add the **Replace \[\] with *** or the **Pre H2Pas** tool to the **before h2pas** tools.
82+Fix: Add the **Replace [] with *** or the **Pre H2Pas** tool to the **before h2pas** tools.
4283
4384## h2pas problem: Makros for 0 pointers
4485
45-Some header files contain typed 0 pointers:
46-
47-``` c
86+Some header files contain typed 0 pointers:
87+```
4888#define MPI_BOTTOM (void *)0
4989```
5090
51-Fix: Add the **Replace macro values 0 pointer like (char *)0 with NULL** or the **Pre H2Pas** tool to the **before h2pas** tools.
91+Fix: Add the **Replace macro values 0 pointer like (char *)0 with NULL** or the **Pre H2Pas** tool to the **before h2pas** tools.
5292
5393## h2pas problem: function types are silently skipped
5494
55-C allows to define function types, while pascal only allows pointers to functions. For example:
56-
57-``` c
95+C allows to define function types, while pascal only allows pointers to functions. For example:
96+```
5897typedef int (MPI_Comm_copy_attr_function)(MPI_Comm, int, void *, void *,
59- void *, int *);
98+ void *, int *);
6099```
61100
62-The C header never uses this definition directly, instead it always uses pointers to such types **MPI_Comm_copy_attr_function***, for instace:
63-
64-``` c
101+The C header never uses this definition directly, instead it always uses pointers to such types **MPI_Comm_copy_attr_function*** , for instace:
102+```
65103#define MPI_COMM_NULL_COPY_FN ((MPI_Comm_copy_attr_function*)0)
66104```
67105
68-The translation can work this way: Make the function type a pointer. That means add a * in front of the definition and prepend a P and remove the * behind all references. For example:
69-
70-``` c
106+
107+The translation can work this way: Make the function type a pointer. That means add a * in front of the definition and prepend a P and remove the * behind all references. For example:
108+```
71109typedef int (*PMPI_Comm_copy_attr_function)(MPI_Comm, int, void *, void *,
72- void *, int *);
73-#define MPI_COMM_NULL_COPY_FN ((PMPI_Comm_copy_attr_function)0)
110+ void *, int *);
111+ #define MPI_COMM_NULL_COPY_FN ((PMPI_Comm_copy_attr_function)0)
74112```
75113
76-Solution: Add the tool **Convert function types to pointer** or **Pre H2Pas** tool to the before h2pas tools.
114+Solution: Add the tool **Convert function types to pointer** or **Pre H2Pas** tool to the before h2pas tools.
77115
78116# Test compilation and add tools to beautify the output
79117
80-When h2pas runs without errors it has created a pascal file. The -i switch defines if it is a unit or an include file. The next step is test compilation. Setup up a test project that uses the new pascal source. Then use the 'Run h2pas and compile' button in the h2pas wizard.
118+When h2pas runs without errors it has created a pascal file. The -i switch defines if it is a unit or an include file. The next step is test compilation. Setup up a test project that uses the new pascal source. Then use the 'Run h2pas and compile' button in the h2pas wizard.
81119
82120### Example: MPICH2
83121
84-Create a new project with Project -\> New Project -\> Program. Save it a **mpihelloworld**. Change the code to
85-
86-``` pascal
122+Create a new project with Project -> New Project -> Program. Save it a **mpihelloworld**. Change the code to
123+```
87124program MPIHelloWorld;
88-
89-{$mode objfpc}{$H+}
90-{$linklib mpich}
91-{$linklib rt}
92-
93-uses
94- {$IFDEF UNIX}pthreads{$ENDIF}, MPI;
95-
96-begin
97- MPI_Init(@argc, @argv);
98- writeln('Hello, world.');
99- MPI_Finalize;
100-end.
125+
126+ {$mode objfpc}{$H+}
127+ {$linklib mpich}
128+ {$linklib rt}
129+
130+ uses
131+ {$IFDEF UNIX}pthreads{$ENDIF}, MPI;
132+
133+ begin
134+ MPI_Init(@argc, @argv);
135+ writeln('Hello, world.');
136+ MPI_Finalize;
137+ end.
101138```
102139
103-Add the h2p/mpi.pas to the project. Project -\> Add editor file to project.
140+Add the h2p/mpi.pas to the project. Project -> Add editor file to project.
104141
105142# Common compiler errors on h2pas output
106143
107-Sometimes h2pas does not create valid pascal code. Here are the common problems and how to fix them.
144+Sometimes h2pas does not create valid pascal code. Here are the common problems and how to fix them.
108145
109146## Unit name contains the file path
110147
111-h2pas sometimes add the whole path to the unitname.
148+h2pas sometimes add the whole path to the unitname.
112149
113-Fix: Add **Replace "unit filename;" with "unit name;"** or the **Post H2Pas** tool to the **After h2pas** tools.
150+Fix: Add **Replace "unit filename;" with "unit name;"** or the **Post H2Pas** tool to the **After h2pas** tools.
114151
115152## Missing include files
116153
117-h2pas converts \#include directives to pascal {\$i } directives. If you create a unit for each header file, then you can remove all include directives with the tool **Remove includes** or the **Post H2Pas**.
154+h2pas converts #include directives to pascal {$i } directives. If you create a unit for each header file, then you can remove all include directives with the tool **Remove includes** or the **Post H2Pas**.
118155
119156## Forward type not resolved
120157
121-For example: **mpi.pas(26,16) Error: Forward type not resolved "PMPI_Aint"** The error line is often a pointer type to a record:
122-
123-``` pascal
158+For example: **mpi.pas(26,16) Error: Forward type not resolved "PMPI_Aint"** The error line is often a pointer type to a record:
159+```
124160PMPI_Aint = ^MPI_Aint;
125161```
126162
127-h2pas adds PMPI_Aint to give nameless C pointers like *MPI_Aint a name. It adds the pointers to the start of the file. But pascal requires, that the forward definition is in the same **type** section. Sometimes h2pas adds a pointer type, although it already exists.
163+h2pas adds PMPI_Aint to give nameless C pointers like *MPI_Aint a name. It adds the pointers to the start of the file. But pascal requires, that the forward definition is in the same **type** section. Sometimes h2pas adds a pointer type, although it already exists.
128164
129-Fix: Add the tool **Remove redefined pointer types** or the **Post H2Pas** to the **After h2pas** tools.
165+Fix: Add the tool **Remove redefined pointer types** or the **Post H2Pas** to the **After h2pas** tools.
130166
131167## Removing system types
132168
133-h2pas adds some system types, like PLongint, which are nowadays part of the system unit.
169+h2pas adds some system types, like PLongint, which are nowadays part of the system unit.
134170
135-Fix: Add **Remove type redefinitons like PLongint** or the **Post H2Pas** tool to the **After h2pas** tools.
171+Fix: Add **Remove type redefinitons like PLongint** or the **Post H2Pas** tool to the **After h2pas** tools.
136172
137173## Empty type/var/const sections
138174
139-After the above tools removed some variables, types, constants some setions become empty, which is not allowed in pascal.
175+After the above tools removed some variables, types, constants some setions become empty, which is not allowed in pascal.
140176
141-Fix: Add **Remove empty type/var/const sections** or the **Post H2Pas** tool to the **After h2pas** tools.
177+Fix: Add **Remove empty type/var/const sections** or the **Post H2Pas** tool to the **After h2pas** tools.
142178
143179## h2pas forgets to add IFDEFs for function bodies
144180
145-h2pas converts macros to functions. But if the macro was in an IFDEF block, h2pas does not add IFDEFs around the function body.
181+h2pas converts macros to functions. But if the macro was in an IFDEF block, h2pas does not add IFDEFs around the function body.
146182
147-Fix: Add the tool **Add missing h2pas IFDEFs for function bodies** or the **Post H2Pas**. Add this right in front of the tool **Reduce compiler directives in pascal file**.
183+Fix: Add the tool **Add missing h2pas IFDEFs for function bodies** or the **Post H2Pas**. Add this right in front of the tool **Reduce compiler directives in pascal file**.
148184
149185## Implicit Types
150186
151-C allows implicits types in parameters. For example: *int \[\]\[3\]*. Pascal does not allow this, so you must give it a name and declare a type. But h2pas does not do this automatically and adds instead the implicit type in a quasi pascal syntax. For example:
152-
153-``` c
187+C allows implicits types in parameters. For example: _int [][3]_. Pascal does not allow this, so you must give it a name and declare a type. But h2pas does not do this automatically and adds instead the implicit type in a quasi pascal syntax. For example:
188+```
154189int some_func(int [][3]);
155190```
156191
157-becomes
158-
159-``` pascal
192+becomes
193+```
160194function some_func(_para1:array[0..2] of Plongint):longint;cdecl;external;
161195```
162196
163-Fix: Luckily there is now a tool to remove these implicit types and add explicit types, it's called **Replace implicit types**. It is also part of the **Post H2Pas** tool. Add it to the *after* tool list.
197+Fix: Luckily there is now a tool to remove these implicit types and add explicit types, it's called **Replace implicit types**. It is also part of the **Post H2Pas** tool. Add it to the _after_ tool list.
164198
165199## Array of nothing
166200
167-Sometimes h2pas converts the C ellipsis parameter type '...' wrongly to 'array of )'. It should be 'array of const)'.
201+Sometimes h2pas converts the C ellipsis parameter type '...' wrongly to 'array of )'. It should be 'array of const)'.
168202
169-Fix: Add the tool **Fix open arrays** or the **Post H2Pas** to the after tools.
203+Fix: Add the tool **Fix open arrays** or the **Post H2Pas** to the after tools.
170204
171205## Identifier not found
172206
173-There are three cases:
207+There are three cases:
174208
175-- The identifier is defined in the unit, but after it is used
176-- The identifier is defined in another unit (another .h file)
177-- The identifier is a pointer to an existing type, but h2pas forgot to add it
178-- The identifier is not defined anywhere
209+ * The identifier is defined in the unit, but after it is used
210+ * The identifier is defined in another unit (another .h file)
211+ * The identifier is a pointer to an existing type, but h2pas forgot to add it
212+ * The identifier is not defined anywhere
179213
180214### The identifier is defined in the unit, but after it is used
181215
182-In pascal forward defined types are only allowed in a type section. For example:
183-
184-``` pascal
216+In pascal forward defined types are only allowed in a type section. For example:
217+```
185218type
186- TMyRecord = record
187- Next: PMyRecord; // using the forward definition
188- end;
189- PMyRecord = ^TMyRecord;
219+ TMyRecord = record
220+ Next: PMyRecord; // using the forward definition
221+ end;
222+ PMyRecord = ^TMyRecord;
190223```
191224
192-The below is not allowed, because the forward definition is in another type section:
193-
194-``` pascal
195-type
196- TMyRecord = record
197- Next: PMyRecord; // using the forward definition
198- end;
225+The below is not allowed, because the forward definition is in another type section:
226+```
199227type
200- PMyRecord = ^TMyRecord;
228+ TMyRecord = record
229+ Next: PMyRecord; // using the forward definition
230+ end;
231+ type
232+ PMyRecord = ^TMyRecord;
201233```
202234
203-Solution: The code must be reordered. Add the tool fix forward definitions-
235+Solution: The code must be reordered. Add the tool fix forward definitions-
204236
205237### The identifier is defined in another unit (another .h file)
206238
207239#### Solution 1: Add the unit to the uses section
208240
209-If the other unit is already using this unit, then you have a circle. A circle dependency is allowed between .h files, but not between pascal units. In this case you must move code between both units or use IFDEFs like the gtk2 bindings or use the below merge function of the h2pas wizard.
241+If the other unit is already using this unit, then you have a circle. A circle dependency is allowed between .h files, but not between pascal units. In this case you must move code between both units or use IFDEFs like the gtk2 bindings or use the below merge function of the h2pas wizard.
210242
211243#### Solution 2: Merge the two include files
212244
213-The h2pas wizard can merge include files into one. For example first.h includes via the c include directive the file second.h. Select the second.h in the wizard and check the 'Merge file' feature. Then the source of second.h' will be appended to first.h before sending the output to h2pas. As always: This does not change the header files.
245+The h2pas wizard can merge include files into one. For example first.h includes via the c include directive the file second.h. Select the second.h in the wizard and check the 'Merge file' feature. Then the source of second.h' will be appended to first.h before sending the output to h2pas. As always: This does not change the header files.
214246
247+
248+
215249#### Example: MPICH2
216250
217-The type MPI_Request is defined in mpi.h and used in mpio.h. Both c header files heavily use each other, so you can not put them into two separated units. Select mpio.h and enable the 'Merge file' function.
251+The type MPI_Request is defined in mpi.h and used in mpio.h. Both c header files heavily use each other, so you can not put them into two separated units. Select mpio.h and enable the 'Merge file' function.
218252
219253### The identifier is a pointer to an existing type, but h2pas forgot to add it
220254
221-Solution: Add the tool **Add missing pointer types like PPPChar** or the **Post H2Pas** tool to the **after H2Pas** tools.
255+Solution: Add the tool **Add missing pointer types like PPPChar** or the **Post H2Pas** tool to the **after H2Pas** tools.
222256
223257### The identifier is not defined anywhere
224258
225-Probably you are missing a header file or you are using one with the wrong version or it is private identifier.
259+Probably you are missing a header file or you are using one with the wrong version or it is private identifier.
226260
227-Solution: Search the header file and convert it too. If you can not find it or you don't want to translate this file, you can comment the identifier or replace it.
261+Solution: Search the header file and convert it too. If you can not find it or you don't want to translate this file, you can comment the identifier or replace it.
228262
229263### Example: MPICH2
230264
231-Note: this is just a demonstration. The real solution for the MPICH2 headers is to setup the Defines correct, so that the below example vanishes automatically.
232-
233-The mpi.h file defines MPI_File as pointer of ADIOI_FileD, but ADIOI_FileD is not defined public:
265+Note: this is just a demonstration. The real solution for the MPICH2 headers is to setup the Defines correct, so that the below example vanishes automatically.
234266
235-``` c
267+The mpi.h file defines MPI_File as pointer of ADIOI_FileD, but ADIOI_FileD is not defined public:
268+```
236269typedef struct ADIOI_FileD *MPI_File;
237270```
238271
239-The h2pas tool translated this to:
240-
241-``` pascal
272+The h2pas tool translated this to:
273+```
242274MPI_File = ^ADIOI_FileD;
243275```
244276
245-Because ADIOI_FileD is not defined public, it is a private structure, so you can simply replace it with a *Pointer*: Add to the 'After h2pas' tools a new tool of type 'Search and replace' with the following properties:
246-
247-| Property | Value |
248-|-------------|----------------------------------|
249-| Caption | Replace ADIOI_FileD with Pointer |
250-| Name | ReplaceADIOI_FileDwithPointer |
251-| SearchFor | ADIOI_FileD |
252-| ReplaceWith | Pointer |
253-| Options | \[trtMatchCase,trtWholeWord\] |
277+Because ADIOI_FileD is not defined public, it is a private structure, so you can simply replace it with a _Pointer_ : Add to the 'After h2pas' tools a new tool of type 'Search and replace' with the following properties:
254278
279+Property | Value
280+---|---
281+Caption | Replace ADIOI_FileD with Pointer
282+Name | ReplaceADIOI_FileDwithPointer
283+SearchFor | ADIOI_FileD
284+ReplaceWith | Pointer
285+Options | [trtMatchCase,trtWholeWord]
286+
255287## Illegal expression
256288
257289### Example: MPICH2
258290
259-Compiler gives *Illegal expression* on the following statement
260-
261-``` pascal
291+Compiler gives _Illegal expression_ on the following statement
292+```
262293const
263- MPI_LONG_LONG = MPI_LONG_LONG_INT;
294+ MPI_LONG_LONG = MPI_LONG_LONG_INT;
264295```
265296
266-The reason is, that h2pas translated the MPI_LONG_LONG_INT constant to a function.
267-
268-Solution: Fix h2pas or use a trick:
297+The reason is, that h2pas translated the MPI_LONG_LONG_INT constant to a function.
269298
270-Replace the
299+Solution: Fix h2pas or use a trick:
271300
272-``` c
301+Replace the
302+```
273303#define MPI_LONG_LONG MPI_LONG_LONG_INT
274304```
275305
276-with
277-
278-``` c
306+with
307+```
279308#define MPI_LONG_LONG ((MPI_Datatype)0x4c000809)
280309```
281310
282-by adding a **Search and replace** tool before h2pas.
311+by adding a **Search and replace** tool before h2pas.
283312
284313# Common other issues
285314
286315## The pascal file contains a lot of unneccessary ifdef
287316
288-h2pas translates even the C \#ifdef directives. Many of them are C specific and not needed under FPC and can even make it impossible many tools on this side to explore the code. That's why there is a tool to clean up and disable or remove many unneeded IFDEFs: **Reduce compiler directives in pascal file**. Do not forget to insert the tool **Add missing h2pas IFDEFs for function bodies** in front of it.
289-
290-### Hints about unneeded directives (\$IFDEFs, \$DEFINEs, ...)
317+h2pas translates even the C #ifdef directives. Many of them are C specific and not needed under FPC and can even make it impossible many tools on this side to explore the code. That's why there is a tool to clean up and disable or remove many unneeded IFDEFs: **Reduce compiler directives in pascal file**. Do not forget to insert the tool **Add missing h2pas IFDEFs for function bodies** in front of it.
291318
292-You can see the directives in the [[IDE Window: Code Explorer|Code Explorer]].
319+### Hints about unneeded directives ($IFDEFs, $DEFINEs, ...)
293320
294-The tool will only remove the \$IFDEFs and \$DEFINEs, that are unneeded on all platforms. That means for example a {\$IFDEF CPU386} will not be removed - even if you are currently working on an intel 32 bit compatible machine it will not be removed. This allows to create platform independent bindings. Of course C headers often contain a lot of IFDEFs not needed under FPC (Delphi, Kylix, gpc, ...).
321+You can see the directives in the [[IDE Window: Code Explorer|Code Explorer]].
295322
296-For example: Many C headers are enclosed in
323+The tool will only remove the $IFDEFs and $DEFINEs, that are unneeded on all platforms. That means for example a {$IFDEF CPU386} will not be removed - even if you are currently working on an intel 32 bit compatible machine it will not be removed. This allows to create platform independent bindings. Of course C headers often contain a lot of IFDEFs not needed under FPC (Delphi, Kylix, gpc, ...).
297324
298-``` pascal
325+For example: Many C headers are enclosed in
326+```
299327{$IFNDEF FOO}
300-{$DEFINE FOO}
301-...
302-{$ENDIF}
328+ {$DEFINE FOO}
329+ ...
330+ {$ENDIF}
303331```
304332
305-This trick allows to include a C header file multiple times - like the units under pascal. So, pascal does not need this. Therefore the tool *Reduce compiler directives in pascal file* and the **Post H2Pas** tool have two properties: *Defines* and *Undefines*. Click on the *...* button of the property to edit them. Each line can contain a macroname, that should be (un)defined. Add the line *FOO* to the *Undefines*. This way the **\$IFNDEF FOO** is always true and the tool will remove it. The **\$DEFINE FOO** is now unneeded too and will be removed too.
333+This trick allows to include a C header file multiple times - like the units under pascal. So, pascal does not need this. Therefore the tool _Reduce compiler directives in pascal file_ and the **Post H2Pas** tool have two properties: _Defines_ and _Undefines_. Click on the _..._ button of the property to edit them. Each line can contain a macroname, that should be (un)defined. Add the line _FOO_ to the _Undefines_. This way the **$IFNDEF FOO** is always true and the tool will remove it. The **$DEFINE FOO** is now unneeded too and will be removed too.
306334
307335#### Example: MPICH2
308336
309-Add the following to the *Undefines* property:
310-
311-` MPI_INCLUDED`\
312-` MPIO_INCLUDE`\
313-` NEEDS_MPI_FINT`\
314-` MPI_BUILD_PROFILING`\
315-` MPICH_SUPPRESS_PROTOTYPES`\
316-` MPI_Wtime`\
317-` HAVE_MPICH2G2`\
318-` FOO`\
319-` HAVE_PRAGMA_HP_SEC_DEF`
320-
321-Add the following to the *Defines* property:
337+Add the following to the _Undefines_ property:
338+```
339+MPI_INCLUDED
340+ MPIO_INCLUDE
341+ NEEDS_MPI_FINT
342+ MPI_BUILD_PROFILING
343+ MPICH_SUPPRESS_PROTOTYPES
344+ MPI_Wtime
345+ HAVE_MPICH2G2
346+ FOO
347+ HAVE_PRAGMA_HP_SEC_DEF
348+```
322349
323-` MPICH2`\
324-` MPICH_NEW_MPIIO`
350+
351+Add the following to the _Defines_ property:
352+```
353+MPICH2
354+ MPICH_NEW_MPIIO
355+```
325356
326357## The C header files contain redefinitions
327358
328-This is ok for C compilers, because a type is the same if its definition is the same. But for Pascal each type is a different type.
359+This is ok for C compilers, because a type is the same if its definition is the same. But for Pascal each type is a different type.
329360
330-Solution: Add the tool **Remove redefinitions in pascal unit** or the **Post H2Pas** to the after h2pas tools. As the name implies, it only works on units, not on include files. And it expect at least a valid pascal syntax. That's why it is important to add this tool **after** the tools that fix the pascal syntax like *Replace "unit filename;" with "unit name;"*, *Remove empty type/var/const sections*, *Remove includes*, *Replace implicit types* and *Fix open arrays*.
361+Solution: Add the tool **Remove redefinitions in pascal unit** or the **Post H2Pas** to the after h2pas tools. As the name implies, it only works on units, not on include files. And it expect at least a valid pascal syntax. That's why it is important to add this tool **after** the tools that fix the pascal syntax like _Replace "unit filename;" with "unit name;"_ , _Remove empty type/var/const sections_ , _Remove includes_ , _Replace implicit types_ and _Fix open arrays_.
331362
332363### Example: MPICH2
333364
334-The mpi.h contains several redefinitions like MPIIMPL_HAVE_MPI_TYPE_CREATE_DARRAY. They can all be fixed with this tool.
365+The mpi.h contains several redefinitions like MPIIMPL_HAVE_MPI_TYPE_CREATE_DARRAY. They can all be fixed with this tool.
335366
336367## Alias macros are always translated by h2pas to constants
337368
338-h2pas converts macros like
339-
340-``` c
369+h2pas converts macros like
370+```
341371#define A B
342372```
343373
344-to
345-
346-``` pascal
374+to
375+```
347376const A = B;
348377```
349378
350-which is almost always correct. But some c header contain aliases for types, variables and functions too.
379+which is almost always correct. But some c header contain aliases for types, variables and functions too.
351380
352381### Example: MPICH2
353382
354-The mpi.h contains the macro
355-
356-``` c
383+The mpi.h contains the macro
384+```
357385#define MPI_LONG_LONG MPI_LONG_LONG_INT
358386```
359387
360-h2pas converts this simply to
361-
362-``` pascal
388+h2pas converts this simply to
389+```
363390const
364- MPI_LONG_LONG = MPI_LONG_LONG_INT;
391+ MPI_LONG_LONG = MPI_LONG_LONG_INT;
365392```
366393
367-but MPI_LONG_LONG_INT is a function.
368-
369-Solution: Add the tool **Fixes section type of alias definitions in pascal unit** or the **Pre H2Pas** tool to the **Before H2Pas** tools.
394+but MPI_LONG_LONG_INT is a function.
370395
371-Note: This tool can also fix alias to external functions. For example:
396+
397+Solution: Add the tool **Fixes section type of alias definitions in pascal unit** or the **Pre H2Pas** tool to the **Before H2Pas** tools.
372398
373-``` pascal
399+Note: This tool can also fix alias to external functions. For example:
400+```
374401function ExternalFunc1(_para1:longint; _para2:pointer):longint;cdecl;external name 'ExternalFunc1';
375-const
376- ExternalFuncAlias1 = ExternalFunc1;// should be replaced with full declaration
402+ const
403+ ExternalFuncAlias1 = ExternalFunc1;// should be replaced with full declaration
377404```
378405
379406## h2pas creates functions for constants
380407
381-For example:
382-
383-``` pascal
408+For example:
409+```
384410function MPI_2COMPLEX : MPI_Datatype;
385-begin
386- MPI_2COMPLEX:=MPI_Datatype(MPI_DATATYPE_NULL);
387-end;
411+ begin
412+ MPI_2COMPLEX:=MPI_Datatype(MPI_DATATYPE_NULL);
413+ end;
388414```
389415
390-All used identifiers are types and constants, so this function can be replaced with a simple constant. This is checked and done by the tool **Replace simple functions with constants** or the **Post H2Pas**.
416+All used identifiers are types and constants, so this function can be replaced with a simple constant. This is checked and done by the tool **Replace simple functions with constants** or the **Post H2Pas**.
391417
392418## h2pas creates functions for type casts
393419
394-For example:
395-
396-``` pascal
420+For example:
421+```
397422function PMPI_Win_f2c(win : longint) : MPI_Win;
398-begin
399- PMPI_Win_f2c:=MPI_Win(win);
400-end;
423+ begin
424+ PMPI_Win_f2c:=MPI_Win(win);
425+ end;
401426```
402427
403-This function can be replaced with a simple type, resulting in a type cast. This is checked and done by the tool **Replace simple functions with type casts** or the **Post H2Pas** tool.
428+This function can be replaced with a simple type, resulting in a type cast. This is checked and done by the tool **Replace simple functions with type casts** or the **Post H2Pas** tool.
404429
405430## Some identifiers are used before they are defined
406431
407-C allows much more forward definitions than pascal, so the definitions must be topologically sorted. For example:
408-
409-``` pascal
432+C allows much more forward definitions than pascal, so the definitions must be topologically sorted. For example:
433+```
410434type
411- PMPI_Datatype = ^MPI_Datatype;
412- MPI_Datatype = longint;
435+ PMPI_Datatype = ^MPI_Datatype;
436+ MPI_Datatype = longint;
413437```
414438
415-The pointer PMPI_Datatype must be moved to the same type section as MPI_Datatype.
439+The pointer PMPI_Datatype must be moved to the same type section as MPI_Datatype.
416440
417-Solution: Add the **Fix forward definitions by reordering** or the **Pre H2Pas** tool to the **Before H2Pas** tools. The tool should be put **after** the tools fixing redefinitions and types. At the moment the tool only moves pointer types. ToDo: reorder some more definitions like constants.
441+Solution: Add the **Fix forward definitions by reordering** or the **Pre H2Pas** tool to the **Before H2Pas** tools. The tool should be put **after** the tools fixing redefinitions and types. At the moment the tool only moves pointer types. ToDo: reorder some more definitions like constants.
418442
419-## The C header files do not contain parameter names
443+│ **English (en)** │ **[[Common problems when converting C header files/fr|français (fr)]]** │
444+****
420445
421-For example there is an ugly function without parameter names:
446+## The C header files do not contain parameter names
422447
423-``` c
448+For example there is an ugly function without parameter names:
449+```
424450int MPI_Intercomm_create(MPI_Comm, int, MPI_Comm, int, int, MPI_Comm * );
425451```
426452
427-The parameter names are defined in the corresponding .c file:
428-
429-``` c
453+The parameter names are defined in the corresponding .c file:
454+```
430455int MPI_Intercomm_create(MPI_Comm local_comm, int local_leader,
431- MPI_Comm peer_comm, int remote_leader, int tag,
432- MPI_Comm *newintercomm)
456+ MPI_Comm peer_comm, int remote_leader, int tag,
457+ MPI_Comm *newintercomm)
433458```
434459
435-Solution: There is no solution yet. Except manual editing or searching for other .h files. Often such .h files were auto generated and there are some nicer header files somewhere on the internet.
460+Solution: There is no solution yet. Except manual editing or searching for other .h files. Often such .h files were auto generated and there are some nicer header files somewhere on the internet.
436461
437-Proposal: Write a clever tool, that searches functions with missing parameter names and the corresponding functions in the .c files with parameter names and improve the .h file.
462+Proposal: Write a clever tool, that searches functions with missing parameter names and the corresponding functions in the .c files with parameter names and improve the .h file.
438463
439464## See also
440465
441-- [[Creating bindings for C libraries]]
442-- [[Pascal for C users]]
443-- [[Pascal for C++ users]]
444-- [[SWIG]]
466+ * [[Creating bindings for C libraries]]
467+ * [[Pascal for C users]]
468+ * [[Pascal for C++ users]]
469+ * [[SWIG]]
445470
446471---
447-*From the Free Pascal wiki, page "Common problems when converting C header files", as it stood on 2022-05-03 after a change by E-ric, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
472+*From the Free Pascal wiki, page "Common problems when converting C header files", as the Internet Archive kept it on 2024-03-08. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history