How to use functions from C *.lib files?

New to FreeBASIC? Post your questions here.
diakin
Posts: 89
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

How to use functions from C *.lib files?

Postby diakin » May 15, 2008 7:58

Hi!

I have a lib file (*.lib) written on с (++?).
How can I use functions from this C *.lib file?

There are *.h header file where function declared as

Code: Select all

int32 __CFUNC DAQmxSaveScale (const char scaleName[], const char saveAs[], const char author[], uInt32 options);
int32 __CFUNC  DAQmxDeleteSavedTask (const char taskName[]);

...etc


Can I use (link) this static lib directly from FreeBasic application or I need to create wrapper (dll) on C?


WBR, Andrew
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Postby dani.user » May 15, 2008 10:13

You need to translate the header file.
diakin
Posts: 89
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Postby diakin » May 15, 2008 10:17

Thanks.
I also found this reply

http://www.freebasic.net/forum/viewtopi ... extern+lib

possible it help
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » May 15, 2008 11:20

I think the declarations for the two examples would need to be something like this:

Code: Select all

declare function DAQmxSaveScale cdecl alias "DAQmxSaveScale"( _
            byval scaleName as zstring ptr, _
            byval saveAs as zstring ptr, _
            byval author[] as zstring ptr, _
            byval options as uinteger ) as integer

declare function DAQmxDeleteSavedTask cdecl alias "DAQmxDeleteSavedTask" ( _
            byval taskName as zstring ptr ) as integer
diakin
Posts: 89
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Postby diakin » May 19, 2008 13:11

Well, I have a problem..
I rename NIDAQmx.lib into libNIDAQmx.a
and add #inclib "NIDAQmx" in bas file. Works fine.

In NIDAQmx.h there are

typedef uInt32 TaskHandle;
int32 __CFUNC DAQmxCreateTask (const char taskName[], TaskHandle *taskHandle);

I write this

Code: Select all

#inclib "NIDAQmx"

Declare Function DAQmxCreateTask Cdecl Alias "DAQmxCreateTask" ( _
            Byval taskName As Zstring Ptr, byref taskHandle as Uinteger ) As Integer



Error
C:\bas\RapidQ\RQIDE\FrreBasicLibTest1.o:fake:(.text+0x67): undefined reference to `DAQmxCreateTask'

What can be wrong?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » May 19, 2008 14:53

Try opening the library in a text editor, searching for the name 'DAQmxCreateTask' and then take note of the exact format of the name, including the case and any prefix or suffix.
diakin
Posts: 89
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Postby diakin » May 19, 2008 16:18

Yes, I already try _DAQmxCreateTask... without success

In text I found
_DAQmxCreateTask@8
__imp__DAQmxCreateTask@8
_DAQmxCreateTask@8[ascii0]nicaiu.dll

tomorrow I will try test it again..

This is part of Unit1.cpp file that works fine.

Code: Select all

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include "NIDAQmx.h"
#include "Unit3.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TaskHandle  MyTaskHandle=0;
/////////
if (DAQmxCreateTask("",&MyTaskHandle)!=0) ShowMessage("Connection error");
else{....


//////////////////



Thank you.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » May 20, 2008 5:11

AFAIK the @8 on the end of the name means that the function uses the STDCALL convention, so you need to remove the CDECL from the declaration. You still need the alias name because FBC will otherwise place an upper case version of the name in the object module, and the linker will not be able to match that with the case-sensitive name in the library. The name in the “undefined reference to” error message is the name in the object module, that the linker is trying to match to the name in the library, and in this case I think it’s supposed to be “_DAQmxCreateTask@8”. Given the alias name “DAQmxCreateTask” and the default STDCALL convention, FBC should add the leading underscore and the @8 suffix. Once you have satisfied the linker, when you run the app if the function is actually in a DLL, and the system cannot find the DLL, then the system will return an error message.
diakin
Posts: 89
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Postby diakin » May 20, 2008 10:04

Declare Function DAQmxCreateTask Alias "DAQmxCreateTask" ( _
Byval taskName As Zstring Ptr, byref taskHandle as Uinteger ) As Integer
....
otv= DAQmxCreateTask(str2,MyTaskHandle)
....

Yes, in asm listing _DAQmxCreateTask@8 presents in this case

Code: Select all

push eax
push dword ptr [ebp-8]
call _DAQmxCreateTask@8
mov dword ptr [ebp-28], eax
push 0
push 4


but the same error
FBLibCall.o:fake:(.text+0xa8): undefined reference to `DAQmxCreateTask@8'

Also I rename in lib file all DAQmxCreateTask to uppercase
DAQMXCREATETASK in Hex editor (file size was not changed)
but the same error...
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » May 20, 2008 16:47

At this point I’m not sure what the problem is. Judging from this, DAQmxCreateTask is a function in nicaiu.dll, so it looks like the library you are using is an import library for nicaiu.dll. If you have nicaiu.dll, you can eliminate the import library as the source of the problem by generating your own import library, directly from nicaiu.dll. To do this you first create a module definition (.def) file, and then create the import library from the .def file.

To create a .def file from nicaiu.dll you can use the MinGW utility pexports, included with the MinGW installation (in the bin directory), or available as part of the MinGW utilities here. Assuming that nicaiu.dll and pexports.exe are in the current directory, you can create the .def file with a batch file like this:

Code: Select all

pexports nicaiu.dll > nicaiu.dll.def
pause

The module definition file is a text file, so you can open it in any editor to see the contents. Once you have nicaiu.def you can create the import library with the dlltool utility in the FB bin\win32 directory, using a batch file like this (the -k option will kill the @n suffix on the end of the names):

Code: Select all

dlltool -k -d nicaiu.dll.def -l libnicaiu.dll.a

And don’t forget to change the #inclib in your header file to:

#inclib nicaiu
wallyfblu
Posts: 69
Joined: May 24, 2006 10:58
Location: ITALY

Postby wallyfblu » May 21, 2008 8:58

I tried to use a .lib written in MASM and the problem is the other library used. i.e kernel.lib etc. . There's a way to use this static library or need I the others .lib?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » May 22, 2008 9:33

I’m not sure what you mean by “kernel.lib”, but there should be no problem with static libraries that call functions from the common system DLLs, e.g. kernel32.dll, user32.dll, etc. With the library in the current directory, you should be able to just change the .lib extension to .a, add an appropriate #inclib statement at the top of your source (that specifies the library base name with no extension) and proper declarations for the library functions/procedures that you will call. Don’t forget to specify the correct case-sensitive name in an alias clause for each declaration, or leave off the alias clause, specify the correct case-sensitive names in the declarations, and place all of the declarations in an extern "windows" block so the case of the names will be preserved.
diakin
Posts: 89
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Postby diakin » May 22, 2008 14:13

MichaelW wrote:At this point I’m not sure what the problem is. Judging from this, DAQmxCreateTask is a function in nicaiu.dll, so it looks like the library you are using is an import library for nicaiu.dll.
{.....}
And don’t forget to change the #inclib in your header file to:

#inclib nicaiu


Thank you very much Michael!
Yes it's lib for NI measuring board. I will try to find and use nicaiu.dll.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » May 22, 2008 14:26

I'm not sure if this will help, but I found a similar topic on the NI forum that has possible solutions.

http://forums.ni.com/ni/board/message?board.id=250&view=by_date_ascending&message.id=12154
diakin
Posts: 89
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Postby diakin » May 22, 2008 14:27

Wow!
I found nicaiu.dll deep in archive and now all seems working!

Thanks again!

WBR, Andrew

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests