C++ linker options for DLL (DEF, LIB, etc) - c++

I have downloaded a library for the purposes of writing a program that can uncompress a RAR file. (http://www.rarlab.com/rar/UnRARDLL.exe) This supplies me with unrar.dll, unrar.h, unrar.lib and UnRDLL.def. I have copied the C example code and have tried compiling it with both Dev-Cpp and Eclipse.
I don't have much experience using DLLs, so I don't know how to deal with the following linker errors:
UnRDLL.o(.text+0x151):UnRDLL.c: undefined reference to RAROpenArchiveEx#4'
UnRDLL.o(.text+0x1c0):UnRDLL.c: undefined reference toRARSetCallback#12'
UnRDLL.o(.text+0x1e2):UnRDLL.c: undefined reference to RARReadHeader#8'
UnRDLL.o(.text+0x2b9):UnRDLL.c: undefined reference toRARProcessFile#16'
UnRDLL.o(.text+0x2fe):UnRDLL.c: undefined reference to RARCloseArchive#4'
UnRDLL.o(.text+0x366):UnRDLL.c: undefined reference toRAROpenArchiveEx#4'
UnRDLL.o(.text+0x3d6):UnRDLL.c: undefined reference to RARSetCallback#12'
UnRDLL.o(.text+0x41c):UnRDLL.c: undefined reference toRARReadHeaderEx#8'
UnRDLL.o(.text+0x4c2):UnRDLL.c: undefined reference to RARProcessFile#16'
UnRDLL.o(.text+0x4fa):UnRDLL.c: undefined reference toRARCloseArchive#4'
Google suggested adding --def UnRDLL.def and -lunrar to the linker options and also copying the .lib file to the Dev-Cpp\lib directory.
Can you please explain to me what I'm doing wrong? If possible, tell me what files need to be in the source code directory, what needs to be with libraries, what needs to be added to the project and what linker options there need to be, as well as anything else I've totally missed.
EDIT: I don't know why, but I just manually redid all the settings as described above and now it works. Thanks for your help anyway.

I recommend using Visual Studio Express (freely available from Microsoft at the provided link) to compile and link your program. I think it's a bit simpler in this instance than the other tools that you mentioned, although that's just my personal opinion.
I recommend using a layout similar to this for your project:
\myproject
\src
\include
\thirdparty
\bin
\lib
\include
Your C/C++ source files will live under myproject\src and your header files will live under myproject\include. The library files that you downloaded live under thirdparty: the DLL belongs in bin, the .lib file and .def file belong in lib, and the library's header files belong in include.
Next, you need to configure your project in Visual Studio Express. In your project properties, under Linker -> General, add \thirdparty\lib to Additional Library Directories. Under Linker -> Input, add unrar.lib to Additional Dependencies. This tells Visual Studio Express the name and location of your thirdparty library, so that it can link it into your main application.
When running your program, you'll need to copy unrar.dll to your project's output directory so your program can load it.
This should help you to get going...

Back in the day, Borland used to supply a tool for creating a lib file from a def file.
In the modern world, your best bet is to add the lib file to your project under Project->Properties Config Props->Linker->Input->Additional Dependancies.
You may need to add the lib file location to Props->Linker->General->Additional Lib Dirs.
Make sure the DLL is on your path or copied in with the executables.

Related

How to properly install c++ libraries when using mingw?

I am having trouble installing a c++ audio library called BASS(https://www.un4seen.com/). I have dumped the bass.h file into the mingw32 include directory. I have also put the bass.dll file in the lib section of the compiler. Whenever I try to use the any of the functions or methods that are included in the bass.h file I get an ERROR, telling me there was no reference to the function at all. Interestingly, my IDE visual studio code recognizes the bass.h file with all the methods and functions. Which leaves to think, what have done wrong?
I have some suspicion that I put in the wrong bass.dll because the file includes 10 different .dll files that differentiate between the 32 bit and 64 bit.
If it's telling you there's no reference to that function, then you're probably not linking the library properly. You have to use the -l flag to link the DLL to you project. If there are more DLLs, as you think there are, you can still use this flag to link them. If you need to include a directory of libraries, use -L.
Check this link (MinGW's website) for more info: http://www.mingw.org/wiki/specify_the_libraries_for_the_linker_to_use

C++ Code::Blocks making libraries on Windows

So I'm using Code::Blocks right now as my IDE on Windows 10 and I made a small library with one little function.
Code::Blocks made me a .a file.
I then made a little test project that uses the library. Added it to my project build options in Linker Settings, and added its directory to my search directories. Great.
I can see the header file I made for the library so that's working.
I then try to use the function and I'm getting an 'undefined reference' for that function call.
So..I'm going to assume Windows doesn't understand how to load libraries with the '.a' extension? I believe Windows uses .lib files instead but I'm not sure how else I'm supposed to compile a .lib file in Code::Blocks as there's no template for it.
I am extremely new to Code::Blocks and making libraries in general.
Solved. Problem was when renaming the file from "main.c" to "main.cpp", Code::Blocks does not automatically change the compiler variable.
This was done by going into the file properties in the IDE and changing the variable from "CC" to "CPP".

errors at building a cpp code using visual studio [duplicate]

I'm using the <XZY> IDE to compile my program, and have some trouble to import/integrate a specific library with it.
I get error messages like
fatal error: 3rdPartyLib.h: No such file or directory
for a
#include "3rdPartyLib.h"
statement
ld.exe: cannot find `lib3rdParty.a`
for specifying
3rdParty
in the additional libraries
At least I got some error message like
undefined reference to `lib3rdParty::foo()'
What can I do to fix this?
It's a common misconception, that the current IDE used, is responsible for getting errors like stated in the question.
See for example
c++ lib in two same project ,one can work but the other can 't
Issue linking libxml++ and glib libraries with CodeBlocks IDE for C++ Windows
...
The problem is almost never related to the currently used IDE.
In the most cases the solution boils down, to supply the actual toolchain's compiler/linker with the appropriate path's to search for included headers, to be linked libraries.
One of the major applicable answers for the linker related problems are
What is an undefined reference/unresolved external symbol error and how do I fix it?
undefined reference to `WinMain#16'
Most of the common IDEs provide features to configure this for a specific project. Here are some samples
Eclipse-CDT
Include path settings:
Library & library search path settings
Visual Studio 2013
Code Blocks
**DEV C++ (Bloodshed C++)
As from their FAQ:
9. How can i use the OpenGL library and others ?
All the libraries that comes with Mingw reside in the Lib directory. They are all named in the following way: lib*.a
To link a library with your project, just add in Project options, Further option files :
-lopengl32
This is for including the libopengl32.a library. To add any other library, just follow the same syntax:
Type -l (L in lowercase) plus the base name of the library (filename without lib and the .a extension).
You may also consider putting a -L option there to add directory pathes searched for libraries.
Qt Creator
In order to add include paths you have to open up the .pro file and then add
the directories to the INCLUDEPATH variable. These paths are separated by spaces. Reference can be found here.
If none of the above samples applies for your actually used IDE/toolchain, I hope you're able to get the point of abstraction:
It's an issue how to provide compiling/linking options to your actual toolchain. The IDE used to setup the context is a minor point here.
For eclipse I use the pkg-config plugin where possible:
https://marketplace.eclipse.org/content/pkg-config-support-eclipse-cdt
Failing that this configuration window:

Import files exists to project eclipse C++ Linux

I am using eclipse to dev my application for linux. I have my library source in workspace/mylib/ and the project in workspace/project.
In a source of my project i include the relative path of source library file,
example: #include "../mylib/foo.h"
The problem start when i try build it, in all parts that use library source eclipse shows this error: undefined reference to 'function()'
I think that it is by library files don't have in src folder or not link.
Any Idea?
Thanks!
error: undefined reference to 'function()'
Indicates that the linker cannot find the definition of the function(). Either you have not defined it in your source code Or If you are using a library which defines it, the library is not getting linked.
This or this posts explain what you should do to add external libraries to eclipse.

Why is VisualStudio looking for this lib file? LNK1104 error

We have a large project using VS2008 and boost 1_42. I'm trying to upgrade to VS2010 and boost 1_44. I installed VS2010 and boost 1_44 and converted the project. Now I am trying to build, and everything compiles, but fails when linking:
LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-1_42.lib'
I have changed the include and lib directories to point to the new boost 1_44 files and I have renamed the old boost 1_42 directory.
Why is the linker still looking for a vc90-1_42 file, when it is only using 1_44 headers? Is there a way that I can determine WHY the linker wants this file? The linker obviously thinks it needs the file, but why?
I have cleaned the project and I am re-building to ensure any old build files are erased.
I've run into exactly this problem a couple of times too. It's usually been some old temporary files but like in your case cleaning didn't always do the trick straight away. Does your project include any static libs that might have been built with 1.42?
Something you can try which may or may not be helpful in tracking down your issue:
Rename the old boost directory back to it's original name
Clean the solution
Under C/C++->Command Line->Additional Options add "/showIncludes"
Under Linker->Command Line->Additional Options add "/verbose:lib"
Rebuild all
Then when you build you'll be able to see at which point 1.42 headers are included, etc. in the output window. Somehow doing this helped me in tracking down where the problem was.
Along with changing the lib directory, you need to change the name of the boost library. That's in the Linker | Input section of the project settings.
Your added comment makes it clear that the dependency on the Boost 1.42 library was being created indirectly by another library that hadn't been rebuilt.
For this you basically have two choices: either add that library as a project to your main solution, and make sure it has enough dependency information that it'll be re-built when you upgrade Boost, or use the /Zl compiler switch when you build your library. This tells the compiler you're building a library so you do not want to embed library dependencies like this.
Boost uses
#pragma comment(lib)
command to inform the linker of libraries it needs to link with. It is not an error. If Boost says you need it, it's likely you do.
On How can I find out why the linker wants this file?
There are programs which will go through your app and dlls/libs and report the content of manifests and what the binaries report they depend on. You could then scan the report for the unexpected libraries being included. We used this mainly to find libs including the previous version of the VC runtime.
Have not used the one we had in about 5 years though, now if only I could remember the name of the app!
DependancyWalker (depends.exe) will allow you to see dependancies of dll/exe but not static libs.
You could open each binary as a 'file' in MSVS and look at the manifest content by hand, but I imaging this would be a bit painful. I've not tried this with a static lib.