Sign in Join
Wiki copytemporary

Chelper: what changed

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

OldNewLine
11### About
22
33Chelper is Lazarus extension tool, that helps converting C headers to Pascal. Yes, it's yet another C-to-Pascal converting tool like lot of similar ([[H2Pas]], ToPas), but one might find it more useful.
44
55The extension doesn't try to convert a header for the user, instead it's a tool to help with manual translation, doing almost all of "dirty work" for the user.
66
77Chelper also supports converting Objective-C declarations into Objective-P.
88
99### Author
1010
1111Dmitry 'skalogryz' Boyarintsev
1212
1313### License
1414
1515[modified](http://svn.freepascal.org/svn/lazarus/trunk/COPYING.modifiedLGPL) [LGPL](http://svn.freepascal.org/svn/lazarus/trunk/COPYING.LGPL) (same as the FPC RTL and the Lazarus LCL). You can contact the author if the modified LGPL doesn't work with your project licensing.
1616
1717### Download
1818
1919There's no "downloadable" package yet, you can access the tool from SVN:
2020
2121<https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/chelper/>
2222
23-`svn checkout `[`https://svn.code.sf.net/p/lazarus-ccr/svn/components/chelper/`](https://svn.code.sf.net/p/lazarus-ccr/svn/components/chelper/)
23+```
24+svn checkout https://svn.code.sf.net/p/lazarus-ccr/svn/components/chelper/
25+```
2426
2527### Change Log
2628
2729- Version 0.8, 8 Aug 2010
2830
2931### Dependencies / System Requirements
3032
3133- Lazarus 0.9.29 or Higher
3234
3335Status: *Beta*
3436
3537### Issues
3638
3739- No complete expression parser to construct the expression tree.
3840
3941If you find a bug in the converter please report it to [bugs.freepascal.org](http://bugs.freepascal.org) for Lazarus-CCR project. Please attach the C-header text and the explanation what doesn't work as expected.
4042
4143### Installation
4244
4345- Get the package from SVN
4446- Install Chelper.lpk package to the Lazarus.
4547- Go to *Tools-\>C to Pascal Options-\>Converter*, uncheck *Use external converter* if you want to use the package built-in converter.
4648
4749Using built-in converter has an advantage of the performance. However, if you'll need to update converter, you'll need to rebuild the whole IDE. It's also possible that converter might cause crashes or hangs. You should bug report it, but you're risking to loose all of your data.
4850
4951It's much safer to use the external cconvert tool. It's also easier to update the converter if a new features are added or bugs fixed. All you need is to rebuild the cconverter executable a lot of faster than rebuilding Lazarus.
5052
5153To use the external tool, you need to do the following:
5254
5355- Open and build cconvert.lpi project (located at the package directory). You should get "cconvert" executable file.
5456- Open Tools-\>C to Pascal Options-\>Converter, press Select and choose cconvert executable file.
5557- Check *Use external converter* at *Tools-\>C to Pascal Options-\>Converter*
5658
5759### How to use
5860
5961#### Shortkeys
6062
6163- ctrl+b - convert a single C declaration to Pascal
6264- ctrl+shift+b - convert all C declarations to Pascal from the current cursor position to the end of the file.
6365
6466#### General
6567
6668- Open a file with C declarations in Lazarus IDE or start a new file copying declarations there
6769- Place the cursor at the line where C declaration and press Ctrl+B (the shortcut would be modifiable in future).
6870
6971for example:
7072
71-`int c;`\
72-`int b; `
73+```
74+int c;
75+int b;
76+```
7377
7478if the cursor placed at 'inc c;' line and ctrl+b pressed the result would be:
7579
76-`var`\
77-`  c : Integer;`\
78-`int b;`
80+``` pascal
81+var
82+ c : Integer;
83+int b;
84+```
7985
8086exactly one definition is converted. The cursor would be place right after the converted definition. You can press ctrl+b again, and the result would be:
8187
82-`var`\
83-`  c : Integer;`\
84-`var `\
85-`  b : Integer;`
88+``` pascal
89+var
90+ c : Integer;
91+var
92+ b : Integer;
93+```
8694
8795Parsing definitions one-by-one will get your C headers ready with a very short period of time, as well as keeping it clean and correct from Pascal point of view.
8896
8997- If you're sure your defines (explained below) are configured well enough. You can try to convert the rest of the file in one call by pressing ctrl+shift+b
9098
9199i.e. you have 2 declarations in the file:
92100
93-`int fn(int, float);`\
94-`extern int v;`
101+```
102+int fn(int, float);
103+extern int v;
104+```
95105
96106placing the cursor before them and pressing ctrl+shift+b, would generate.
97107
98-`function fn(par0: Integer; par1: Single): Integer; cdecl; external;`\
108+``` pascal
109+function fn(par0: Integer; par1: Single): Integer; cdecl; external;
99110
100-`var`\
101-`  v : Integer; external;`
111+var
112+ v : Integer; external;
113+```
102114
103115**You must be careful, because it's common to use macros in C headers. Leaving any of macros undefined might produce wrong pascal declaration**. The next section explains how to define a macro for the chelper.
104116
105117#### Defines
106118
107119In most cases parameterless macro definitions, like
108120
109-`#define VALUE 1 << 2;`
121+```
122+#define VALUE 1 << 2;
123+```
110124
111125are used like constant values. Chelper translates them as constants trying to convert the define expression.
112126
113-`const`\
114-`  VALUE = 1 shr 2;`
127+``` pascal
128+const
129+ VALUE = 1 shr 2;
130+```
115131
116132In other cases defines are used for some "special" markings or additional information for external tools.
117133
118-`#define LIB_DEPRECATED `\
119-`LIB_DEPRECATED int somefunc(int p);`
134+```
135+#define LIB_DEPRECATED
136+LIB_DEPRECATED int somefunc(int p);
137+```
120138
121139without the knowledge of LIB_DEPRECATED being the macro (of nothing), it's impossible to parse function declaration correctly. You can specify macros used by Chelper in the special defines file.
122140
123141Select Tools-\>C to Pascal options-\>Main select the defines file and press Edit.
124142
125143This would open the custom defines file in Lazarus IDE. All macros declared at this file are used by Chelper. After adding/editing macro defines save the file and you can return to the C header converting.
126144
127145Chelper handles parameter macros as well.
128146
129-`int somefunc SAFE_PARAMS( (int i))`
147+```
148+int somefunc SAFE_PARAMS( (int i))
149+```
130150
131151adding the the following declaration into defines file
132152
133-`#define SAFE_PARAMS(x) x`
153+```
154+#define SAFE_PARAMS(x) x
155+```
134156
135157will allow to parse the declaration correctly
136158
137-`function somefunc(i: Integer): Integer;`
159+``` pascal
160+function somefunc(i: Integer): Integer;
161+```
138162
139163#### Configuration File
140164
141165Configuration file represents itself a simple .ini file. Following section are used in the file:
142166
143167- Main - for general declarations, that effects
144168
145169- Types - custom type conversion section. Name=Value, where Name is the c-type name, Value is resulting pascal type
146170
147171example:
148172
149-`[Types]`\
150-`int=integer`\
151-`int*=PInteger`\
152-`float=single`\
153-`float*=PSingle`
173+```
174+[Types]
175+int=integer
176+int*=PInteger
177+float=single
178+float*=PSingle
179+```
154180
155181---
156182*From the Free Pascal wiki, page "Chelper", as it stood on 2022-04-30 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