User forums > Help

Using DLLs with CodeBlocks

<< < (2/4) > >>

Miguel Gimenez:
Forget that line and go to the part where full log is enabled.

tigerbeard:

--- Quote from: rascalito on April 08, 2022, 08:40:18 am ---(let's be creative!!). I have included my_dll.h.

The problem is that it doesn't compile or link. I get that kind of message:
undefined reference to __imp_my_function

Can anybody explain me how to use a DLL in CodeBlocks?

--- End quote ---
Welcome to using C::B it requires a bit of getting used to but once you mastered that I am sure you will love it.

I am getting this message when i forget to include the library file itself in the build options. I am getting this message a lot as I usually forget to add the libs to the build options lib page.
The message is not a CB issue, since you would get it as well when you do not include the lib on the command line.

If you are new here a few general hints (which can not replace learning ftrom trial and error of course).
* learn the Project/Build options. This is the main dialog you will be using. Sadly there is not tool button for it. You find it in the menu Projects/BuildOptions... It takes a while to figure it all out.
** The dialog shows a tree with your build project on top and each build targets (Delete, Release etc) below. It took me some time to learn that everything I enter in the topmost tree item is valid for all items. Saves a lot of typing.
** for each project you must chech: compiler, link, Search directories. The others are optional.
** the link section is where you enter the libs. Here you must learn how to enter the name which is different between linux and windows. You can leave away the ending (.dll) for example. Generally you just add the name here. The paths to all libs must be in the search path pages. Make sure they are correct. Then the error you got should go away.
* The BuildLog and BuildMessage panes are your best friends  (in the lower message window - F2). Most VisualStudio users never had to do anything with the command line. Also CB kind of makes that transparent for you. But its really helpful to know about it. When you start build the BuildLog will open (if not hiffen with F2) and every cpp file in your project causes a command line output to the compiler, e.g. the mingw compiler you defind in the compiler setup. You see all command line parameters, e.g. the used libs and include paths. Since this is all in one line, the lines get very long and look a but cryptic. But they are quite simple to understand because there is only 3-4 different thing repeating all the time. So if you get a complie or linker error you do not understand, first thing you should to is to check one of the command lines in the BuildLog. All libs are listed for each command (e.g. -lGL for the OpenGL lib in Linux, similar in windows) and each search path is listed (-LC:/whatever). Its mutch easier to spot an error or a missing library there than in the many tabs in the BuildOption dialog  - but essentially all stuff you entered in the build options is for creating those lines.
* The BuildMessage you get after the compilation is only a summary of the BuildLog. If a certain thing is strange, often the much longer info and the compile command before helps you to find out what the problem might be.







rascalito:
Hello!

Thanks a lot for the detailed reply. I will try that tomorrow (Monday morning).
Right now, it's beer time.

Thanks again!

R

rascalito:
Hello!

Ok, it took longer than foreseen to find time to try it, but I did it. I checked the documents above (thanks Miguel)

I used the beginning of the template to give as many details as possible. As It doesn't even compile, I skipped
the description of runtime errors at the end.

--------------------
I am running Code::Blocks version 20.03 on Windows10
The compiler I use is MinGW 64
version: how do I know. Probably the latest one since I installed it last saturday.

I want to use an existing DLL I made earlier, called kalman_lib
- I have written a code that uses that DLL
- I have included the dll h file in my c code (#include "kalman_lib.h")
- I have opened Project -> build options
- In Linker Settings, I have added kalman_lib.dll by browsing from the add button to
   the library file. Apparently it's stored correctly in the left side space.
        As I was asked if I keep the local path, I tried both without any difference.
- I left "default" for linker executable.
- I first created a hello world-ish project (console application) with my future project
   name, and which prints hello on the terminal. Compiled and executed. It works.
- I have added the dll I want to use, in the same folder as my main.cpp code.
        I have changed the code in main by the real code, that uses the dll.
   As the compiler seems to find the source code, I guess it should find the library
        which is in the same folder. And beside this, it's also set in the project settings above.

When setting build options as suggested in the link above to save build log and
always output the full command line, I get an HTML file as follows:
--------------------
Build started on: 12-04-2022 at 20:43.58
Build ended  on:  12-04-2022 at 20:44.00
-------------- Build: Debug in Kalman (compiler: GNU GCC Compiler)---------------
x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\Kalman -c C:\Users\Rascalito\Develop\CodeBlocks\Kalman\main.cpp -o obj\Debug\main.o
x86_64-w64-mingw32-g++.exe  -o bin\Debug\Kalman.exe obj\Debug\main.o   C:\Users\Rascalito\Develop\CodeBlocks\Kalman\kalman_lib.dll C:\Users\Rascalito\Develop\CodeBlocks\Kalman\kalman_lib.dll
collect2.exe: error: ld returned 5 exit status
Process terminated with status 1 (0 minute(s), 2 second(s))
1 error(s), 0 warning(s) (0 minute(s), 2 second(s))
--------------------
By the way how should I use the report? It's an HTML file, but if I post it in a code window, of course it shows the code.
I had to replace html to BBCode, which is not what I would call efficient.

Thanks,

R.

tigerbeard:

--- Quote from: rascalito on April 12, 2022, 02:08:12 pm ---- In Linker Settings, I have added kalman_lib.dll by browsing from the add button to
   the library file. Apparently it's stored correctly in the left side space.
        As I was asked if I keep the local path, I tried both without any difference.

--- End quote ---
I think the normal way is to add in the linker tabs the DLL filename only. No path. Then add the linker path to the search directory tab. Maybe thats is the resone for your weird linker output (see below).


--- Quote from: rascalito on April 12, 2022, 02:08:12 pm ---x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\Kalman -c C:\Users\Rascalito\Develop\CodeBlocks\Kalman\main.cpp -o obj\Debug\main.o
x86_64-w64-mingw32-g++.exe  -o bin\Debug\Kalman.exe obj\Debug\main.o   C:\Users\Rascalito\Develop\CodeBlocks\Kalman\kalman_lib.dll C:\Users\Rascalito\Develop\CodeBlocks\Kalman\kalman_lib.dll
collect2.exe: error: ld returned 5 exit status

--- End quote ---
Thesse two lines are the key lines to check. Anything you set in CB somehwere is only used to produce these two commands.


--- Code: ---x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\Kalman -c C:\Users\Rascalito\Develop\CodeBlocks\Kalman\main.cpp -o obj\Debug\main.o
--- End code ---
This calls the compiler compiling your source code. When you copy this line, go into a terminal into your project folder it should compile as well. That should be fine



--- Code: ---x86_64-w64-mingw32-g++.exe  -o bin\Debug\Kalman.exe obj\Debug\main.o   C:\Users\Rascalito\Develop\CodeBlocks\Kalman\kalman_lib.dll C:\Users\Rascalito\Develop\CodeBlocks\Kalman\kalman_lib.dll

--- End code ---
This calls the linker. This line causes your error. This should not list your DLL twice and it should list it with a prefix (-l on Linux, I think the syntax in windows was the same, but I can not remember right now). Typically a linker line should more look like that

--- Code: ---x86_64-w64-mingw32-g++.exe  -LC:\Users\Rascalito\Develop\CodeBlocks\Kalman -o bin\Debug\Kalman.exe obj\Debug\main.o   -lkalman_lib.dll

--- End code ---
(seems the code formating eats the '_' char) To figure out how the correc way is, again go to your projet folder in a termina and type there linker command there. Once you got it running you will know how to enter the data into the build settings to yield the correct result on the command line.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version