How to “add reference” in C++

前端 未结 4 846
不思量自难忘°
不思量自难忘° 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: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

提交回复
热议问题