How to compile openssl for only two functions - c++

I am trying to use openssl to encode a simple string, no need of decrypting it.
I am using the functions
AES_set_encrypt_key()
AES_cfb128_encrypt()
I need I think only the include:
#include <openssl/aes.h>
I couldn't find instructions in the web or in the openssl website about how to compile the source code for a visual studio project. Does anyone know how this should be done?
Can I compile only the code relevant for these two functions?
Thanks a lot

I understand you want to use these functions in your code. You don't need to compile OpenSSL for that.
Install the package on your development machine (if developing for Windows, you can get the latest installer here). It will contain header files you need to include in your code (they will be in c:\OpenSSL-Win32\include\openssl), static libraries you need to link your application against (in c:\OpenSSL-Win32\lib\VC) and DLL your application needs to run (in c:\OpenSSL-Win32\bin).
So, in your code you'll have:
#include <openssl/aes.h>
...
AES_set_encrypt_key(...);
...
AES_cfb128_encrypt(...);
...
Your compiler needs to know the path to header you included (path to openssl directory) so you need to provide it. If you are using Visual Studio, open project settings and add c:\OpenSSL-Win32\include to Additional Include Directories.
In the Linker section, add ssleay32MDd.lib;libeay32MDd.lib; to Additional Dependencies so your linker know which OpenSSL libraries to look for. Path to these libraries - c:\OpenSSL-Win32\lib\VC - add to Additional Library Directories.
Be careful with these libraries: they need to be linked against the same runtime library as your application. Their suffix tells you which runtime library they are linked against: libraries I mention above have suffix MDd which means they use Multi-threaded Debug DLL (/MDd) - so you will include them if your application use the same runtime (check this in Code Generation->Runtime Library setting).
Your application will build now but to run it, your binary will need to have access to OpenSSL DLL libeay32.dll as depends on it. Copy that DLL to system directory or to the directory you're running your app or add path c:\OpenSSL-Win32\bin to Path environment variable.

Related

VS2017 doesn't copy Additional Include Directories to remote env (linux) causing compilation error

I'm playing with Visual-Studio 2017 c++ cross platform projects (linux).
I was able to compile and run a single executable project.
Now I've created two c++ projects. One is a static library, the other is a dynamic library. The dynamic library uses a class from the static library, so I have an #include statement for the corresponding header file that contains the class definition. Compilation fails saying the header file is not found. I added the include directory to Additional Include Directories in the project properties (which usually works with windows vc++ projects). However, it looks like the header file is not copied to the remote linux env (WSL).
I don't want to add the include file to the dynamic library project because there is a reason why Additional Include Directories were invented.
Is there a way to configure the build to copy Additional Include Directories to the remote env?
Files referenced thru Additional Include Directories are expected to be on the Linux remote already, VCLinux doesn't copy them for you. And, to be honest, you wouldn't want it to because additional includes are often third-party packages.
But if you've built the static library then it's source, including headers, will be present on the Linux remote. Specify the paths to the static library headers and library on the Linux system thru the project settings.

Ensure all dependency dlls are packaged into executable

I thought if you compile a Visual-studio 2013 Win32 project using /MT Code Generation->Runtime library then it will automatically package all dependency dll's - including 3rd party dll's - into the executable?
Currently certain users get an error when running my .exe. Its related to not having a 3rd party dll (OpenSSL):
The program can't start because LIBEAY32.dll is missing from your computer
This error has occurred for users using my .exe on Windows 10 OS's. How can I ensure all dependency dll's are packaged into my .exe?
I currently compile my application on my 64bit Windows 8.1 OS. The win32 visual-studio project is compiled using the following project properties:
Character Set: Unicode char set
Use of MFC: Use standard windows libraries
Whole Program Optimisation: Use Link Time Code Generation
Debug info format: none
Code Generation->Runtime library: /MT
/MT indicates that you are using multithread, static version of the run-time library. This doesn't affect third party dependency, e.g. OpenSSL is still linked dynamically.
To check your dll's dependency I prefer Dependency Walker tool. It will show if some of dependency is missed.
To ensure all dependency linked into your .exe file use static linking for all your third party dependency, e.g. for OpenSSL you should use libeay32MT.lib library.
Nikita is right, use Dependency Walker and I'd add that you're missing some knowledge about what a DLL really is. No offence meant, I know dlls can be a pita. What is a DLL?
By definition a DLL is not included in your .exe but it is loaded at runtime and could be shared amongst several applications. If you want to include it in your .exe it will require some extra non trivial work to embed them into your exe, unpack and load at runtime.
See this post
I'd suggest to use an installer instead, much easier to handle! Just create an installer (you know the click click click "yes-yes-I agree-Ok-Done" wizard installer) that will include your .exe and all needed dependency files. As a reference Inno setup is quite great.

Netbeans C++ using MinGW and the libnoise library

Using netbeans 7.2 and the most recent version of MinGW (using installer) I can't use the libnoise library. I am properly including the header files, the auto completion confirms this, however the library is simply not working. There is a .lib file and a .dll. I have tried every possible combination of adding them under project > properties > Build > Linker as well as putting the .dll in the base project directory. Despite all this I am still getting undefined reference errors whenever I try and run the code. Does anyone know what to do?
I know that it is possible to link import library files (*.lib) with MinGW, but I still suggest to recompile libnoise.
With the current Makefile, this is not very easy and may break. Thus I've written a CMake script, which doesn't only work on Windows, but should work on all CMake supported platforms. In addition to this, I've cleaned up the directory structure. The code itself hasn't been touched and when you build a library it should essentially be the same as the current one.
libnoise on GitHub
After you've built your shared library, you'll have a libnoise.dll.a and libnoise.dll file. You then add libnoise.dll.a to the linking settings and put the DLL itself next to the binary, or in the working directory.
You have to link the lib file (= adding it to linker libraries) and put the dll to
<project root>/dist/<...>/
(where your exe is put to). There's no need to add the dll to linker too.
Can you please post the error message you get?

opencv_highgui230.dll was not found

I am creating an application by using opencv2.3 IN VC++2010 express addition. The build is successful but while compiling it says that 'opencv_highgui230.dll was not found.Reinstalling the application may fix the problem.' Though I have added all the necessary include and lib files.
It's likely that this DLL can be found in bin or similarly named directory under where you installed the OpenCV library. For Windows binary distributions of various libraries, the DLL is usually included.
For your program to load it, it either has to be in the same directory as the executable, in your system directory, usually C:\Windows\system32\, or I think that it is possible to specify the location programmatically, in your code. This MSDN article can tell you more.
Quick and, more likely then not, correct solution would be to copy the DLL into your executable's directory.
Because your application relies upon the library, you have to build the library first before you build your application. The error message is telling you that it can't find the binary file corresponding to your library, opencv_highgui230.dll, not one of the code files.
You can either configure Visual Studio to automatically build the projects in the correct order by setting up the appropriate project dependencies, or you can do it manually.

How can I build a C/C++ program using `static linking` & `Dynamic linking` with gcc & Visual studio?

A library can be used in an application in two ways:
Statically-linked
Dynamically-linked
But how to do that using both Visual Studio (windows) & GCC?
I know libraries are distributed only in these 4 ways:
Source
header-only libraries
*.lib files for windows. *.a for linux
*.dll (windows) & *.so (linux).
Source distribution is just compiled."header-only libraries" are nothing but a source distribution.
Now if the desired library is distributed in *.lib form. Inorder to use it.
On Visual Stuido :
We add directory path containing headers(*.h) to Configuration Properties > General > Additional Include Directories
we add each *.lib file to Configuration Properties > Linker > Input > Additional Dependencies
we add directory path of *.lib files to: Configuration Properties > Linker > Additional Library Directories
How to do the same thing for GCC/MingW?
I don't know how to build my application when the library is distributed as *.dll or *.so too. Can someone tell me what do I need to do in these situations for both Visual studio (windows) and GCC(linux)/mingw(windows)
On GCC, for static linking, you'll include the library in the command line. Lets say you've glib-2.0.lib and your program that uses GLib library is my_prog.c, then you invoke GCC as
gcc my_prog.c -L<library_dir_here> -lglib-2.0.
As for the dll and so, dynamic libraries are something you don't link to your programs by passing them to your linker. Instead the operating system gives you a function to load them when it's required, at run time. Thats the reason it's called dynamic. In Windows you've LoadLibrary and on Linux you've dlopen. Both these functions get a string (which is the dll or so's name) and load it if it's avaiable on the machine. Once it's loaded, the function you require from the library is looked-up by passing its name to GetProcAddress on Windows and dlsym on Linux; both returns a function pointer, with which you can call that function. Since you're not directly calling the functions provided by the libraries directly, but thru' function pointers, there'll be no need for you to link them statically (i.e. pass them to the linker) when you build your app.
For DLL distributions the scenario is similar to that of .lib files. (your #3)
You will have to configure your project to build a DLL. The project will build LIB and DLL files.
Depending on your needs/architecture/design you can either
Link against the LIB file just as you do in your #3 above. Note that the DLL file will have to exist on the target machine at run-time otherwise the application will not load.
Call "LoadLibrary()" from the client app and skip the linking part/no need to have the LIB used in the client application.
I can't help you with the gcc specific questions.