add two dll's to msvc 2010 application - c++

I'm trying to use libcurl library in my application.
when i start my application, it asks for libcurl.dll and zlib1.dll
to exist in the same folder. when i add them everything works fine but i want to include
them to the executable.
how can i statically add both dll's to the application?

You cannot statically link dlls into an executable. The purpose of dynamic libraries is to be loaded dynamically during runtime from an external image (dll file). You should check whether curl and zlib provide proper static libraries and link against them instead of lib stubs for the dlls.
Consider that linking against static external libraries makes your binary potentially vulnerable against any security issues in statically linked blobs. That means that you will have to update any binary you statically linked against those libraries instead of simply updating the libraries themselves.

Related

Include Libraries in Visual Studio for Other PCs

I'm fairly new to C++ and I know how to link external libraries in Visual Studio, but I'm not sure how to include them with the .exe file that is made when you build. How do I include a library with that so the other PC doesn't have to have the libraries installed in the same exact location on the disk that I do (or in other words just with the application)? I tried putting the .dll files directly with the .exe but it says it can't locate them.
This depends on the type of linkage that you use for the library. You basicaly have 2 options:
static linking
dynamic linking
When you statically link your program with a library, that library is embedded into the resulting application (exe). So you need to distribute only that resulting application.
Or the library can be linked dynamically. This means that the actual library code will be looked up at runtime (by OS) from dll file (on Windows). In this case you must be sure, that user will have dlls you need on their computer. To see where Windows searches for dlls see this msdn page. Basically you want your dlls to reside in the folder, where your program is, so Widnows can find it. Or if you are using some system libraries (e.g. Direct3D) your user will probably have those dlls already installed.

Are lib files exclusively statically linked or do they need to be compiled specifically (VS2015)

I have some confusion about static and dynamic linked libraries and .lib and .dll files.
I have a project with two libraries, one I built myself and one is from an open source library.
The one I built myself is a separate project in the same solution (Visual Studio 2015, C++), and I don't need to copy over the .lib files or create a DLL for the executable to build and run
For the other open source library, I do need to copy over the .lib file and the DLL into the executable folder. However, I thought it would be possible to statically link a .lib file and not have to copy over the DLL.
Does this mean I need to compile the Open Source library differently? Like change the define __declspec(dllexport) to __declspec(dllimport) ? Or change /mD to /mT in compiler options?
I tried both of these, but it's still saying that it can't start without the .dll
Or can I get away with changing a setting in the executable project to link this library statically? If so, what are these settings?
EDIT: I know this is a standard question that can be looked up on google, but I haven't been able to find an exact answer for a while. Mainly, I'm confused about what settings need to be changed, and which project they need to be changed in. (The library or the executable).
I'm under assumption that static linking means the library is built into the executable, and dynamic linking means the library needs to be in a separate file, if this is incorrect, please let me know. Otherwise, I need to know how to build the library into the executable file.
And I can go ahead and change the build options in the open source library, and I tried this already.
Thanks,
-D
In Windows, dll files (dynamically linked libraries) need to be in the same directory as the application or on the search path. lib files (static libraries) need to be statically linked during linking (the last step of building the application). It's common in Windows so have a library come with both a dll and lib file. In this case, the lib file is an import library containing the information needed to easily link to the dll.
Place the dll file where your application will be built and statically link with the lib file. Go to 'Project->Properties->Link->Input->Additional Dependencies' and 'Project->Properties->Link->General->Additional Library Directories' to specify the static libraries you want to link.
Edit: It seems I misunderstood the question. The question is how to recompile a dynamic library as a static library. You need the source code of the library you are using along with it's Visual Studio Project file. Open the library and in `Project->Properties->General->Configuration Type' change it from Dynamic Library to Static Library.
Beware that Dynamic Library uses the Linker group of properties while the Static Library uses the Librarian group of properties. Changing between these types may cause the project to drop essential linker flags options. Since every library is different, I can't predict what you will have to do work around this. Make sure to backup the project file so you can see the original options and flags.
I had to change the setting for "Static Library" for All Configurations, not just Debug, although it was building in Debug. Not sure what may have caused this. Possibly because the debug and release builds for the library were set to the same folder, it may have been overwriting the debug builds with release builds when building

c++ HTTP request without .dll files at runtime

Libraries like curl require you to have .dll files when running .exe
In theory you should be able to link those .dll statically to the .exe
But I can't find a way to do that without errors and crashes.
But are there libraries which don't require external .dll files at runtime?
That allow me to do a HTTP request?
Libraries like curl require you to have .dll files when running .exe
In theory you should be able to link those .dll statically to the .exe
No you can't. They are dynamic link libraries. DLLs can't be linked statically. If the supplier also supplies a static .lib file, you can link that instead of the DLL. It isn't the same thing.
But I can't find a way to do that without errors and crashes.
Unsuprising, as there isn't one.
But are there libraries which don't require external .dll files at runtime? That allow me to do a HTTP request?
Very likely, but why? What's your objection to DLLs?
From my experience, the .lib you are linking is not the dll's implementation, it just contains some metainfo on the dll.
If you are trying to convert a .dll to a .lib, this may help (but it is daunting):
http://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/
If everything fails, you should consider compiling static version of libcurl on you own
http://fluxxu.com/2010/12/08/building-static-libcurl-with-ssl-support-on-windows/ (note: export CURL_STATICLIB in your app)
Building libcurl with SSL support on Windows

Qt non-static linking and .pro files

I am trying to build a .dll that uses the QtCore4 and QtGui4 .dll files. However, I want both Qt .dll files to be linked non-statically. I have read a lot about .pro files and static linking but no one seems to talk about dynamic linking with Qt .dll's. How do I ensure that the qt dlls are built for dynamic linking?
I am having problems that there seems to be two instances of the dll's in my app, one in the app that uses my dll, and then one for the dll itself, so when I try and get a window handle the code inside my dll can't 'see' it; disjoint qtwidget sets is the phrase often used.
Any suggestions?
Unless you added the keyword 'static' to your configuration parameters, the Qt libraries should be already building as dynamic link libraries. That is the default configuration.
The dll extension means Dynamically Linked Library. This means that if you compile the library as a dll, it will always be suitable for dynamic linking.
(If you build a library for statical linking, that has a different extension and requires different config.)
In Qt projects, dynamic linking is the default option. You don't need to do anything to achieve it.

Visual Studio: How to specify different runtime libraries for the linker? (/MTd, MDd, etc)

I'm linking to a few libraries in VS2008. If my knowledge of the linker is correct, MTd is for static linking and MDd is for dynamic linking (to a DLL.) My goal is to statically link some libraries and dynamic link others. The project options seems to only have one setting for all libraries in the linker input. How would I do this?
Your project will be given a sensible C Runtime Library default after you set it up, depending on how you answer the New Project Wizard prompts. You can inspect and alter this (if needed) as follows:
right-click the relevant project in Solution Explorer, select Properties
look under Configuration Properties, C/C++, Code Generation, Runtime Library.
Other libraries can be linked however you want, you just specify the library to link to under Linker, Input, Additional Dependencies.
Even if you are linking to a DLL, it will still have a .LIB file (of the correct form for a DLL) to resolve external references, unless you are manually loading the DLL and discovering required function entry points.
You do need to make sure that the LIB files you link to use the same CRT as your app does, or things can go unexpectedly wrong.
No, you're mixing it up. The /MD vs /MT options is only relevant to which CRT version you link. There are two, the static version (/MT) which you should use only if you don't use any DLLs in your project. And the DLL version, a version that every binary in your process can share so that you won't have heap allocation misery. The kind of misery you get into when memory is allocated by one module and freed by another.
Choosing your own libraries is entirely up to you. Mixing and matching is fine, the linker just gets another kind of .lib. An import library instead of a static library. Just keep in mind to use /MD when you use DLLs.
The linker options your describing are for the CRT only. The static version will limit how you share memory between modules.
All other libraries you use will have be linked in based on the .lib files you provide(or not) to the linker.
There are 3 ways to use a MS library
statically link static library (.lib equivelant of an .a archive of .o)
statically link the stub (.lib compiler generated loadlib/getproc) of a dynamic library
manually load a dynamic library (loadlib/getprocaddress)