Qbasicnews.com

Full Version: Problem with C function in compiled qb exe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I wrote small routine in C(borland Turbo C/C++ 3.1). Made an .OBJ file from it and linked with VBDCL10E.LIB into funk.lib file, from what I made a .qlb file.

C function: (compiled using medium model(386 code generation)
Code:
int far ontarget(int mx, int my, int xpos, int ypos, int x1pos, int y1pos)
{
    if (mx < xpos) return 0;
    if (mx > x1pos) return 0;
    if (my < ypos) return 0;
    if (my > y1pos) return 0;
    return -1;
}

In qb everything works just fine. Made even small test program to see wich is faster. Just in case here's qbasic code:
Code:
DEFINT A-Z
DECLARE FUNCTION ontarget CDECL (BYVAL mx, BYVAL my, BYVAL Xpos, BYVAL Ypos, X1pos, BYVAL Y1pos)
DECLARE FUNCTION OnTarget2 (BYVAL mx, BYVAL my, BYVAL Xpos, BYVAL Ypos, X1pos, BYVAL Y1pos)
CLS
PRINT "C function"
PRINT ontarget(100, 100, 50, 50, 150, 150)
PRINT ontarget(100, 10, 50, 50, 150, 150)

StartTest1! = TIMER
   FOR i = 1 TO 32000
      FOR m = 1 TO 1000
         a = ontarget(100, 100, 50, 50, 150, 150)
      NEXT m
   NEXT
EndTest1! = TIMER
PRINT EndTest1! - StartTest1!

PRINT "qb function"
PRINT ontarget(100, 100, 50, 50, 150, 150)
PRINT ontarget(100, 10, 50, 50, 150, 150)

StartTest1! = TIMER
   FOR i = 1 TO 32000
      FOR m = 1 TO 1000
         a = OnTarget2(100, 100, 50, 50, 150, 150)
      NEXT m
   NEXT
EndTest1! = TIMER
PRINT EndTest1! - StartTest1!

FUNCTION OnTarget2 (BYVAL mx, BYVAL my, BYVAL Xpos, BYVAL Ypos, X1pos, BYVAL Y1pos)
IF mx < Xpos THEN EXIT FUNCTION
IF my < Ypos THEN EXIT FUNCTION
IF mx > X1pos THEN EXIT FUNCTION
IF my > Y1pos THEN EXIT FUNCTION
OnTarget2 = -1
END FUNCTION
(C function completes test with smth of 12.xxx seconds and qb's function takes 20.xxx seconds :wink: )

Now the problem is when I compile it into exe. (using /g3 switch(it's VBDOS))

It compiles without errors. Everything is fine. But when I ran the test, it seems that C function doesn't return any value. It just returns 0 whenever function is called. Dunno why is it so. Did I compile smth wrong?
I might be wrong, but aren't return values passed in the ax register as opposed to being passed on the stack? And I think the return command in C returns the value on the stack.
You have to declare the routine as cdecl expliciltly, otherwise borland will use its own calling convention.
Hey Von Godric, what is the runtime libe VBdDOS use to make QLB's?
VBDOSQLB.LIB
VBDCL10E.LIB
lol... They have different versions?

Sorry about that rel.
Dude, did you try what i said?
Sry ... not yet. I had some problems with win98... Dunno I guess installer was corrapted or smth... well anyway I'll try and let you know.
Tnx
nope Cry

I put:
Code:
int far pascal ontarget(...)
and had to remove cdecl statament in qb code. Again works fine in qb ide, but doesn't as soon as I compile the program and try to run it.
Pages: 1 2