News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

[SOLVED] Using a DLL

Started by TheLastArrived, May 12, 2008, 05:00:42 PM

Previous topic - Next topic

TheLastArrived

Hello all,
I don't know if this is the right place so I apologize if it's not.

I've to write an application that uses function from a dll but when I try to build a
undefined reference to `_Z11TOpenDesignllPKcP10DbxContext@16'|
error message appears.

I've tried to tell C::B where to find the function but without success.

Any hints?

thomas

Too little information, but these are general things that can go wrong:
1. Name mangling may bite you if you use C++. Most DLLs export C functions, try using extern "C". Unless you forgot to link the library, this is the most common cause of symbol-related problems.
2. You should add the import library (if you use MinGW you can also direcly link to a DLL) to the linker's "link library" field
3. Name rape may affect you if you have Windows headers or something similarly abusive (wxWidgets) included. Sometimes, you will find stuff like #define new or #define GetMessage in such headers. Unlikely in your case, but something to always consider.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

TheLastArrived

Thank you for helping.

Let's try to be less vague.

I've to write an interface to a program, the program vendor gives a DLL and a header file to develop interface applications (too bad the vendor is no more accessible so no support available).

I create a std new wxWidget program, I include the header file then I use a function of the DLL  (TOpenDesign) in event handler and I get
-------------- Build: Debug in DBXProva02 ---------------

Linking executable: bin\Debug\DBXProva02.exe
obj\Debug\DBXProva02Main.o: In function `ZN15DBXProva02Frame9OnNewFileER20wxFileDirPickerEvent':
D:/Sources/SCE/DBX Utilities/DBXProva02/DBXProva02Main.cpp:81: undefined reference to `_Z11TOpenDesignllPKcP10DbxContext@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings


Quite right, there is no reference to libraries, so I add the dll to "link libraries" list, retry building an get

Linking executable: bin\Debug\DBXProva02.exe
D:\Programmi\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -lDbx32.dll
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings


It seems that the dll is built (and to be used) with MS VC++ (i'm using MinGW compiler) so I'm trying using pexports, dlltool and other tool to create an import library but I got no success yet.

There is something I'm missing?

thomas

QuoteIt seems that the dll is built (and to be used) with MS VC++ (i'm using MinGW compiler) so I'm trying using pexports
That works, but shouldn't even be necessary. I always link to MSVC DLLs directly, works every time.

QuoteLinking executable: bin\Debug\DBXProva02.exe
D:\Programmi\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -lDbx32.dll
It can't find the library. Try giving it an absolute path (or project root relative), if nothing else helps.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

MortenMacFly

Quote from: TheLastArrived on May 13, 2008, 12:08:25 PM
There is something I'm missing?
Enabling the full compiler log might help (see my sig).This may give you some more hints, e.g. a missing / wrong include path for the linker (not the compiler!).
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

TheLastArrived

Quote from: thomas on May 13, 2008, 12:38:27 PM
QuoteIt seems that the dll is built (and to be used) with MS VC++ (i'm using MinGW compiler) so I'm trying using pexports
That works, but shouldn't even be necessary. I always link to MSVC DLLs directly, works every time.

well, it's quite an old DLLs maybe that's the problem

Quote from: thomas on May 13, 2008, 12:38:27 PM
QuoteLinking executable: bin\Debug\DBXProva02.exe
D:\Programmi\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -lDbx32.dll
It can't find the library. Try giving it an absolute path (or project root relative), if nothing else helps.

If I give an absolute path, C::B finds the lib but the undefined reference problem returns

-------------- Build: Debug in DBXProva02 ---------------

Linking executable: bin\Debug\DBXProva02.exe
obj\Debug\DBXProva02Main.o: In function `ZN15DBXProva02Frame9OnNewFileER20wxFileDirPickerEvent':
D:/Sources/SCE/DBX Utilities/DBXProva02/DBXProva02Main.cpp:81: undefined reference to `_Z11TOpenDesignllPKcP10DbxContext@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
1 errors, 0 warnings


Same problem if I link the import library (lib<name>.a) made with dlltool  or even if I link both the DLL and the .a :(
Missing function TOpenDesign is defined in header file aslong __stdcall TOpenDesign(/* args list*/);
so I've tried dlltool with and without -U switch

TheLastArrived

Quote from: MortenMacFly on May 13, 2008, 02:17:12 PM
Enabling the full compiler log might help (see my sig).This may give you some more hints, e.g. a missing / wrong include path for the linker (not the compiler!).

Right, here you are
Dbx32.dll only linked:
-------------- Build: Debug in DBXProva02 ---------------

mingw32-g++.exe -Wall -pipe -mthreads  -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE  -g -D__WXDEBUG__    -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\contrib\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud  -c "D:\Sources\SCE\DBX Utilities\DBXProva02\DBXProva02Main.cpp" -o obj\Debug\DBXProva02Main.o
mingw32-g++.exe -Wall -pipe -mthreads  -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE  -g -D__WXDEBUG__    -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\contrib\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud  -c "D:\Sources\SCE\DBX Utilities\DBXProva02\GUIFrame.cpp" -o obj\Debug\GUIFrame.o
windres.exe -i D:\Sources\SCE\DBXUTI~1\DBXPRO~1\resource.rc -J rc -o obj\Debug\resource.res -O coff -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud
mingw32-g++.exe -Wall -pipe -mthreads  -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE  -g -D__WXDEBUG__    -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\contrib\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud  -c "D:\Sources\SCE\DBX Utilities\DBXProva02\DBXProva02App.cpp" -o obj\Debug\DBXProva02App.o
mingw32-g++.exe -LD:\Programmi\wxWidgets2.8\lib\gcc_dll  -o bin\Debug\DBXProva02.exe obj\Debug\DBXProva02Main.o obj\Debug\GUIFrame.o obj\Debug\DBXProva02App.o  obj\Debug\resource.res   -lwxmsw28ud "D:\Sources\SCE\DBX Utilities\DBXProva02\Dbx32.dll"  -mwindows
obj\Debug\DBXProva02Main.o: In function `ZN15DBXProva02Frame9OnNewFileER20wxFileDirPickerEvent':
D:/Sources/SCE/DBX Utilities/DBXProva02/DBXProva02Main.cpp:81: undefined reference to `_Z11TOpenDesignllPKcP10DbxContext@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 24 seconds)
1 errors, 0 warnings


with libDbx32.a (-U switch) only linked:
-------------- Build: Debug in DBXProva02 ---------------

mingw32-g++.exe -Wall -pipe -mthreads  -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE  -g -D__WXDEBUG__    -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\contrib\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud  -c "D:\Sources\SCE\DBX Utilities\DBXProva02\DBXProva02Main.cpp" -o obj\Debug\DBXProva02Main.o
mingw32-g++.exe -Wall -pipe -mthreads  -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE  -g -D__WXDEBUG__    -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\contrib\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud  -c "D:\Sources\SCE\DBX Utilities\DBXProva02\GUIFrame.cpp" -o obj\Debug\GUIFrame.o
windres.exe -i D:\Sources\SCE\DBXUTI~1\DBXPRO~1\resource.rc -J rc -o obj\Debug\resource.res -O coff -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud
mingw32-g++.exe -Wall -pipe -mthreads  -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE  -g -D__WXDEBUG__    -ID:\Programmi\wxWidgets2.8\include -ID:\Programmi\wxWidgets2.8\contrib\include -ID:\Programmi\wxWidgets2.8\lib\gcc_dll\mswud  -c "D:\Sources\SCE\DBX Utilities\DBXProva02\DBXProva02App.cpp" -o obj\Debug\DBXProva02App.o
mingw32-g++.exe -LD:\Programmi\wxWidgets2.8\lib\gcc_dll  -o bin\Debug\DBXProva02.exe obj\Debug\DBXProva02Main.o obj\Debug\GUIFrame.o obj\Debug\DBXProva02App.o  obj\Debug\resource.res   -lwxmsw28ud "D:\Sources\SCE\DBX Utilities\DBXProva02\libDbx32.a"  -mwindows
obj\Debug\DBXProva02Main.o: In function `ZN15DBXProva02Frame9OnNewFileER20wxFileDirPickerEvent':
D:/Sources/SCE/DBX Utilities/DBXProva02/DBXProva02Main.cpp:81: undefined reference to `_Z11TOpenDesignllPKcP10DbxContext@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 20 seconds)
1 errors, 0 warnings

thomas

Quote from: TheLastArrived on May 13, 2008, 02:31:39 PMIf I give an absolute path, C::B finds the lib but the undefined reference problem returns
Having it find the DLL at all is half of it already. Now, if it still misses a symbol, there are two possibilities left:
1. the symbol isn't in the DLL at all (wrong DLL)... duh :)
2. C++ name mangling. Either the DLL or your program (most likely your program) mangles names, and the respective other does not. Therefore the names aren't the same, and the linker doesn't find the symbol.
Solution: play with extern "C"
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

stahta01

#8
Quote from: thomas on May 13, 2008, 08:33:55 PM
Solution: play with extern "C"

But, he does not have the source. So, playing with extern "C" would be hard to use.

Edit: But, I just realized he most likely have a header that might need edited by added extern "C".

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

Do you have the header called Dbx32.h

What does the signature of the function TOpenDesign or whatever the function having linking problem look like.

I Goggled the below.


long
DLLX TOpenDesign (long language,
                  long version,
                  const char* pDesignName,
                  DbxContext* pContext);


If yes, the DLLX needs defined as something, but I am not sure what in your case.

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

thomas

Quote from: stahta01 on May 14, 2008, 01:24:04 AMEdit: But, I just realized he most likely have a header that might need edited by added extern "C".
Well, yes :) Somewhere the function must be declared, after all.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

TheLastArrived

Update

@stahta01: DLLX is defined as __stdcall

extern "C": in Dbx32.h added

#ifdef __cplusplus
extern "C"
{
#endif

.......

#ifdef __cplusplus
}
#endif



now if I try to build I get
-------------- Build: Debug in DBXProva02 ---------------

Linking executable: bin\Debug\DBXProva02.exe
obj\Debug\DBXProva02Main.o: In function `ZN15DBXProva02Frame9OnNewFileER20wxFileDirPickerEvent':
D:/Sources/SCE/DBX Utilities/DBXProva02/DBXProva02Main.cpp:81: undefined reference to `TOpenDesign@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings


The prefixed underscores disappeared and the name more readable but the function is still missing.  :(
I tried with all possible combinations of .dll, .lib, lib<>.a w and w/o -U switch in dlltool. No way.
It's not likely I'm using wrong DLL, because it's part of a package with the header file, given with a CD by the vendor.

TheLastArrived

I've found this article, it seems it could be useful so I'll give it a read.
More news later :wink:

TheLastArrived

#13
Bingo  :D

edit: I'm wrong don't do it... see some posts below
I've replace
#define DLLX __stdcall
with
#define DLLX __cdecl
in DBx32.h and now I get a beautiful
Process terminated with status 0 (0 minutes, 26 seconds)
0 errors, 0 warnings


I hope it'll functions correctly :wink:

Michael

[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]