Tutorial : Using C language code in Ring programs - Sum Two Numbers

274 views
Skip to first unread message

Mahmoud Fayed

unread,
Mar 10, 2019, 4:33:44 AM3/10/19
to The Ring Programming Language
Hello

Tutorial : Using C language code in Ring programs - Sum Two Numbers


In this extension we learn how to create a C function to sum two numbers

In mylib.c we add


RING_FUNC(ring_sumtwonumbers)
{
       
double nNum1,nNum2,nSum;
       
// Check Parameters Count
               
if (RING_API_PARACOUNT != 2) {
                        RING_API_ERROR
(RING_API_MISS2PARA);
                       
return;
               
}
       
// Check Parameters Type
               
if ( ! (RING_API_ISNUMBER(1) && RING_API_ISNUMBER(2)) ) {
                        RING_API_ERROR
(RING_API_BADPARATYPE);
                       
return;
               
}
       
// Sum Numbers
                nNum1
= RING_API_GETNUMBER(1);
                nNum2
= RING_API_GETNUMBER(2);
                nSum  
= nNum1 + nNum2 ;
       
// Return Output
                RING_API_RETNUMBER
(nSum);
}

 

Then we register the new function


ring_vm_funcregister("sumtwonumbers",ring_sumtwonumbers);

 

The previous code is written to check errors, and to be easy to understand

We can write short code like

		
RING_API_RETNUMBER(RING_API_GETNUMBER(1) + RING_API_GETNUMBER(2));


 

The file test.ring contains


? "Loading Library"
load
"mylib.ring"

? "Calling a C Function"
myfunction
()

? "Sum Two Numbers (3,5)"
? SumTwoNumbers(3,5)

 

Then we test the function using


ring test.ring

Output


Loading Library
Calling a C Function
Hello, World!
Sum Two Numbers (3,5)
8

Greetings,

Mahmoud



Bert Mariani

unread,
Mar 10, 2019, 11:14:48 AM3/10/19
to The Ring Programming Language
Hello Mahmoud

Thank you for the C Tutorials.

I am a bit confused.
     Created:  mylib.c    with  your code example
     Created:  test.ring
     Get message:   "Can't open file mylib.ring"


If I create: mylib.ring   with your code example
     C:/MyStuff/mylib.ring Line (2) Syntax error
     C:/MyStuff/mylib.ring Line (3) Syntax error
     C:/MyStuff/mylib.ring Line (20) Syntax error
     C:/MyStuff/mylib.ring errors count : 3

myfunction()  ... where does it come from ?

Is it possible to record and show the complete procedure in a video ?

====================================
Create as  mylib.c  or mylib.ring                     ###  what file name should it be ?

RING_FUNC(ring_sumtwonumbers)                   
{                                                                            ### <<< line 2
        double nNum1,nNum2,nSum;                      ### <<< line 3
        // Check Parameters Count
                if (RING_API_PARACOUNT != 2) {
                        RING_API_ERROR(RING_API_MISS2PARA);
                        return;
                }
        // Check Parameters Type
                if ( ! (RING_API_ISNUMBER(1) && RING_API_ISNUMBER(2)) ) {
                        RING_API_ERROR(RING_API_BADPARATYPE);
                        return;
                }
        // Sum Numbers 
                nNum1 = RING_API_GETNUMBER(1);
                nNum2 = RING_API_GETNUMBER(2);
                nSum  = nNum1 + nNum2 ;
        // Return Output
                RING_API_RETNUMBER(nSum);
}                                                                           ### <<< line 20

ring_vm_funcregister("sumTwoNumbers",ring_sumTwoNumbers);    ### added this line here 

===========================
Create as: test.ring

? "Loading Library"
load "mylib.ring"

? "Calling a C Function"
myfunction()                            ### <<<  where is this defined

? "Sum Two Numbers (3,5)"
? SumTwoNumbers(3,5)

==============================

Mahmoud Fayed

unread,
Mar 10, 2019, 11:48:18 AM3/10/19
to ring...@googlegroups.com
Hello Bert

The steps are :-

(1) Create the C source code in file mylib.c

(2) Create batch file to build the library using Visual C/C++

(3) Run the Batch File (buildvc.bat) to build the mylib.dll file

(4) Create mylib.ring to load the dll library

(5) Create test.ring to use mylib.ring and test the functions

Greetings,
Mahmoud

Bert Mariani

unread,
Mar 10, 2019, 4:32:09 PM3/10/19
to The Ring Programming Language
Hello Mahmoud

I have Windows 10
     c:\ring          directory for Ring
     c:\MyStuff    directory for programs written in Ring

Using copy/paste created
      c:\MyStuff\mylib.c
      c:\MyStuff\buildvc.bat

Get error messages shown below when running that bat file

Does not like the  ../../../
Need to specify   c:\ring\src    c:\ring\include   c:\ring\lib  etc

Can not find "cl" command
Can not  find "link" command

=====================

buildvc.bat   ( in C:\MyStuff)

cls
call ../../../src/locatevc.bat
cl /c /DEBUG mylib.c -I"..\..\..\include"
link /DEBUG mylib.obj  ..\..\..\lib\ring.lib /DLL /OUT:mylib.dll /SUBSYSTEM:CONSOLE,"5.01" 
del mylib.obj

================

c:\MyStuff>buildvc.bat

c:\MyStuff>call ../../../src/locatevc.bat
'..' is not recognized as an internal or external command,
operable program or batch file.

c:\MyStuff>cl /c /DEBUG mylib.c -I"..\..\..\include"
'cl' is not recognized as an internal or external command,
operable program or batch file.

c:\MyStuff>link /DEBUG mylib.obj  ..\..\..\lib\ring.lib /DLL /OUT:mylib.dll /SUBSYSTEM:CONSOLE,"5.01"
'link' is not recognized as an internal or external command,
operable program or batch file.

c:\MyStuff>del mylib.obj
Could Not Find c:\MyStuff\mylib.obj

c:\MyStuff>

==========================


On Sunday, March 10, 2019 at 4:33:44 AM UTC-4, Mahmoud Fayed wrote:

Mahmoud Fayed

unread,
Mar 11, 2019, 1:26:33 AM3/11/19
to The Ring Programming Language
Hello Bert

cl & link are tools related to Visual C/C++

Just open the command prompt with access to Visual C/C++ tools

For Example if you have Visual Studio 2017 Community Edition, You run the next batch file
Path : "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat"

To get the next window, So you can use (cl) and (link) tools

niceshot.png

niceshot2.png


Then you can run buildvc.bat

Greetings,
Mahmoud

Bert Mariani

unread,
Mar 11, 2019, 3:22:46 PM3/11/19
to The Ring Programming Language
Hello Mahmoud

It worked !!! Thanks.
At work have  Windows 7 with Visual C Community edition

For buildvc.bat
I had to run the commands one at a time. It complained that it could not find  ring.h
And specify full path  ----
It really does not like  ../../../  

================

c:\SumTwoNumbers>more buildvc.bat
cls
call c:\ring/src/locatevc.bat
cl /c /DEBUG mylib.c -I" c:\ring\include"
link /DEBUG mylib.obj  c:\ring\lib\ring.lib /DLL /OUT:mylib.dll /SUBSYSTEM:CONSOLE,"5.01"
del mylib.obj

c:\SumTwoNumbers>


================
OUTPUT

Loading Library
Calling a C Function
Hello, World!
Sum Two Numbers (3,5)
8
Say Hello
Hello Mahmoud

======================

**********************************************************************
** Visual Studio 2017 RC Developer Command Prompt v15.0.26127.3
** Copyright (c) 2016 Microsoft Corporation
**********************************************************************

c:\SumTwoNumbers>cl /c /DEBUG mylib.c -I"c:\ring\include
Microsoft (R) C/C++ Optimizing Compiler Version 19.10.24911 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

mylib.c

c:\SumTwoNumbers>

c:\SumTwoNumbers>link /DEBUG mylib.obj  c:\ring\lib\ring.lib /DLL /OUT:mylib.dll /SUBSYSTEM:CONSOLE,"5.01"
Microsoft (R) Incremental Linker Version 14.10.24911.0
Copyright (C) Microsoft Corporation.  All rights reserved.

   Creating library mylib.lib and object mylib.exp

c:\SumTwoNumbers>test.ring

c:\SumTwoNumbers>

=====================================



On Sunday, March 10, 2019 at 4:33:44 AM UTC-4, Mahmoud Fayed wrote:

Mahmoud Fayed

unread,
Mar 11, 2019, 3:27:14 PM3/11/19
to The Ring Programming Language
Hello Bert

Very good news :D
I'm using ../../../  because the extension are inside ring/extensions/tutorial/sumtwonumbers
So using ../../../ from this folder means the Ring folder
And it doesn't work on your machine, because of using another folder

Greetings,
Mahmoud

Bert Mariani

unread,
Mar 11, 2019, 3:27:55 PM3/11/19
to The Ring Programming Language
When trying to run buildvc.bat

=========================
buildvc.bat

cls
call c:\ring/src/locatevc.bat
cl /c /DEBUG mylib.c -I"c:\ring\include"
link /DEBUG mylib.obj  c:\ring\lib\ring.lib /DLL /OUT:mylib.dll /SUBSYSTEM:CONSOLE,"5.01" 
del mylib.obj

OUTPUT

**********************************************************************
** Visual Studio 2017 RC Developer Command Prompt v15.0.26127.3
** Copyright (c) 2016 Microsoft Corporation
**********************************************************************
Microsoft (R) C/C++ Optimizing Compiler Version 19.10.24911 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

mylib.c
mylib.c(1): fatal error C1083: Cannot open include file: 'ring.h': No such file or directory
Microsoft (R) Incremental Linker Version 14.10.24911.0
Copyright (C) Microsoft Corporation.  All rights reserved.

LINK : fatal error LNK1181: cannot open input file 'mylib.obj'
Could Not Find c:\SumTwoNumbers\mylib.obj

c:\SumTwoNumbers>

===========================



On Sunday, March 10, 2019 at 4:33:44 AM UTC-4, Mahmoud Fayed wrote:

Mahmoud Fayed

unread,
Mar 11, 2019, 3:31:14 PM3/11/19
to The Ring Programming Language
Hello Bert

When using
cl /c /DEBUG mylib.c -I"c:\ring\include"

Be sure that you have ring.h in the c:\ring\include folder

Greetings,
Mahmoud

Bert Mariani

unread,
Mar 11, 2019, 3:38:26 PM3/11/19
to The Ring Programming Language
Hello Mahmoud

buildvc.bat   Works !!!
Note the full path and spacing in yellow.

====================
cls
call c:\ring/src/locatevc.bat
cl /c /DEBUG mylib.c -I"c:\ring\include"
link /DEBUG mylib.obj  c:\ring\lib\ring.lib /DLL /OUT:mylib.dll /SUBSYSTEM:CONSOLE,"5.01" 
del mylib.obj

====================


On Sunday, March 10, 2019 at 4:33:44 AM UTC-4, Mahmoud Fayed wrote:

Mahmoud Fayed

unread,
Mar 12, 2019, 7:24:23 AM3/12/19
to The Ring Programming Language
Hello Bert

Very good news, Keep up the good work :D

Greetings,
Mahmoud

Bert Mariani

unread,
Mar 14, 2019, 10:06:18 PM3/14/19
to The Ring Programming Language
Hello Mahmoud

Re: You can write a C function that do the calculations then return a List of points 
       Then write a Ring function that draw the points from this list

Please see code below.

I am defining i
In Ring>>   myList = newList(width,Height)    for the Pixel Points on the screen
In C>>       arrayM = RING_API_GETCPOINTER(8,  ??? );  

How do you pass / define  a Pointer to myList  from Ring  to  C  , so C can use it to as  arrayM[x][y]  ?

====================

RING_FUNC(ring_mandel)
{

printf("Mandel called \n" );
    
    double minI   = -2.0, maxI = 2.0, minR = -2.0, maxR = 2.0 ;
    double stepR  = 0, stepI  = 0 ; 
    double pointI = 0, pointR = 0 ;
    double zI = 0, zR = 0 ;
    double a  = 0, b  = 0 ;

    int    width = 400,  height = 400 ;
    int    iter  = 50 ;
    int    x = 0, y = 0, n = 0 ;
    
    int    arrayM[60][60]  ;

// test.ring
        //   ? "Mandelbrot Calc"
        //   width  = 52
        //   height = 54
        //   iter   = 56
        //   minI   = -1.71  maxI = 1.72  minR = -1.63  maxR = 1.67
        //   myList = newList(width,Height)
        //   myList = mandel(minI,maxI,minR,maxR,iter,width,height,myList)

    minI   = RING_API_GETNUMBER(1);
    maxI   = RING_API_GETNUMBER(2);
    minR   = RING_API_GETNUMBER(3);
    maxR   = RING_API_GETNUMBER(4);
    iter   = RING_API_GETNUMBER(5);
    width  = RING_API_GETNUMBER(6);
    height = RING_API_GETNUMBER(7);
    //arrayM = RING_API_GETCPOINTER(8, "void *char" );  <<<< how is this coded ? Pointer to arrayM[][]
    
    
printf("Vars: \nminI: %10.9f maxI: %10.9f \nminR: %10.9f maxR: %10.9f iter: %d \nwidth: %d height: %d \n\n", 
              minI,maxI,minR,maxR,iter,width,height ) ; // &arrayM )
 

==============================
From Ring to C,  parameters passed

Vars:
minI: -1.710000000 maxI: 1.720000000
minR: -1.630000000 maxR: 1.670000000 iter: 56
width: 52 height: 54

====================

On Sunday, March 10, 2019 at 4:33:44 AM UTC-4, Mahmoud Fayed wrote:
mylib.c
test.ring
mylib.ring

Mahmoud Fayed

unread,
Mar 15, 2019, 5:33:28 AM3/15/19
to The Ring Programming Language
Hello Bert

Nice question, Just wait until I finish the tutorials that explain Ring API for C Extensions step by step

So you can understand everything about the API and the different ways to do things based on your need.

Keep the questions in the tutorial scope, and once we finish the tutorials you will know everything already and we can work together in different extensions.

Greetings,
Mahmoud
Reply all
Reply to author
Forward
0 new messages