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

2019-08-25 04:18发布

问题:

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.

回答1:

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...



回答2:

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.



标签: c++ dll