Using c code (re:C Static library experiment )

BlitzMax Forums/BlitzMax Programming/Using c code (re:C Static library experiment )

Jim Teeuwen(Posted 2006) [#1]
In followup to a thread Surreal started once (googled it). I am trying to get some basic c functions to work in blitzmax.

- wrote and compiled the code with MingW 3.1 as is said in the thread by Mark Sibly.
- I followed the steps lined out to create the .a file.
- Made the mod directory, the according .bmx file that provides the Extern block. Note: the mod directory is not in the BRL or PUB dir, but straight in the Mods root.
- ran bmk -makemods. it created the needed .i files.
- placed an Import jt.utilities in my game code (name of the mod as I have defined it)

Now my game compiles without giving the nasty 'Could not find Interface to jt.utilities' error, but it fails somewhere in the Linking process saying that the functions I defined in the mod can not be found.

I checked and doublechecked the profile for the functions as they are in the c files and in the .bmx file and they seem to match just fine. Why wouldnt this work?

the c-code
#include <windows.h>

static long util_GetTickCount()
{
	return GetTickCount();
}
static byte util_QueryPerformanceCounter( long* lpPerformanceCount )
{
	return QueryPerformanceCounter( (LARGE_INTEGER*) lpPerformanceCount) ? 1 : 0;
}
static byte util_QueryPerformanceFrequency( long* lpFrequency )
{
	return QueryPerformanceFrequency( (LARGE_INTEGER*) lpFrequency) ? 1 : 0;
}


bmx code in module
Module jt.utilities
Strict

Import "utilities.a"

Extern "Win32"
	Function util_GetTickCount:Long()
	Function GetTickCount:Long() = "util_GetTickCount"

	Function util_QueryPerformanceCounter:Byte( lpPerformanceCount:Long  Var )
	Function QueryPerformanceCounter:Byte( lpPerformanceCount:Long Var ) = "util_QueryPerformanceCounter"

	Function util_QueryPerformanceFrequency:Byte( lpFrequency:Long Var )
	Function QueryPerformanceFrequency:Byte( lpFrequency:Long Var) = "util_QueryPerformanceFrequency"

End Extern



The module directory structure looks like:
<blitzmax>\mod\jt.mod\utilities.mod\


The following files are present there:
 - utilities.a
 - utilities.bmx
 - utilities.debug.win32.x86.a
 - utilities.debug.win32.x86.i
 - utilities.release.win32.x86.a
 - utilities.release.win32.x86.i


The buildlog I get when compiling my game code that references the mod is:
...
Linking:Entry.exe
D:/Files/Coding/BlitzMax/SpriteLib/.bmx/Entry.bmx.console.release.win32.x86.o(code+0x35e7): undefined reference to `util_QueryPerformanceFrequency'
D:/Files/Coding/BlitzMax/SpriteLib/.bmx/Entry.bmx.console.release.win32.x86.o(code+0x362e): undefined reference to `util_QueryPerformanceCounter'
Build Error: Failed to link D:/Files/Coding/BlitzMax/SpriteLib/Entry.exe
Process complete


Any ideas? as I am out of em atm..

ps: the Extern block has "Win32" nehind it. I tried it without that but the same error occurs.


Byteemoz(Posted 2006) [#2]
I'm not sure why you used/included an .a file or declared the functions static ... but this works:
utilities.c :
#include <windows.h>

long util_GetTickCount()
{
	return GetTickCount();
}
byte util_QueryPerformanceCounter( long* lpPerformanceCount )
{
	return QueryPerformanceCounter( (LARGE_INTEGER*) lpPerformanceCount) ? 1 : 0;
}
byte util_QueryPerformanceFrequency( long* lpFrequency )
{
	return QueryPerformanceFrequency( (LARGE_INTEGER*) lpFrequency) ? 1 : 0;
}


utilities.bmx
Module jt.utilities
Strict

Import "utilities.c"

Extern
	Function GetTickCount:Long() = "util_GetTickCount"

	Function QueryPerformanceCounter:Byte( lpPerformanceCount:Long Var ) = "util_QueryPerformanceCounter"

	Function QueryPerformanceFrequency:Byte( lpFrequency:Long Var) = "util_QueryPerformanceFrequency"

End Extern


a test:
Import jt.utilities


Print GetTickCount()

Local pc:Long
Print QueryPerformanceCounter( pc )
Print pc

Print QueryPerformanceFrequency( pc )
Print pc


and the result:
Building untitled4
Compiling:untitled4.bmx
flat assembler  version 1.64
3 passes, 2433 bytes.
Linking:untitled4.exe
Executing:untitled4.exe
10132240170483508
1
374547490944
1
3579545

Process complete


-- Byteemoz


Jim Teeuwen(Posted 2006) [#3]
awesome, thanks!