I'm new to C++ and there's something I just completely don't get. In C#, if I want to use an external library, log4net for example, I just add a reference to the log4net DLL and its members are automatically available to me (and in IntelliSense). How do I do that in non-managed C++?
39.3k 14 14 gold badges 76 76 silver badges 116 116 bronze badges asked Jul 14, 2009 at 19:24 John M Gant John M Gant 19.3k 18 18 gold badges 65 65 silver badges 83 83 bronze badgesOften, the library comes with 1) a header file (.h) and 2) a .lib file in addition to the .dll.
The header file is #include'ed in your code, to give you access to the type and function declarations in the library.
The .lib is linked into your application (project properties -> linker -> input, additional dependencies).
The .lib file usually contains simple stubs that automatically load the dll and forward function calls to it.
If you don't have a .lib file, you'll instead have to use the LoadLibrary function to dynamically load the DLL.
answered Jul 14, 2009 at 19:33 Stack Overflow is garbage Stack Overflow is garbage 247k 51 51 gold badges 351 351 silver badges 556 556 bronze badgesMore thorough than my answer. I would also add that, if you're using LoadLibrary, then you can't simply call functions by name. You need to do late binding.
Commented Jul 14, 2009 at 20:14I see I'm being incomplete again. LoadLibrary returns a handle you can pass to GetProcAddress, which gives you a real pointer. You can also choose to call FreeLibrary to unload it. (I went ahead and deleted my original answer.)
Commented Jul 14, 2009 at 20:18@Steven: True, I figured that if he does need to use LoadLibrary, I'd just give him a starting point, without getting bogged down in the (many) gritty details.
Commented Jul 14, 2009 at 20:23The basic concept is the following: There are 2 types of libraries: static & dynamic. The difference between them is that static libraries, during the linking build step, embed their compiled code in your executable (or dll); dynamic libs just embed pointers to the functions and instructions that some dll should be loaded when program is going to be loaded. This is realized for you by the linker.
Now you can decide which of those two you are going to use. DLLs have many advantages and disadvantages. If developing a huge application it might be worthy to consider using DLLs with delay loading instead of static lib's. Some libs are simply delivered to you as DLLs and you have no choice. Anyway the easiest way for a beginner would be to use static libraries. That would make your deployment and test much easier, since, when dealing with DLL you have to ensure that they are found at runtime (even when using debugger), this involves either copying everything in one directory or dealing with path variables.
Usually a DLL provider (if it is intended that you should be able to deal with the library) delivers you a header file(s) and a .lib which contains the calls into the desired DLL. Some vendors (e.g. boost) only require you to include the header file and the lib is automatically linked to your executable (can be achieved through compiler prorietary pragma directive). If it is not the case you must go into the project settings of the C++ project (project properties/Configuration Properties/Linker/Input) and enter the lib file name into the "Additional Dependencies" row, e.g. iced.lib; iceutild.lib . You can also put fully qualified path names there. Be aware that you have to enter the lib file names for both configurations (Debug, Release). This is the procedure you do with static libraries and Dll equally. The only difference that DLL will require a DLL lib to be either in you app-directory or in one of the path-directories.
After that step, you still might get compiler errors if you try to link incompatible libraries. There are many reasons, why they can be incompatible. But try to first link the lib this way and see if works. If not, post again your errors here ;)
Include file(s) is(are) used to be included in places, where you would like to use smth. from the lib. Just include it and the compiler will know that the symbols must come either from another (compiled) compilation unit (compiled cpp-file=>object file) or the .lib. It will make the look up and notify you if the required symbols are not found.
Good Luck, Ovanes
P.S. This might be hard in the beginning, but when you get used to it, it will be easy.