Author Topic: Linking to a DLL  (Read 33432 times)

Offline AndreasL

  • Single posting newcomer
  • *
  • Posts: 4
Linking to a DLL
« on: October 07, 2010, 10:09:38 am »
Hi!

I'm trying to utalize functionallity from a Win32 DLL in my Code::Blocks C++ program.

I have .dll, .lib and .h-file and API documentation, and that's all I've got. The DLL is once built with MFC and the source code is not available. Is this possible to link an Extension DLL (MFC) from an application not build with VisualStuio? (My aim is to use a free alternative to Vicious Studio, and Code::Blocks have topped all my expectations!)

With the .h file I can compile my sw, but when coming to the linker, I get the error code undefined reference to `DLL_Func1(long)'. Not suprisingly, the linker cannot fins the symbols.

I have tried to add the .lib file in Project Build Options, Linker Settings, Linked Libraries but it does not make any difference.

I'm running Win XP and Code::Blocks 10.05.

(I have not used the LoadLibrary and GetProcAddress methods, I was kind of hoping that I could use it as any functions via standard function calls to keep the code simple.)

Best Regards,
Andreas Lundgren

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Linking to a DLL
« Reply #1 on: October 07, 2010, 11:18:09 am »
If I remember correctly you need to convert the .lib file to .a file, search the net for info how to do it.
If the dll's interface uses MFC classes, probably you should link you app to the MFC libs and I don't know if this is possible with MinGW.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Linking to a DLL
« Reply #2 on: October 07, 2010, 03:10:28 pm »
@Andreas Lundgren:

The compiler and its version is what?

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

Offline AndreasL

  • Single posting newcomer
  • *
  • Posts: 4
Re: Linking to a DLL
« Reply #3 on: October 20, 2010, 10:59:12 am »
Hi!

I have stepped a few step back now. I create one "Dynamic Link Library" and one "Console application" project using the GNU C++ compiler.

I build the auto generated setup in the "Dynamic Link Library"-project with the one change that I rename the "main.h" to "main_dll.h". I now have exported a function called "SomeFunction". I can access this function from Matlab.

How do I access this from my "Console application"-project in Code::Blocks???
(Is there any documentation about how to set this up?)

I have no idéa about where to start. I have of course included the main_dll.h in my console application I does compile, bot from Linker I get "undefined reference to `_imp__SomeFunction". I guess I need to take more actions, any tip about what (or even better, where can I read about it?)

This is my "Console application":

Code
#include <iostream>
#include "main_dll.h"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    SomeFunction("Andreas hej!");
    return 0;
}

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Linking to a DLL
« Reply #4 on: October 20, 2010, 02:43:33 pm »
I do NOT see a Code::Blocks question in your Question.

MinGW Compiler Link; try to write a question that is a Code::Blocks Question NOT a compile Question that has nothing to do with IDE.

http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs


Note: You can also link directly to an C (NOT C++) only DLL using MinGW; but, I am not sure what limits there is on this.
CB Example doing this is here
http://wiki.codeblocks.org/index.php?title=Linking_the_plugin_to_a_Nightly_Build#Step_Four_-_Link_Everything_Together


Tim S.

« Last Edit: October 20, 2010, 04:28:41 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline AndreasL

  • Single posting newcomer
  • *
  • Posts: 4
Re: Linking to a DLL
« Reply #5 on: October 21, 2010, 10:51:47 am »
Hi!

Sorry if it was not clear, all steps above was made in Code::Blocks.

I find information about how to add a DLL when writing make-file by hand for MinGW, but I thought that I would have some help in the Bode::Blocks GUI how to set up this.

The projects that I'm setting up, "Dynamic Link Library" and "Console application", are projects from the Code::Blocks "File -> New -> Project" window "New from Template", and what I'm trying to do is to link to projects created from Code::Blocks together, the DLL and the Console application.

BR,
Andreas

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Linking to a DLL
« Reply #6 on: October 21, 2010, 09:09:50 pm »
Although you are using Code::Blocks for all of that, what you are trying to do has more to do with programming in general. If I get it right, what you want to do is to be able to use the DLL from the DLL example included in Code::Blocks in the Console application example also included with it. What you need to look for is how to use DLLs in your programs using C++. That is something you can do with Code::Blocks (actually, that the compiler, linker and OS can get to work) by writing the right code and setting everything up properly. Well, this is the wrong place to ask. Even if many people could help you with it, this forums are not used to solve general programming questions... and your questions seems to be just that.

In fewer words: we do not provide support for user applications that do not improve or extend in any way Code::Blocks itself.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Linking to a DLL
« Reply #7 on: October 22, 2010, 02:38:15 am »
If you really want to ask an Code::Blocks Question!!

Turn On full Compiler Logging
http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F

Ask an question like on the command line I do "gcc -Xy" how to I enter the option -Xy in Code::Blocks.

Currently, you are posting like a person who has no idea what an Compiler or Linker is or do!!

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

Offline AndreasL

  • Single posting newcomer
  • *
  • Posts: 4
Re: Linking to a DLL
« Reply #8 on: October 22, 2010, 12:03:50 pm »
I found it.

You provide the import library (.a-file) under "Project Build Options" -> "Linker Settings" -> "Link Libraries".

I still think that this was a pure Code::Blocks question.

Offline keeganator

  • Single posting newcomer
  • *
  • Posts: 2
Re: Linking to a DLL
« Reply #9 on: October 30, 2010, 04:22:21 pm »
Hi AndreasL - glad you found your answer. I'm a newbie to Code::Blocks, but not to dev. I have the same requirement and came to the same conclusion that you did about adding my lib to the list of Link Libraries, but I'm still getting the "Undefined Reference" error when I try to call a global function in the lib. To set up my test, all I did was choose Static Lib from the project templates and a console app to call the functions. I prototyped the functions in a .h file which I included in my console project's main.cpp. Any thoughts?

Thanks very much,
Dan

Offline keeganator

  • Single posting newcomer
  • *
  • Posts: 2
Re: Linking to a DLL
« Reply #10 on: October 31, 2010, 01:29:30 am »
Hi again. I've solved my problem. Seems I just wasn't saving my changes correctly in the Project Options dialog.
Thanks,
Dan