I'm working on the internal mod menu for one game and I want to use CPR library inside my DLL to get the latest version of offset from the server after injection. right now I get a static version of CPR package with vcpkg and move all .lib file and include file to my project and add them in my include and lib directories and use it like this :
#pragma once
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "zlib.lib")
#pragma comment(lib, "libcurl.lib")
#pragma comment(lib, "cpr.lib")
#include "cpr/cpr.h"
cpr::Response r = cpr::Get(cpr::Url{ "https://myurl.com/getoffset" });
cout << r.status_code << endl;
cout << r.text << endl;
string json= r.text;
It's work fine without any problem but as you can see it's a bit messy with all those "#pragma comment" so I tried to use a dynamic version of CPR so after compiling it I have 3 DLL files :
myMode.dll
libcurl.dll
zlib1.dll
the problem now is I must put "zlib1.dll" and "libcurl.dll" inside game folder near .exe file otherwise it won't inject; is there any way to locate them inside my DLL and tell DLL to use that location?
or just use it like this but remove this "#pragma comment" somehow and add the location for all of them in visual studio ?
Everything you need to know about DLL search paths is well documented here.
The easiest approach would be to just statically link with libcurl and zlib instead of using the DLL stub libraries. What's the downside of that?
Otherwise, all your dependent DLLs need to be in your EXE folder, the current working directory (usually the same as the EXE folder), a system directory (no!), or the PATH.
If your extension has an installer, then maybe it just updates PATH with your install folder of your DLL and all its dependencies.
As for the "messy" pragma comment statements. The standard approach is to add these via the Visual Studio project IDE. That gives you better control of the link search path so you don't have to dump all the stub .lib files in the same folder you build from.
Related
I created a lib file called StackExample.lib. There are functions and objects described in Stack.hpp and Example.hpp.
I want to have an easy time importing my library so I wrote another file called StackExample.hpp.
This file contains:
#pragma once
#pragma comment(lib, "StackExample.lib")
#include "Stack.hpp"
#include "Example.hpp"
Unfortunately I can no longer compile StackExample.lib because it can not import itself.
Is there a precompiler statement that I can use so that all importing programs load the lib but my lib would ignore that line?
#ifndef __STACK_EXAMPLE_INTERNAL
#pragma comment(lib, "StackExample.lib")
#endif
and then right click on your library project, select Properties => Configuration Properties => C/C++ => Preprocessor
Add __STACK_EXAMPLE_INTERNAL into "Preprocessor Definitions".
This way your library will define this definition and client's (hopefully) not.
I'm using Windows 8 / Visual Studio 2012, C++11 and Direct3D 11 for development.
I include the Direct3D libraries like this
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib") // <-- error LNK1104: cannot open file 'd3dx11.lib'
#pragma comment(lib, "d3dx10.lib")
However, the linker can't seem to find the d3dx11.lib. After adding the path where the library is located to the 'Library directories' of the project, the linker still can't find those files. Even after I copied the lib files into the project directory itself, it doesn't work.
I installed the Windows 8 SDK as well as the DirectX SDK from June 2010. Am I missing anything?
When dealing with this myself, I started here:
Where is the DirectX SDK?
then - in about the middle of that page, reading about DirectXMath:
DirectXMath
It's pretty much just including the right header files and changing "D3DX" prefixes to "XM". Not quite that simple, but that's the general idea.
Your first program will probably have inclusions/usings like this:
#include <d3d11.h>
#include “DirectXMath.h”
#include “DirectXPackedVector.h”
using namespace DirectX;
using namespace DirectX::PackedVector;
struct mystruct
{
DirectX::XMFLOAT3 position;
DirectX::PackedVector::HALF packedValue;
};
Of course, it's not considered best practice to have "using namespace", but in this one instance I found that "DirectX::" on every line of my program was making my program almost impossible (for me) to read - so I went with the "using".
Good luck!
Probably you included bad path in properties, Instead "*DirectxSDK/Lib/x86", you included ""*DirectxSDK/Lib".
I was running into this issue as well using VS2010, but I just found the solution. You need to provider the Linker with the library file that you are having trouble with.
Project->Properties->Configuration Properties->Linker->Additional Library Directories
Then add the library file that you want into there. Worked for me, hopefully works for you too, and then you don't have to mess with DirectXMath and finding new tutorials for it.
I use NetBeans, Windows and Cygwin with G++ compiler.
I'm examining Windows Sockets 2. I do everything that is written in MS manual. I have a code (mostly from this manual):
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstdlib>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
else cout << "Initialization OK.";
return 0;
}
And I have a problem when I try to run the project:
undefined reference to `_WSAStartup#8'
I understand that Ws2_32.lib is missing. This is because I do not have Windows SDK installed. But before installing it I want to try out tools that Cygwin offers. It has all the w32api header files, I have them in:
C:\cygwin\usr\include\w32api
And it has some w32api almost .lib files in the directory:
C:\cygwin\lib\w32api
But all these lib files are different, they have .a extension and a little bit different name, like:
libws2_32.a // in Cygwin
vs.
ws2_32.lib // in Windows
When I use Cygwin terminal to create an .exe file, everything works fine. The commands I input are:
cd C:\\c++\\myProgram // go to the dir
g++ myProgram.cpp -lws2_32 // compile using -l option to link libws2_32.a
And after it I get a.exe file. I run it and it works:
./a.exe // Initialization OK.
But as I said I use NetBeans. And if I try to run the project from NB ([F6] button) I always have this error undefined reference to '_WSAStartup#8'.
I've tried already everything I could find on NB forums. I've tried to link libws2_32.a to my project this way. I go to:
File -> Project Properties -> Linker -> Libraries
And there are three options:
Add Library...
Add Library File...
Add Option...
I've tried them all. I've tried to link both just Add Library... and Add Library File.... I've also tried to add such an option in the Add Option... button:
Add Option... -> Other option -> // and I input here "-lws2_32"
But whatever I do I can't run the project from NB, I get error undefined reference to '_WSAStartup#8'.
So it seems that it is not a problem (error) in the code. It seems that the problem is with NB, with its possibility to link libraries. Or I do wrong steps to attach them to the project.
So my questions are:
1) What do I do wrong? How may I run the project right from NB? I didn't try to install Windows SDK, I want to try with Cygwin tools as it has such kind of tools.
2) What is the difference between Windows .lib files and Cygwin .a files? Is it better to install Windows SDK and just forget about those .a files? Everything I could find so far about them on Cygwin site is this:
The import library is a regular UNIX-like .a library, but it only
contains the tiny bit of information needed to tell the OS how your
program interacts with ("imports") the dll. This information is linked
into your .exe. This is also generated by dlltool.
3) Is it possible to use #pragma comment(lib, "libws2_32.a") to link .a files? I've tried but didn't get success results.
1) What do I do wrong? How may I run the project right from NB? I didn't try to install Windows SDK, I want to try with Cygwin tools as it has such kind of tools.
Try this: http://forums.netbeans.org/ptopic44959.html
2) What is the difference between Windows .lib files and Cygwin .a files? Is it better to install Windows SDK and just forget about those .a files?
Both of these files in this particular case are called "import libraries". Import libraries are basically a file containing a list of valid functions, so that when you link your exe, the linker knows that those functions will exist in some particular DLL. So when you link to wsock32.lib or ws2_32.lib, the linker now knows that these functions will exist in wsock32.dll and ws2_32.dll. Thus, it will not complain. Now, the .lib import library format is Microsoft's format. GCC/unix/linux/mingw/cygwin etc. have a different format, and the extension for that format is .a. Now, cygwin/mingw etc. provide a ws2_32.a so that when using cygwin/mingw/gcc, the linker can read the import library in the correct format. cygwin/mingw/gcc will simply not understand the .lib. Microsoft provides the .lib files in their SDK, but I am not sure how this will help in this case. (Though the SDK is definitely useful, because it provides lots of header files and DLLs for other useful things you might need, but the import libraries are useless, because gcc/mingw/cygwin will not understand them; unless you use a converter tool, like the one mentioned in your duplicate question).
3) Is it possible to use #pragma comment(lib, "libws2_32.a") to link .a files? I've tried but didn't get success results.
No, the #pragma linking comments are an MSVC specific (ugly IMO) extension. Use the linker options in the menus.
eclipse, Cygwin
propeties -> C/C++ Build -> Settings -> Cygwin C Linker
Command line pattern
add to -lws2_32
ex)${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lws2_32
I wrote an image matching tool (console application, without gui or windows), using openCV.
I want to port my EXE file to another computer but it asks for opencv dlls (opencv_core220.dll, opencv_highgui220.dll, ...)
My question is how to do it. I see two ways any of them is good:
Recompile opencv as static library (.lib instead of .dll). That didnt work. I get 10 linker errors regarding cap_vfw.obj
If possible, how to merge/embedd DLL's into exe file.
I tried to use ILMerge, but it doesnt work (Error: coul'd not load assembly from a.exe file) since it is designed for .Net only
P.S. - I am using visual studio 2005, on windows, c++ compiler, openCV 2.2
I found an answer.
You must open the original openCV project and recompile all the relevant parts in static library mode.
Do it for each project starting from libjasper and alphabetically until opencv_video. Also do the same for zlib project
For each project, go to project properties\configuration properties\general\configuration type and set it to static library
Now recompile those projects. They will create many large lib files (of up to 10MB) each. Those files are located mainly in modules directory of opencv. You must find them and copy to the directory when you keep your regular lib files when compiling against dll's. Now is a tricky part
You have to include in your code more libraries, than when you compile against dll's
Here is an example what your code should look like this. Sorry that it is not formatted well. Probably html bugs of this page:
#include "cv.h"
#include "highgui.h"
using namespace std;
using namespace cv;
// Directives to linker to include openCV lib files.
#ifndef STATIC_LIBRARY_LINK
// Linking against DLL. For each 'lib' file that appears below, final EXE will need a DLL.
// Core of openCV
#pragma comment(lib, "opencv_core220.lib")
#pragma comment(lib, "opencv_highgui220.lib")
#pragma comment(lib, "opencv_imgproc220.lib")
// Calibration and image matching
#pragma comment(lib, "opencv_flann220.lib")
#pragma comment(lib, "opencv_features2d220.lib")
#pragma comment(lib, "opencv_calib3d220.lib")
// Other libs that might be needed
/*#pragma comment(lib, "opencv_gpu220.lib")
#pragma comment(lib, "opencv_video220.lib")
#pragma comment(lib, "opencv_legacy220.lib")
#pragma comment(lib, "opencv_ml220.lib")
#pragma comment(lib, "opencv_objdetect220.lib")
#pragma comment(lib, "opencv_ffmpeg220.lib")
#pragma comment(lib, "opencv_contrib220.lib") */
#else
// Static linking. No DLL's would be required but EXE file will be bigger
// and linking in debug mode might produce many warnings since *.pdb are not always
// present with the lib files
// Core of openCV. Must be compiled as lib and not as dll's
#pragma comment(lib, "opencv_core.lib")
#pragma comment(lib, "opencv_highgui.lib")
#pragma comment(lib, "opencv_imgproc.lib")
// Calibration and image matching. Must be compiled as lib and not as dll's
#pragma comment(lib, "opencv_flann.lib")
#pragma comment(lib, "opencv_features2d.lib")
#pragma comment(lib, "opencv_calib3d.lib")
// Image I/O auxillary libraries. Must be compiled as lib and not as dll's
#pragma comment(lib, "libtiff.lib")
#pragma comment(lib, "libpng.lib")
#pragma comment(lib, "zlib.lib")
#pragma comment(lib, "libjasper.lib")
#pragma comment(lib, "libjpeg.lib")
// OpenCV linear algebra methods. Must be compiled as lib and not as dll's
#pragma comment(lib, "opencv_lapack.lib")
// Auxillary libs, found in visual studio microsoft sdk
#pragma comment(lib, "vfw32.lib")
#pragma comment( lib, "comctl32.lib" )
//#pragma comment(lib, "window_w32.lib") // Not needed
#endif
int main(void){
// Your code here
return 0;
}
The term you're looking for is static linking. "DLL" stands for "Dynamically Linked Library", which is the opposite of static. You cannot statically link a dynamically linked library. You need a "normal" library for that.
Go to the properties for the project you are building (right click on project in the solution explorer and seleck propeties). Now expand the Configuration properties->Linker option and under General, set the path to the statically linked libraries (i.e., with .lib extensions). Now select the Configuration properties->Linker->Input option and type in the names of all the libraries you want it to statically link to. Now rebuild the project and they should be linked in to the executable. It will warn you if the paths to the files are not correct.
I want to call MATLAB function in my C++ project.
I'm using Matlab R2010a and Visual Studio 2010
First I created a simple matlab function:
function y = foo(x)
y = x+1;
and then I used matlab compiler to compile this function using matlab GUI compiler (File-> new -> Deployment Project and then choose C++ shared Library). It produces this files 2 folders: distrib and src.
distrib contains:
foo.dll
foo.h
foo.lib
src contains :
foo.cpp
foo.dll
foo.exp
foo.exports
foo.h
foo.lib
foo_mcc_component_data.c
I want to use this file in a C++ application. I tried many times and I didn't find a way. All the ways I found over the internet are using old matlab compiler which produces different files or works on an old version of visual studio.
So please could anyone help me?
What must I do? What files/references must I add and to where? What paths must I define?
maybe it is too late but for the future.
Include foo.h.
Add C/C++-General-Additional Include Directories the way to the matlab headers (C:\Program Files (x86)\MATLAB\R2009b\extern\include).
Add foo.lib, mclmcrrt.lib and mclcommain.lib for Linker in Additional Dependencies.
For linker in Additional Library Directories show the way to your matlab libs(C:\Program Files (x86)\MATLAB\R2009b\extern\lib\win32\microsoft for 32bit ver (matlab and VS versions should be the same. I had to install the second Matlab 32bit version.)).
I added the way to the foo.lib in my system path.
Before using your library foo.dll, you should initialize MCR and library function.
mclInitializeApplication(NULL,0);
fooInitialize();
After using don't forget:
mclTerminateApplication();
fooTerminate();
And some demonstration code, looks like:
int num = 1;
double numbrIn = 1.5;
std::cout<<"now we have " << numbrIn << std::endl;
mwArray array_in(num, 1, mxDOUBLE_CLASS, mxREAL);
array_in.SetData(&numbrIn,num);
mwArray array_out;
foo(1, array_out, array_in);
array_out.GetData(&numbrIn, num);
std::cout<<"now we have " << numbrIn << std::endl;
The files foo.h and foo.lib will be required to compile your application. The foo.dll file will need to be shipped with your resulting application, usually in the same directory.
If you put the foo.h file in the same directory as your source files, you won't need to do anything special to #include "foo.h". You can also add the direct path to foo.lib in the external linker dependencies.
If you want to store these files outside of your project folder and/or re-use these files in other applications, you can read up on VC++ Directories, Projects and Solutions.
Edit: You probably also need to add the MATLAB libraries to your include and library paths. Check out the MathWorks support solution Why do I receive the error 'Could not find include file "mclmcrrt.h"' when trying to compile a stand-alone application?