Sign in Join
Wiki copytemporary

Common problems when converting C header files/fr: what changed

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

Title: Common problems when converting C header files/fr

OldNewLine
1+# h2pas rapporte une erreur
2+
3+Voilà une liste des structures C courantes qui ne sont pas reconnues par h2pas et comment les corriger: (NdT: les titres ne sont pas traduits pour correspondre au message de l'outil)
4+
5+## Problème h2pas : extern "C"
6+
7+Certains fichiers d'entête contiennent le bloc espace de nom de C++:
8+
9+``` c
10+#if defined(__cplusplus)
11+extern "C" {
12+#endif
13+...
14+#if defined(__cplusplus)
15+}
16+#endif
17+```
18+
19+Correction: Ajouter les lignes **Supprimer les lignes 'extern "C" ' de C++** dans les outils de la page Avant h2pas.
20+
21+## Problème h2pas : Empty macro
22+
23+Certains fichiers d'en-tête contiennent des macros vides utilisées pour d'autres extensions:
24+
25+``` c
26+#define MPI_FILE_DEFINED
27+```
28+
29+Correction: Ajouter la ligne **Remove empty C macros** dans les outils de la page **Avant h2pas**.
30+
31+## Problème h2pas : Implicit array types
32+
33+C autorise des tableaux implicites dans les types de paramètre. Par exemple:
34+
35+``` c
36+int MPI_Group_range_incl(MPI_Group, int, int [][3], MPI_Group *);
37+```
38+
39+int \[\]\[3\] est un type implicit, qui n'est pas permis dans Pascal. h2pas supporte l'ajout de types de pointer. Ainsi, il suffit de remplacer tous les \[\] avec *.
40+
41+Correction: Ajouter la ligne **Remplacer \[\] par *** dans les outils de la page **Avant h2pas**.
42+
43+## Problème h2pas : Macros for 0 pointers
44+
45+Certains fichiers d'entête contiennent de pointeurs typés 0:
46+
47+``` c
48+#define MPI_BOTTOM (void *)0
49+```
50+
51+Correction: Ajouter la ligne **Replace macro values 0 pointer like (char *)0 with NULL** dans les outils de la page **Avant h2pas**.
52+
53+## Problème h2pas : function types are silently skipped
54+
55+C permet de définir des types de fonctions, alros que Pascal ne permet que des pointeurs vers des fonctions (NdT: point à vérifier, Free Pascal supporte les types procéduraux). Par exemple:
56+
57+``` c
58+typedef int (MPI_Comm_copy_attr_function)(MPI_Comm, int, void *, void *,
59+ void *, int *);
60+```
61+
62+Le fichier d'entête n'utilise jamais cette définition directement, au lieu de cela, il utilise toujours des pointeurs de tels types **MPI_Comm_copy_attr_function***, par exemple :
63+
64+``` c
65+#define MPI_COMM_NULL_COPY_FN ((MPI_Comm_copy_attr_function*)0)
66+```
67+
68+La traduction peut fonctionner en procédant ainsi: faire du type de fonction un pointeur. Ce qui signifie d'ajouter un * devant la définition et de faire précéder par un P le nom et de retirer le * après chaque référence. Par exemple:
69+
70+``` c
71+typedef 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)
74+```
75+
76+Solution: Ajouter l'outil **Convertir les types de fonction en pointeurs** dans les outils de la page Avant h2pas.
77+
78+# Compilation de test et ajouter des outils pour embellir la sortie
79+
80+Lorsque h2pas s'exécute sans erreur, il a créé un fichier Pascal. Le commutateur -i définit s'il s'agit d'une unité ou d'un fichier d'inclusion. La prochaine étape est le test de compilation. Préparez un projet de test qui utilise le nouveau source Pascal. Ensuite utilisez le bouton **Exécuter H2Pas et compiler** dans l'assistant h2pas.
81+
82+### Exemple: MPICH2
83+
84+Créer un nouveau projet avec Projet -\> Nouveau Projet -\> Programme. Enregistrez-le dans **mpihelloworld**. Modifiez le code:
85+
86+``` pascal
87+program 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.
101+```
102+
103+Ajoutez le h2p/mpi.pas au projet. Projet -\> Ajouter le fichier de l'éditeur au projet
104+
105+# Erreurs courantes du compilateur dans la sortie h2pas
106+
107+Parfois, h2pas ne produit pas du code Pascal valide. Voici les problèmes courants et comment les résoudre.
108+
109+## Unit name contains the file path
110+
111+Parfois h2pas ajoute le chemin complet au nom d'unité.
112+
113+Résolution: Ajouter **Remplacement de "unit filename;" par "unit name;"** dans les outils de la page Avant h2pas.
114+
115+## Missing include files
116+
117+h2pas convertit les directives \#include en directives pascal {\$i }. Si vous créez une unité pour chaque fichier d'entête C alors vopus pouvez retirer toutes les directives avec l'outil **Suppression des directives incluses**.
118+
119+## Forward type not resolved
120+
121+Par exemple **mpi.pas(26,16) Error: Forward type not resolved "PMPI_Aint"** La ligne d'erreur est souvent sur un type pointeur vers un enregistrement:
122+
123+``` pascal
124+PMPI_Aint = ^MPI_Aint;
125+```
126+
127+h2pas ajoute PMPI_Aint pour donner un nom au pointeurs C anonymes comme *MPI_Aint. Il ajoute les pointeurs au début du fichier. Mais Pascal exige que la définition soit une section de même **type**.
128+
129+Parfois h2pas ajoute un type pointer alors qu'il existe déjà.
130+
131+Résolution: Ajouter **Supprimer les types de pointeurs redéfinis** dans les outils **Après h2pas**.
132+
133+## Removing system types
134+
135+h2pas ajoute quelques types système, comme PLongint, qui font désormais partie de l'unité System.
136+
137+Résolution: Ajouter l'outil **Supprimer les redéfinitions de type comme "PLongint"** aux outils **Après h2pas**.
138+
139+## Empty type/var/const sections
140+
141+Après que les outills du dessus aient enlevé quelques variables, types, constantes, des sections deviennent vides, ce qui n'est pas permis en Pascal.
142+
143+Correction: Ajouter **Remove empty type/var/const sections** ou l'outil **Post H2Pas** aux outils **After h2pas**.
144+
145+## h2pas forgets to add IFDEFs for function bodies
146+
147+h2pas convertit les macros en functions. Mais la macro était dans un block IFDEF, h2pas n'ajoute pas les IFDEFs autour de la fonction.
148+
149+Correction: Ajouter l'outil **Add missing h2pas IFDEFs for function bodies** ou le **Post H2Pas**. Ajoutez ceci juste devant l'outil **Reduce compiler directives in pascal file**.
150+
151+## Implicit Types
152+
153+C permet les paramètres de types implicites, par exemple, *int \[\]\[3\]*. Pascal ne le permet pas, donc vous devez lui donner un nom et déclarer un type. Mais h2pas ne le fait pas automatiquement et ajoute le type implicite à la place dans une syntaxe quasi-Pascal. Par exemple:
154+
155+``` c
156+int some_func(int [][3]);
157+```
158+
159+devient
160+
161+``` pascal
162+function some_func(_para1:array[0..2] of Plongint):longint;cdecl;external;
163+```
164+
165+Correction: Heureusement, il y a maintenant un outil pour supprimer les types implicites, c'est appelé **Replace implicit types**. Cela fait aussi partie de l'outil **Post H2Pas**. Ajoutez-le dans la liste des outiles *after*.
166+
167+## Array of nothing
168+
169+Parfois h2pas convertit le type paramètre à ellipse du C '...' incorrectement en 'array of )', il devrait être convertit en 'array of const)'. Sometimes h2pas converts the C ellipsis parameter type '...' wrongly to 'array of )'. It should be 'array of const)'.
170+
171+## Identifier not found
172+
173+Il y a quatre cas:
174+
175+- The identifier is defined in the unit, but after it is used L'identificateur est défini mais après été utilisé.
176+- The identifier is defined in another unit (another .h file) L'identificateur est défini dnas une autre unité.
177+- The identifier is a pointer to an existing type, but h2pas forgot to add it L'identificateur est un pointeur sur un type existant mais h2pas a oublié de l'ajouter.
178+- The identifier is not defined anywhere L'identificateur n'est défini nulle part.
179+
180+### The identifier is defined in the unit, but after it is used
181+
182+En Pascal les types déclarés en avance sont uniquement permis dans une même section type. Par exemple:
183+
184+``` pascal
185+type
186+ TMyRecord = record
187+ Next: PMyRecord; // utilisation de la déclaration avancée
188+ end;
189+ PMyRecord = ^TMyRecord;
190+```
191+
192+Ce qui suit n'est pas permis, parce que la déclaration avancée est dans une autre section type
193+
194+``` pascal
195+type
196+ TMyRecord = record
197+ Next: PMyRecord; // utilisation de la déclaration avancée
198+ end;
199+type
200+ PMyRecord = ^TMyRecord;
201+```
202+
203+Solution: Le code doit être réordonné. Ajouter l'outil ''Corriger les déclarations avancées.
204+
205+### The identifier is defined in another unit (another .h file)
206+
207+#### Solution 1: Ajouter l'unité dans la section uses
208+
209+Si l'autre unité utilis déjà cette unité, alors vous avez un cycle. Les dépendances circulaires sont permises entre fichier .h, mais pas dans les unité Pascal. Dans ce cas, vous devez déplacer le codes entre les deux unités ou utiliser des IFDEFs comme dans les liaisons gtk2 ou utiliser la fonction de fusion de l'assistant h2pas.
210+
211+#### Solution 2: Fusionner les deux fichiers d'inclusion
212+
213+L'assistant h2pas peut fusionner des fichiers d'inclusion en un seul. Par exemple, first.h inclut via la directive d'inclusion C le fichier second.h. Sélectionner le second.h dans l'assistant et contrôler la fonctionnalité 'Merge file'. Par la suite, le source de second.h sera ajouté à first.h avant d'envoyer le sortie vers h2pas. Comme toujours, cela ne change pas les fichiers d'entêtes.
214+
215+#### Exemple: MPICH2
216+
217+Le type MPI_Request est défini dans mpi.h et utilisé dans mpio.h. Les deux fichiers d'en-tête c s'utilisent fortement l'un l'autre, vous ne pouvez donc pas les mettre dans deux unités séparées. Sélectionnez mpio.h et activez la fonction 'Merge file'.
218+
219+### The identifier is a pointer to an existing type, but h2pas forgot to add it
220+
221+Solution: Add the tool **Add missing pointer types like PPPChar** or the **Post H2Pas** tool to the **after H2Pas** tools.
222+
223+### The identifier is not defined anywhere
224+
225+Probably you are missing a header file or you are using one with the wrong version or it is private identifier.
226+
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.
228+
229+### Exemple: MPICH2
230+
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:
234+
235+``` c
236+typedef struct ADIOI_FileD *MPI_File;
237+```
238+
239+The h2pas tool translated this to:
240+
241+``` pascal
242+MPI_File = ^ADIOI_FileD;
243+```
244+
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+| Propriété | Valeur |
248+|-------------|----------------------------------|
249+| Caption | Replace ADIOI_FileD with Pointer |
250+| Name | ReplaceADIOI_FileDwithPointer |
251+| SearchFor | ADIOI_FileD |
252+| ReplaceWith | Pointer |
253+| Options | \[trtMatchCase,trtWholeWord\] |
254+
255+## Illegal expression
256+
257+### Exemple: MPICH2
258+
259+Compiler gives *Illegal expression* on the following statement
260+
261+``` pascal
262+const
263+ MPI_LONG_LONG = MPI_LONG_LONG_INT;
264+```
265+
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:
269+
270+Replace the
271+
272+``` c
273+#define MPI_LONG_LONG MPI_LONG_LONG_INT
274+```
275+
276+with
277+
278+``` c
279+#define MPI_LONG_LONG ((MPI_Datatype)0x4c000809)
280+```
281+
282+by adding a **Search and replace** tool before h2pas.
283+
284+# Autres problèmes courants
285+
286+## The pascal file contains a lot of unneccessary ifdef
287+
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, ...)
291+
292+You can see the directives in the [[IDE Window: Code Explorer|Code Explorer]].
293+
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, ...).
295+
296+For example: Many C headers are enclosed in
297+
298+``` pascal
299+{$IFNDEF FOO}
300+{$DEFINE FOO}
301+...
302+{$ENDIF}
303+```
304+
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.
306+
307+#### Exemple: MPICH2
308+
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:
322+
323+` MPICH2`\
324+` MPICH_NEW_MPIIO`
325+
326+## The C header files contain redefinitions
327+
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.
329+
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*.
331+
332+### Exemple: MPICH2
333+
334+The mpi.h contains several redefinitions like MPIIMPL_HAVE_MPI_TYPE_CREATE_DARRAY. They can all be fixed with this tool.
335+
336+## Alias macros are always translated by h2pas to constants
337+
338+h2pas converts macros like
339+
340+``` c
341+#define A B
342+```
343+
344+to
345+
346+``` pascal
347+const A = B;
348+```
349+
350+which is almost always correct. But some c header contain aliases for types, variables and functions too.
351+
352+### Example: MPICH2
353+
354+The mpi.h contains the macro
355+
356+``` c
357+#define MPI_LONG_LONG MPI_LONG_LONG_INT
358+```
359+
360+h2pas converts this simply to
361+
362+``` pascal
363+const
364+ MPI_LONG_LONG = MPI_LONG_LONG_INT;
365+```
366+
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.
370+
371+Note: This tool can also fix alias to external functions. For example:
372+
373+``` pascal
374+function ExternalFunc1(_para1:longint; _para2:pointer):longint;cdecl;external name 'ExternalFunc1';
375+const
376+ ExternalFuncAlias1 = ExternalFunc1;// should be replaced with full declaration
377+```
378+
379+## h2pas creates functions for constants
380+
381+Par exemple:
382+
383+``` pascal
384+function MPI_2COMPLEX : MPI_Datatype;
385+begin
386+ MPI_2COMPLEX:=MPI_Datatype(MPI_DATATYPE_NULL);
387+end;
388+```
389+
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**.
391+
392+## h2pas creates functions for type casts
393+
394+Par exemple:
395+
396+``` pascal
397+function PMPI_Win_f2c(win : longint) : MPI_Win;
398+begin
399+ PMPI_Win_f2c:=MPI_Win(win);
400+end;
401+```
402+
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.
404+
405+## Some identifiers are used before they are defined
406+
407+C allows much more forward definitions than pascal, so the definitions must be topologically sorted. For example:
408+
409+``` pascal
410+type
411+ PMPI_Datatype = ^MPI_Datatype;
412+ MPI_Datatype = longint;
413+```
414+
415+The pointer PMPI_Datatype must be moved to the same type section as MPI_Datatype.
416+
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.
418+
419+## The C header files do not contain parameter names
420+
421+For example there is an ugly function without parameter names:
422+
423+``` c
424+int MPI_Intercomm_create(MPI_Comm, int, MPI_Comm, int, int, MPI_Comm * );
425+```
426+
427+The parameter names are defined in the corresponding .c file:
428+
429+``` c
430+int MPI_Intercomm_create(MPI_Comm local_comm, int local_leader,
431+ MPI_Comm peer_comm, int remote_leader, int tag,
432+ MPI_Comm *newintercomm)
433+```
434+
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.
436+
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.
438+
439+## Voir aussi
440+
441+- [[Creating bindings for C libraries/fr|Création de liaisons pour des bibliothèques C]]
442+- [[Pascal for C users/fr|Pascal pour les utilisateurs du C]]
443+- [[Pascal for C++ users/fr|Pascal pour les utilisateurs du C++]]
444+- [[SWIG/fr|SWIG]]
445+
446+---
447+*From the Free Pascal wiki, page "Common problems when converting C header files/fr", as it stood on 2023-03-21 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).*

Back to history