DLL References in Visual C++ - c++

I have had C++ experience but not MSVC.
What I am trying to do is incorporate a .dll from an open source project into my project. The code is available and I have built it. I have the .dll as well as the .lib which as I understand it is required for C++ projects.
Now unfortunately there is no simple "Add Reference", drop my .dll into an include directory and add that to my solution. I have edited the project property pages, the C/C++ Additional Include Directories option as well as adding the .lib as an additional linker dependency. I have created an include directory for the dll and lib inside my solution tree.
My problem is when I try to include the header files from the documentation, VS output spits out error messages. Now I realize that I am using the dll/lib combo and that the .h files are not present in my solution so how do I add the proper includes? I am using QT toolkit also which is working but how I add the other header / dll from the open source library eludes me.
Can someone please point me in the right direction.

You need to do a couple of things to use the library:
Make sure that you have both the *.lib and the *.dll from the library you want to use. If you don't have the *.lib, skip #2
Put a reference to the *.lib in the project. Right click the project name in the Solution Explorer and then select Configuration Properties->Linker->Input and put the name of the lib in the Additional Dependencies property.
You have to make sure that VS can find the lib you just added so you have to go to the Tools menu and select Options... Then under Projects and Solutions select VC++ Directories,edit Library Directory option. From within here you can set the directory that contains your new lib by selecting the 'Library Files' in the 'Show Directories For:' drop down box. Just add the path to your lib file in the list of directories. If you dont have a lib you can omit this, but while your here you will also need to set the directory which contains your header files as well under the 'Include Files'. Do it the same way you added the lib.
After doing this you should be good to go and can use your library. If you dont have a lib file you can still use the dll by importing it yourself. During your applications startup you can explicitly load the dll by calling LoadLibrary (see: http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx for more info)
Cheers!
EDIT
Remember to use #include < Foo.h > as opposed to #include "foo.h". The former searches the include path. The latter uses the local project files.

The additional include directories are relative to the project dir. This is normally the dir where your project file, *.vcproj, is located. I guess that in your case you have to add just "include" to your include and library directories.
If you want to be sure what your project dir is, you can check the value of the $(ProjectDir) macro. To do that go to "C/C++ -> Additional Include Directories", press the "..." button and in the pop-up dialog press "Macros>>".

You mention adding the additional include directory (C/C++|General) and additional lib dependency (Linker|Input), but have you also added the additional library directory (Linker|General)?
Including a sample error message might also help people answer the question since it's not even clear if the error is during compilation or linking.

Related

create dll out of yaml-cpp source code

Newbie to windows. I need to use yaml-cpp library in a project, but I can't seem to compile it in windows. I tried everything (everhthing!) I could find but no place have the full answer, just tips for the process. but those tips don't help so much.
I did create shared lib in Ubuntu but can't create dll in windows.
can someone give the full explanation to get dll from source code?
(I also be grateful for explanation of how use the dll with it's includes).
Working with visual studio 2015.
So finally I got it.
for linux users - use cmake. for windows users - you can but I really don't recommend it unless you need cross compiling. Use visual studio: (explained for VS 2017)
First create new project with existing code.
The folder you pick should be "src" (in case of cpp-yaml).
When creating the project you need to choose type of project (exe, dynamic or static library), so choose dynamic library to create .dll or static to create .lib.
After creating your project go to your project properties, go to c/c++ tab --> General --> "additional include directories", and add your path to the headers folder ("include"). Do not add "cpp-yaml" inside the include folder, only "include".
Now build. In the console you can see where the library was created.
To use it, in case you made static library 4 things need to be done:
add to your project "#include cpp-yaml/yaml.h"
in properties:
in tab "c/c++" --> General --> "additional include directories", add the include folder path. (as before)
in tab "Linker" -->Input, add to the "Additional dependencies" your lib name (followed by semi-colon)
in tab "Linker" --> General, add to "additional library directories" the path to your lib.

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.

Is there a way to link classes from different solutions together in Visual Studio?

I have multiple solutions for different project I have worked on but the project I am currently working on rely on a class from a previous solution. I was wondering if there was an easy way to link these solutions together so I have access to all the previous classes.
The most maintainable way to do this is to add the source project's header file directory to your target project's include list.
In the project's configuration properties page, go to:
C/C++ -> General -> Additional Include Directories.
If the source project is compiled into a library, dynamic or otherwise, you will need to link with the corresponding .lib file.
To do this, go to "VC++ Directories" and add to the "Library Directories" field the path containing the .lib file.
Note that you can also add your source project's header directory in this page as well.
After that, go to Linker->Input and add your library's .lib file name.
You will now be able to #include<your_header_file.h>.
Note that you will need to be careful about the choice of directory, as files with the same names as standard or platform-specific headers will cause problems.
EDIT:
I do not know of a way to avoid editing the VC++ Directories page in the project's property page to add a library directory. Why is this a problem? Things like Intel's C++ composer like to step all over these fields (you have to let it make a mess, then you can clean it up).

Sharing files between projects in Visual C++ 10

I've got a relatively simple setup in Visual Studio 2010- a main application and a DLL it depends on. I have a header that defines it's interface- how can I set up the header to be included in both projects? They are both in the same solution.
See my answer to this question, but its just that you are using a dynamic library.
In summary, the main application project should reference the DLL project for the purposes of linking, and the main project should include folder references so the compiler can access header files.
In the VC different projects are placed in a different subdirectories of the solution dir.
If you want include files from the other project, you need to explicitly add it's dir to the include search path:
right click on the ".exe" project in the solution explorer. Choose properties.
In the property window go to the "C/C++" section.
There is the "Additional Include Directories" property. Add your "dll" project directory there.

C++ Project dependencies issue Visual studio 2005

I am working on a dataManagement project that periodically deletes files in a specific folder. The solution has three projects of which, one is the application and the other two are static libraries. Now I want to add one more project which is a static library used for logging. The logging static library project has a header file which the application project refers. When I build the solution, I am getting error as the header file is not found. When I added the logging static library project, I also made the application project dependent on it by checking the appropriate bix in the project dependencies.
Can anyone please help me?
It needs an additional include file path to reference the header file directory...
Project->Properties->Config Properties->C/C++->Additional Include Directories
it doesn't auto pick up the header file paths, it just knows how to link to the project.... Its completely undefined where the header file should be. or even if you have a header file, you can forward reference the thing in the other project if you like!
There are two things you need to do to get a statically linked library working in VS. The compiler needs to be able to find the declaration for the symbols that you're referencing and the linker needs to be able to resolve the full definition. When you add the .lib file to the VS project this meets the second obligation. To meet the first you must include the header somewhere in your source hierarchy before the first reference and you must also tell the project where to find the header files. The dependency settings in VS only set the build order - they will not help here. You need to make sure that the folder that your header files are in is added to the "Additional Include Directories" setting in the project properties, or is one of the global include directories in the main VS Options. You must also make sure that the .lib is added to the linker's "Additional Dependencies" setting.