|
|
|
![]() Downloads ![]() Articles ![]() Resources |
|
![]() C Programming In Clarion ![]() The Clarion development system has been shipping with a C compiler for some time and yet many people are still running into problems developing Multi-Language applications.
The Programmer's Guide contains a single chapter on Multi-Language Programming which is a must read, despite the fact that it was written about 5 years ago and some of the information is no longer accurate or relevant.
Multi-Language Programming deserves a detailed discussion, however this article is intended only to illustrate the most fundamental aspects of calling C code from a Clarion application.
Those of you that use the Application Generator for developing every application will have to excuse the fact that I'm an unrepentant hand-coder.
![]() The Anatomy of a C++ Source File ![]() Those of you who are still conscious are probably saying "C++, why would I want to know about C++? I want to call C code!" The problem is that the 'C' compiler that ships with Clarion is really a C++ compiler. It does the same job, but there are some important differences, I won't go into all the subtle differences between C & C++, I will simply discuss those that are likely to affect the way you have to code for compatibility with Clarion.
The best way to examine a programming issue is to look at some code:
![]()
1: // TSTC.C
2: #if defined(__cplusplus)
3: extern "C" {
4: #endif
5: int add(int a, int b);
6: #if defined(__cplusplus)
7: };
8: #endif
9:
10: int add(int a, int b)
11: { return a+b;
12: }
This possibly looks a little more complicated than you first anticipated, I will now describe in detail what is going on at each line of code:
![]()
![]() Calling C From Clarion ![]() The Clarion code is simpler to explain, particularly as you are probably quite familiar with the language, still taking a similar approach:
1:! TSTCLW.CLW
2: PROGRAM
3:
4: MAP
5: MODULE('TstC')
6: add(SIGNED a, SIGNED b), SIGNED, PROC, RAW, NAME('_add')
7: END
8: END
9:
10: CODE
11: MESSAGE(add(2,2))
This code does very little, in fact nothing useful other than to demonstrate the success of calling the add() function by displaying its' result.
![]()
![]() Putting it all Together ![]() The only thing missing is the project file, this couldn't be simpler and is easily created in the Clarion IDE. I have included it here for completeness but it deserves no additional explanation: -- tstc #noedit #system win32 #model clarion dll #pragma debug(vid=>full) #compile tstclw.clw #compile tstc.c #link tstc.exe ![]() And Finally... ![]() That's it! Nothing simpler really, however I realise I have only scratched the surface here. If there are specific issues that you would like to see addressed then let me know, I will gladly incorporate new information into future articles. |
![]() ![]() Copyright © L3 Technologies, Ltd. |