How to “add reference” in C++

前端 未结 4 847
不思量自难忘°
不思量自难忘° 2021-02-07 10:53

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

相关标签:
4条回答
  • 2021-02-07 11:03

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

    0 讨论(0)
  • 2021-02-07 11:08

    The first thing you need to do is to #include the header file that describes the functions that are available in that library.

    The actual code for the library will be in one of 2 places:

    1. A static library (.lib)
    2. A dll (.dll)

    Depending on how the library's code is given to you (as .lib files, or as a .dll), you'll have to either:

    • #pragma comment( lib, "libraryname.lib" ) if its a .lib
    • LoadLibrary if its a .dll

    Sometimes a package comes with BOTH a .lib file that you need to link to, and a .dll file. In this case you don't need to call LoadLibrary, you only need to #pragma comment( lib, "libaryfile.lib" ) because in this case the .lib links you into the .dll.

    A very important detail is to put the DLL where your application can find it. Charles Petzold says:

    When Windows needs to load a DLL module before running a program that requires it, the library file must be stored in the directory containing the .EXE program, the current directory, the Windows system directory, the Windows directory, or a directory accessible through the PATH string in the MS-DOS environment. (The directories are searched in that order.) Programming windows, 5th ed MSDN

    I don't recommend using the project properties menu to link because it isn't as visible what libraries you're linking to.

    See also

    0 讨论(0)
  • 2021-02-07 11:13

    C++ doesn't have libraries in the sense you're thinking of. It has header files that you #include, and it has things called libraries that the linker deals with, which contain the compiled code. You need to add the libraries (.LIB files) to the linker settings.

    On Windows if you're using a DLL, ideally you should have a .LIB file to go with it that is called the Import Library for the DLL, and you add that .LIB file to your linker settings.

    0 讨论(0)
  • 2021-02-07 11:14

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

    0 讨论(0)
提交回复
热议问题