Recent

Author Topic: Using C/C++ code in FPC/Lazarus  (Read 16056 times)

yellowbit

  • New Member
  • *
  • Posts: 22
Using C/C++ code in FPC/Lazarus
« on: May 31, 2016, 08:59:04 pm »
After reading this small tutorial: ftp://ftp.freepascal.org/fpc/docs-pdf/CinFreePascal.pdf I tried to use it in real life. Trying to compile the code from PDF, I get this error:

Quote
hellou.pas(17,0) Warning: Compile Project, Target: FPC_INT.exe: Exit code 1, Errors: 1, Warnings: 1
hellou.pas(16,0) Warning: Library c.dll not found, Linking may fail !
FPC_INT.lpr(20,1) Error: Import library not found for c

I use CodeTyphon IDE on Windows. What should I install or configure to run this code? Where can I get DLL file mentioned in the error message?
« Last Edit: May 31, 2016, 09:09:44 pm by yellowbit »

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Using C/C++ code in FPC/Lazarus
« Reply #1 on: May 31, 2016, 10:36:34 pm »
What C compiler are you using for the example C code? Note that the .pdf article is written for Linux. The C runtime libraries will be named differently with the Windows C compilers (Visual C++, Mingw). I would guess you would need to revise the example Pascal program to get it to work on Windows.

Also, what are you trying to accomplish? Do you have C code that you need to link into a Pascal executable? Is it part of a dynamic library as well (DLL on Windows)? If so, then you might find the following helpful, although it looks like the .pdf covers a lot of the same issues, although only from a Linux perspective:

https://dl.dropboxusercontent.com/u/28343282/MacXPlatform/PascalDynLibs.html

-Phil

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Using C/C++ code in FPC/Lazarus
« Reply #2 on: May 31, 2016, 10:50:18 pm »
« Last Edit: May 31, 2016, 10:56:54 pm by ChrisF »

yellowbit

  • New Member
  • *
  • Posts: 22
Re: Using C/C++ code in FPC/Lazarus
« Reply #3 on: May 31, 2016, 11:44:00 pm »
I have C++ code written in AtmelStudio (AVR GCC) that is a graphic library for LED display. I want to make a PC emulator of this display. This emulator needs to be used as a part of the Windows application written in Lazarus. It's a quite complex code, so rewriting it to pascal is very uncomfortable. This code will be modified in the future, so it will require keeping these 2 branches synchronized.

Maybe a better solution will be wrapping this C++ code into the DLL. It will be possible to build it with very small modification, so merging new things will be much easier than rewriting it to freepascal  :)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Using C/C++ code in FPC/Lazarus
« Reply #4 on: June 01, 2016, 12:58:52 am »
That sounds quite doable. I would suggest putting the C++ code into its own DLL.

In any case, to call C++ from some other language is generally not much different from calling Pascal from some other language. In both cases, you need to "tear down" (flatten) the C++ (or Pascal) classes into exported C compatible functions.

You can also then "build up" classes on the calling side that wrap (and hide) the library functions. This allows you to work with classes that are similar to what you have on the library side. See the referenced dynamic library articles above. The code in the library is a Pascal class, "flattened" to a series of C-like functions. The calling code is a Python wrapper class, C# wrapper class, etc. as needed that calls the library functions.

Note that to keep track of instantiated C++ objects, you'll probably need to pass them to and from library as pointers. This is generally safe as long as you don't do anything else with them on the calling (Pascal) side.

-Phil

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using C/C++ code in FPC/Lazarus
« Reply #5 on: June 01, 2016, 12:56:10 pm »
I have C++ code written in AtmelStudio (AVR GCC) that is a graphic library for LED display. I want to make a PC emulator of this display.
Long, long time ago I had the plan similar to yours. I wanted to create a plugin for AVR Studio 3 in order to simplify graphics development. I even thought about creating glue hardware to physically emulate display and show it's content on pc screen. Then I found E-Lab AvrCo Multitasking Pascal with ICE HW debugger, programmer and simulator and I could instantly visualize all graphic commands. Since then I have never turned back. The beauty is that AvrCo graphic and other libs are not hardware specific, so besides a lot of supported hardware you can use it even for not supported ones if you write your own low level callback functions. AvrCo is not cheap, but there are 3 free limited versions to try: 1) 4kB Flash code where advanced functionality works on every MCU, 2) 8 kB flash limit on just Mega8/Mega48, and 3) 16kB flash limit on just XMega8E5..XMega32E5.

http://forum.lazarus.freepascal.org/index.php/topic,31024.msg198264.html#msg198264
« Last Edit: June 01, 2016, 12:57:57 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

yellowbit

  • New Member
  • *
  • Posts: 22
Re: Using C/C++ code in FPC/Lazarus
« Reply #6 on: June 01, 2016, 05:57:47 pm »
@avra, it's nice piece of software, but I'm afraid that it doesn't fit my needs. The application's purpose is not only development. It's an animation creator for the LED display. Emulation of the physical device is needed to see how exactly the animation will look like, without uploading it to the target device. It must be usable by "ordinary" people.

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Using C/C++ code in FPC/Lazarus
« Reply #7 on: June 02, 2016, 04:17:21 pm »
@avra, it's nice piece of software, but I'm afraid that it doesn't fit my needs. The application's purpose is not only development. It's an animation creator for the LED display. Emulation of the physical device is needed to see how exactly the animation will look like, without uploading it to the target device. It must be usable by "ordinary" people.
You mean my ma? Ordinary programmers will understand Avra's rather good explanation. (And yes, my ma,  85, can program in Delphi and FPC ;) );
Ordinary people won't. ever. Unless they wannaby programmers and try..., just like Mum did... 8-) 25 years ago.
« Last Edit: June 02, 2016, 04:22:56 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018