How to implement and use a .dll or .lib in a Visual C++ Project? [SA-MP Source] - c++

I am trying to learn more about Multiplayer Modifications so i've downloaded the source code of San Andreas Multiplayer.
My problem is that the client project creates a .dll and a .lib files. I've searched on many sites how to implement them into a new project but i just did not find a clear answer.
So i am creating a new Visual C++ project where i need to implement the libraries resulted from SAMP Client compilation. Any help would be great :).
If you want you can join me in this project.

I don't know how SAMP dlls are but in general, this is how it works:
Linker need *.lib files. So you should copy lib files to your default lib directories or create a new directory under libraries of your project and copy them in it.
If the project you want to link have include files too, do what you did for lib files for include files too. Copy them to include directory.
Put DLLs in a directory that your application can reach. This can be your Debug folder or even it can be Windows or System32 directory. Choose where to put them on your own (it depends on many parameters. Pick one that fits you).
This link tells you how to put them in project directories.
That's it. You can call functions of the project you want to use. Tell me if you got problem.

Related

How do I link a library? (MySQL Library)

Alright, I am trying to Link this into my program and I never linked anything before so I need help with not only linking it (Because I have the directory in the linker -> additional directories but I need to link against the libmysql.lib/dll. I am not sure how to do that.
If you could give me a basic understanding that would be great! Just don't make it too complex ;)
I am trying to follow the steps on the website and it says to do this: (I have looked up and people said something about a MakeFile and I honestly don't know anything how that works)
The Connector/C++ static library file is mysqlcppconn-static.lib. You link this library statically with your application. Also link against the files libmysql.dll and libmysql.lib. At runtime, the application will require access to libmysql.dll.
Here's what you need to do to install an library to your c++ project.
Add it to additional depencies in the linker.
Project->Properties->Linker->Input
Click the dropdown, click edit. On the list on the top, add just the names for every .lib file included in the library. For example, If installing SDL, one would add:
SDL2.lib
SDL2main.lib
These files can probably be found under the lib folder of the library you downloaded (look around a bit).
Tell VS where the header files are.
Project->Properties->VC++ Directories->Include Directories
Click the dropdown, click edit. add the location of all of the header files of the library. You can put them wherever you want, but it is suggested you put them in their own folder in your project folder somewhere. Wherever you put them, put the directory here; the containing folder, not the files themselves.
Tell VS where the lib files are.
Project->Properties->VC++Directories->Library Directories
Click the dropdown, edit. Same thing as step 2, but you instead are putting the location of all the .lib files. Yep, the same ones you defined in step 1.
Provide the .dll files to your executable.
When the executable runs, it needs .dll in the same directory or it will not run (CORRECTION = It will run, but will give you an error upon open). So find your executable and put any .dll files in the same directory. How to find where it is by default:
Solution Explorer->Right Click Solution->Open in explorer->Debug
You should see the .exe there. Put the libraries dll files in that same directory.
If you have any questions please ask.

Adding my DLL into a Visual Studio project in C++

I am working on a project that involves making a dynamic link library, so I want to test it in a console app in Visual Studio.
The DLL is also made in Visual Studio, it doesn't have much, just a few functions in it. I'm not sure if I'm just supposed to include the librarys header in the include directories panel in Properties, or do something else
A lot of people say I'm supposed to add its corresponding .lib file in the Library or Reference directory, but I'm not sure that VS generates a .lib file alongside the DLL. I'm using VS 2015.
I don't have VS in front of me this very moment, but these should be the general steps to set it up:
Properties->Linker->Input: your.lib
Properties->Linker->Additional Library Directories: ../your/bin
Properties->General->Compiler->Additional Include Directories: ../your/include
To build your app, the DLL's API headers must be in the include for the compile-time, it's LIB files in the bin for the link-time. Once you have your app EXE, all you need is the DLL to be in the same folder as your EXE when it executes.
You might also want to add the dll project and the app project into a common solution in VS and add (right click) Project Dependency from the app to the dll. This ensures correct order of building, assuming you are going to build the dll at all.
You can also do what I did.
You can create a Libs directory inside of your Solution directory.
You can then place your .DLL files inside of the Libs directory or some sub-directory inside of Libs
In my case, I added the entire SFML-2.3.2 directory in there, which included the source-code, .lib files, and .dll files.
I did link up what I could in the project properties, but I used Visual Studio's macros to fill in the path name to the Solution directory. Just in case I wanted to put this in version control and work on it from multiple machines.
Then I opened up the Project's Property Page.
Within the property page, I went to Build Events -> Post-Build Event -> Command Line
Within the Command Line, you can add a copy command that will copy any needed files into the same directory as the executable that will need them.
In my case I used: copy "$(SolutionDir)Libs\SFML-2.3.2\bin\*" "$(TargetDir)"
I could have written multiple commands to copy just the individual files that I needed, but I had spent a good three hours trying to get SFML to work without actually installing it.

OpenGL Configuration Problems. Unresolved external signals

I watch "OpenGL Essentials" to learn OpenGL programming. The author writes a framework himself which contains lots of .hpp and .cpp files. To use the framework, I have to compile all files to a single Library.lib file. But there are two questions that I cannot figure it out.
When use the original sln provided by author, it create two lib files, one in a new created folder "Lib" and one in "Debug" folder, It also copy all hpp files in "Library" folder to a new created "include" folder. But this do not happen when I create a new VC++ project by myself. So how should i configure the VC++ project properties to make that happen.
When we use Glfw, we have to include glfw3.lib, and we also need to tell the exe file where to find the glfw3.dll. That's hard to understand. Because, we can compile our framework to lib file, And we also can compile it to dll file. So, Why we need both lib and dll file for GLfw? What's the difference between this two files.

SFML library can't find .dll

I implemented the SFML library nightly build to my Visual Studio 2013, because the original one is not compatibile with this VS version. I done everything what is needed (added directory to include folder in both Debug and Release, added directory to .dll files), but it can't find the files in program. What else should be done to make this library work? Or should i consider changing Visual Studio to 2010?
You haven't given really to much information so I am just really guessing as to what the problem is.
added directory to .dll files
But that sounds like your problem right there. You don't add the directory that the .dll files are in to your project. The only directories you need to add to the project are the include directory and the library directory.
But anyways I am assuming you are using dynamic linking since otherwise you wouldn't be dealing with .dlls. Now different IDE's require that you place the .dlls in different spots but since you are dealing with VS2013 you need to copy whatever .dlls that you are using into the same folder where your program's compiled executable is (The .exe file).
Another option is to link statically instead of dynamically which I generally prefer to do on small projects but it is really up to the developer which he prefers.
When you link statically you don't need to include any .dlls. What you will need to do is recompile SFML's sources and make sure to build the library so it produces the static library files (They should be named something like sfml-graphics-s-d.lib for debug and sfml-graphics-s-d.lib for release).
Add that library directory which contains the static library files to your project and then link to them .lib files in VS's input window (Remember that -d is for the debug build).
Next you will need to add SFML_STATIC to your preprocessor options on both the release and debug build.
After that you are good to go and don't need to include the .dll files with your project. And again whether you choose to link dynamically or statically is really up to you and the project you are working on but for small projects I would suggest linking statically.

C++, Visual Studio 2012, LIBs, DLLs, Includes, Source and Linker

I'm trying to understand what exactly all of these are and how they relate to each other (and most importantly, how to install them).
From what I've read, LIBs are libraries linked during the compilation of my project and DLLs are libraries linked during the runtime of my project.
So for me to use a LIB, I have to have the actual .LIB file somewhere in my computer, go to Project -> Properties -> VC++ Directories and add the path to the file in the Library Directories, and after this I have to go to Linker -> Input -> Additional Dependencies add the .lib name in there, and finally I need to type #include in my code, right?
So, some questions:
When I finish and build the release of my program, will the .exe only run if the target platform has the .lib installed in their PC as well? If yes, what steps do I need to do to make sure the .lib goes with the .exe?
When I get the source of a open source project, if I add them (using Add Existing Item...) to my project, can I use them just by using #include as if the files were mine and it would be the same as having the .lib installed? Or do I need to install the .lib file and still use these source files?
I have a project using OpenGL and I linked to glew32.lib, but I don't have the lib or any new directory added in the VC++ Directories, so I think this means I must've installed the .lib in the system folder or somewhere where the Visual Studio won't ask for another directory, should I worry about this when releasing a project?
How the above questions relate to DLLs and is there any reason why should I use DLLs over LIBs or the other way around?
I'm starting to use more and more libraries and I noticed I just dragged, copied and included it everywhere so I could use them but never really understood how they "fit" in the project. Especially those open source libraries where they provide so many files and I don't really know what to do with them...
You don't need to have LIB files along with your EXE file for running in another computer, LIB files are static files and DLL files are dynamic. So when you compile all static codes will be included in your EXE file, but DLL files will be loaded and used dynamically in runtime, so you just need to have your DLL files with your EXE file. This way, your code will work and run properly in other computers.
Just adding another project is not enough, you need to compile them and generate LIB files out of them. Then you add the generated LIB file to your final project and include external projects in your final binary. If you are compiling multiple projects together in a solution, you'll need to set project build order in solution properties in VS.
No, that's OK. It seems you've put LIB files in right folder and you don't need to have LIB file with your EXE file to run it in other computers.
DLLs are dynamic libraries, so you need to have them with your application. Installers usually install EXE files with DLL files in the same folder, so your app will run properly, but no need to include LIB files at all.
Also you can include LIB files like this:
#pragma comment(lib, "glew32.lib")
So you don't need to do it in project settings, but assuming you have your LIB file in "Library Directories" path.
Using DLL files can be done in two ways:
One is linking your application to DLL file and having DLL file's function entry in your EXE file's import table:
like using
#include <windows.h>
then
GetWindowsDirectory(windir, MAX_PATH);
So you'll have GetWindowsDirectory API entry in your EXE file's Import Table.
Also you can do it dynamically:
hinstDLL = LoadLibrary("kernel32.dll");
if (hinstDLL != NULL)
{
func_GetWindir = (DLLPROC) GetProcAddress(hinstDLL, "GetWindowsDirectoryA");
...
There is not much difference, only difference is:
In first method, as it's in your EXE file's Import Table, if there was no kernel32.dll or there was no GetWindowsDirectory entry in kernel32.dll, your EXE will not run at all, it will show a critical error and will not run. But in dynamic way (second way), your app will run, but as soon as your code try to use GetWindowsDirectoryA API, it will fail. You will have 0x00 in func_GetWindir. If you attempt to call it, then program will crash.