The MASM Forum

General => The Campus => Topic started by: hexbase on December 20, 2012, 01:52:26 AM

Title: C++ static library usage
Post by: hexbase on December 20, 2012, 01:52:26 AM
Hi,
Im a newbie in asm and in programming in general.
I wanted to use re2win (http://code.google.com/p/re2win/) library. I added a new sourcefile in the project and created a function to be called in masm. Something like:
Code: [Select]
extern "C" bool __stdcall atestfunc (int first, int second){
}
Then I compiled it as static library with no problem.
Then I created a .inc file to use the function in masm and then included the lib.
When building, I get this:
Code: [Select]
POLINK: fatal error: Corrupt library: '\masm32\lib\re2.lib'; unable to find symb
ol '_atestfunc@8' in member 'Release\test.obj'.

Dumpbin /SYMBOLS doesnt show any symbol in the lib.

Thanks
Title: Re: C++ static library usage
Post by: dedndave on December 20, 2012, 02:11:46 AM
are you trying to use an OBJ as a LIB ?
you can put the OBJ inside a LIB
a LIB is a collection of OBJ's, essentially
i guess 1 is a collection   :P

otherwise, you can directly link the OBJ with the OBJ of the test project
Title: Re: C++ static library usage
Post by: dedndave on December 20, 2012, 02:17:53 AM
http://www.masmforum.com/board/index.php?topic=17669.msg148924#msg148924 (http://www.masmforum.com/board/index.php?topic=17669.msg148924#msg148924)

http://www.masmforum.com/board/index.php?topic=15480.0 (http://www.masmforum.com/board/index.php?topic=15480.0)

i used the ms linker - the switches may be different for polink
Title: Re: C++ static library usage
Post by: qWord on December 20, 2012, 02:26:49 AM
Hello,
your problem is probably the name mangling of C++ (VC++?) - the this document (http://www.agner.org/optimize/calling_conventions.pdf) for more details.

qWord

EDIT: I didn't see you are using extern "C"
Title: Re: C++ static library usage
Post by: Adamanteus on December 20, 2012, 03:00:37 AM
Maybe extrn C allowed only in decalration - first, implamentation - second, not suppose usnig this keyword.
Title: Re: C++ static library usage
Post by: MichaelW on December 20, 2012, 03:27:05 AM
I'm guessing that the problem is that in your cpp source you have declared atestfunc but not defined it.

I don’t have VC set up on my system, so I used the VC Toolkit 2003 compiler and batch files.

As a quick check of the symbols in a library, instead of taking the time to use Dumpbin or similar just open the library in a text editor and look at the names.
Title: Re: C++ static library usage
Post by: japheth on December 20, 2012, 03:48:52 AM
There might be several problems:

- your lib was created for 64-bit
- your lib is in Unix AR format ; this format is not quite compatible with the MS AR format.
- your lib might be "empty" - you are "new to programming in general" and may have done something stupid
  (don't worry, lots of people here are doing this virtually all the time  :bgrin: )

The simplest is to zip your lib and post it here - if it isn't too large.

Title: Re: C++ static library usage
Post by: qWord on December 20, 2012, 04:11:08 AM
I've build the lib and was also unable to get it link - maybe because of the project settings there are a lot of dependencies that I wasn't able to fix. However, I was able to build a DLL of it (simple add __declspec(dllexport) to the function / classes you want to export).
Title: Re: C++ static library usage
Post by: Vortex on December 20, 2012, 05:25:54 AM
Hi hexbase,

Welcome to the forum.

Could you post here your library? ( attachment )
Title: Re: C++ static library usage
Post by: jj2007 on December 20, 2012, 05:41:28 AM
Erol,

The lib is 23MB (of which 755184 bytes dedicated to "stringprintf.obj" ;))

By the way, to my great surprise, Visual C++ 2010 Express was able to convert it, and even compiled it.
Title: Re: C++ static library usage
Post by: Vortex on December 20, 2012, 05:48:04 AM
Hi Jochen,

Very big library. Sorry, I didn't knew it.
Title: Re: C++ static library usage
Post by: hexbase on December 20, 2012, 08:16:22 AM
Hi guys,

I was able to build it as a dll. I just selected to build it as dll in the project settings.  I added a test function called "matchparcial", like
Code: [Select]
extern "C" bool __declspec(dllexport) __stdcall matchparcial (string *first, string *second){
}

Dumpbin shows the function as exported. How do I use the dll inside my program?
Attached is the dll and dumpbin info (not all of it).
The dll is small, compared to the lib file. I dont know if that's ok.

Thanks
Title: Re: C++ static library usage
Post by: jj2007 on December 20, 2012, 08:43:32 AM
How do I use the dll inside my program?

From C or assembler?

Here is one option:

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Dll "\RE2\re2.dll"                        ; load the DLL
  Declare matchparcial, 2        ; matchparcial expects 2 pointers
  Inkey Str$("Result=%i", matchparcial("String1", "String2"))
  Exit
end start

It assembles, links and runs - and then stops complaining that it can't find GetTickCount64 in Kernel32.dll ...
Title: Re: C++ static library usage
Post by: Farabi on December 20, 2012, 09:33:40 AM
How do I use the dll inside my program?

From C or assembler?

Here is one option:

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Dll "\RE2\re2.dll"                        ; load the DLL
  Declare matchparcial, 2        ; matchparcial expects 2 pointers
  Inkey Str$("Result=%i", matchparcial("String1", "String2"))
  Exit
end start

It assembles, links and runs - and then stops complaining that it can't find GetTickCount64 in Kernel32.dll ...

Whoa your MASM BASIC is great :t
Title: Re: C++ static library usage
Post by: hexbase on December 20, 2012, 10:00:33 AM
How do I use the dll inside my program?

From C or assembler?

Here is one option:

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Dll "\RE2\re2.dll"                        ; load the DLL
  Declare matchparcial, 2        ; matchparcial expects 2 pointers
  Inkey Str$("Result=%i", matchparcial("String1", "String2"))
  Exit
end start

It assembles, links and runs - and then stops complaining that it can't find GetTickCount64 in Kernel32.dll ...

I put re2.dll in the masm32 folder and used: Dll "\masm32\re2.dll". When I run the program, it complains it cannot load the dll (loadlibrary). What im doing wrong?
Title: Re: C++ static library usage
Post by: qWord on December 20, 2012, 11:05:51 AM
I've no clue why we need to use third party macros whereas MASM and VC supplies everything we need:
Just search in the Release folder for the import library re2.lib and copy this (and the DLL) to your project folder (MASM)
ASM:
Code: [Select]
includelib re2.lib
matchparcial proto STDCALL first:PVOID, second:PVOID
...
invoke matchparcial ,0,0
Also note that "string"-types are objects. You may change the paramters to char*.
Title: Re: C++ static library usage
Post by: jj2007 on December 20, 2012, 05:27:53 PM
I put re2.dll in the masm32 folder and used: Dll "\masm32\re2.dll". When I run the program, it complains it cannot load the dll (loadlibrary). What im doing wrong?

No idea. Check the folder, or use this to test:
  Init
  Let esi="\RE2\re2.dll"
  .if !Exist(esi)
        MsgBox 0, esi, "Dll not found:", MB_OK
  .else
        Dll "\RE2\re2.dll"                        ; load the DLL
        Declare matchparcial, 2        ; matchparcial expects 2 pointers
        Inkey Str$("Result=%i", matchparcial("String1", "String2"))
  .endif
  Exit


I've no clue why we need to use third party macros...

So did you get it running? No complaints about GetTickcount64? On a Win7-64 system I suppose?
Title: Re: C++ static library usage
Post by: qWord on December 20, 2012, 06:31:24 PM
So did you get it running? No complaints about GetTickcount64? On a Win7-64 system I suppose?
No need to try it - he does not supplies a constructor for the class "string".
He should do all the OOP stuff in the DLL and use only none-object types for the function interface.
Title: Re: C++ static library usage
Post by: qWord on December 20, 2012, 07:32:32 PM
Works perfectly here (Win7,x64) - example attached.

EDIT: msvcrt.dll dependency removed.
Title: Re: C++ static library usage
Post by: jj2007 on December 20, 2012, 08:26:30 PM
The problem is somewhere in the original dll & lib. I got it "working", after obeying to messages requesting msvcp110.dll and msvcr110.dll, but then GetProcAddress did not find matchparcial in the dll. So I launched PeView, and matchparcial is clearly present as _matchparcial@8, it can be called with its ordinal value #1:

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Dll "\RE2\re2.dll"                        ; load the DLL
  Declare #1=matchparcial, 2        ; matchparcial expects 2 pointers
  Print Str$("Result=%i", matchparcial("String1", "String"))
  Exit
end start

... but the output is crappy:
re2\re2.cc:173: Error parsing 'String  Result=%i
ü@ Oü@ xü@ Öü@ ­©@ GetLastError...': invalid UTF-8
re2\re2.cc:768: Invalid RE2: invalid UTF-8
Result=0


In contrast, qWord's dll and lib work like a charm, both via the static lib and via the dll with ordinal #37:
Version 1, static only (but dll must be present!):
include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
includelib re2.lib
FullMatch PROTO :DWORD, :DWORD
  Init
  Print Str$("Result=%i", rv(FullMatch, "hello", "h.*o"))
  Exit
end start

Version 2, dynamically linked (but lib must be present!):
include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
includelib re2.lib
  Init
  Dll "re2.dll"                        ; load the DLL
  Declare #37=FullMatch, 2        ; ordinal works; matchparcial expects 2 pointers
  ; Declare FullMatch, 2          ; name not found in dll
  Print Str$("Result=%i", FullMatch("hello", "h.*o"))
  Exit
end start

Output for both:
Result=1


And both versions do not require the msvcp110.dll crap. So qWord, C++ jedi knight, how did you manage to reduce re2.lib from over 23MB to a mere 13,480 bytes?  :t

And why does GetProcAddress not find the FullMatch? Knowing that you don't trust "third party macros", here a plain Masm32 snippet:

include \masm32\include\masm32rt.inc
includelib re2.lib
.code
start:
   invoke LoadLibrary, chr$("re2.dll")
   .if eax
      xchg eax, ebx
      invoke GetProcAddress, ebx, chr$("FullMatch")
      MsgBox 0, str$(eax), "Hi", MB_OK
      invoke FreeLibrary, ebx
   .else
      MsgBox 0, "Could not load", "Hi", MB_OK
   .endif
   exit
end start


Edit: Mystery solved. It's Christmas time, so FullMatch needs the full decoration :badgrin:

  Declare _FullMatch@8, 2          ; that one works...
  Print Str$("Result=%i", _FullMatch@8("hello", "h.*o"))
Title: Re: C++ static library usage
Post by: qWord on December 20, 2012, 09:21:10 PM
So qWord, C++ jedi knight, how did you manage to reduce re2.lib from over 23MB to a mere 13,480 bytes?
the compiler discarded everything that is not used by the function RE2::FullMatch.

BTW: the word Padawan may be more appropriate
Title: Re: C++ static library usage
Post by: jj2007 on December 20, 2012, 09:37:46 PM
BTW: the word Padawan may be more appropriate

Un Padawan, allievo Padawan, o Apprendista Jedi in lingua Basic (http://it.starwars.wikia.com/wiki/Padawan)
Welcome on board :icon14:
Title: Re: C++ static library usage
Post by: hexbase on December 21, 2012, 04:34:19 AM
Thanks guys for the help. It works perfectly now. I need to learn more about c++, my code is not good.
Title: Re: C++ static library usage
Post by: Gunther on December 22, 2012, 09:38:14 PM
Hi hexbase,

welcome to the forum.

Gunther