Where do I input DLL dependencies in Visual Studio C++ project? - visual-studio

I am converting some Qt project files (.pro) that run on Linux and Mac into Visual Studio project files (.vcproj) The Qt Visual Studio add-in converted everything fine except the DLL dependencies. Where do I put these in Visual Studio 2008?
If I put the DLLs in Configuration Properties > Linker > Input > Additional Dependencies, I get:
fatal error LNK1107: invalid or corrupt file: cannot read at 0xABC
Where do dynamically-linked dependencies go?

Project Properties -> Linker -> Input -> Additional Dependencies
In that field put xxxx.lib for whatever library you need.

You might want to check what’s the differences between .dll , .lib, .h files ?.
You need to specify the corresponding .lib file at link time. not the dll.

Related

External/Additional include and source directories in Visual Studio 2022

In order to use external code (QGeoView in this case) I copied the demo code (main.cpp, mainwindow.cpp, the .ui file, etc.) into my Visual Studio project root alongside the QGeoView lib directory.
This alone left me with compiler errors like "Cannot open include file: 'QGeoVies/QGVMap.h'" which I resolved by adding the lib/include directory to the 'Additional Include Directories' in the C/C++ -> General menu in the project properties.
Now I obviously get linker errors since the source files that belong to the just added headers aren't compiled yet.
Where exactly can I add the additional source directory (project root/lib/source in my case) for Visual Studio to find and compile the source files?

How to statically link VCPKG produced .lib file in Visual Studio

I use VCPKG to build third party libraries like libcurl for example. I have dynamic and static builds. Obviously, import .lib and object .lib files are contained in two different folders, x64-windows and x64-windows-static respectively.
I want to link object libcurl.lib statically with my program, but cannot figure out how to configure Visual Studio to do it. It always ends up using the import lib, rather than object lib and thus my program ends up requiring libcurl.dll at execution.
I have configured the main project to use /MT runtime library.
I have tried configuring linker to use additional libraries folder pointing to the static lib folder and additional dependencies.
I have even tried using full path to the object libcurl.lib file in linker additional dependencies.
When I us /MT switch for runtime libraries, I can tell by using Dependency Walker that all the regular libraries like vcruntime are integrated in the executable, not loaded as DLLs, but libucurl.dll is still there, loaded dynamically.
It seems that due to VCPKG automatic include path integration, Visual Studio always finds and uses the import libcurl.lib first, even if I specify the full path of the object libcurl.lib in linker inputs.
How do I configure Visual Studio to statically link the right .lib file?
OK, I found the solution for anyone who might have the same problem.
By default, Visual Studio uses x64-windows, or x86-windows "triplet"
You can see the automatically deduced triplet by setting your MSBuild verbosity to Normal or higher:
Shortcut: Ctrl+Q "build and run"
Tools -> Options -> Projects and Solutions -> Build and Run -> MSBuild project build output verbosity
To override the automatically chosen triplet and use x64-windows-static or x86-windows-static, you can specify the MSBuild property VcpkgTriplet in your .vcxproj by adding this to the Globals PropertyGroup.
<PropertyGroup Label="Globals">
<!-- .... -->
<VcpkgTriplet Condition="'$(Platform)'=='Win32'">x86-windows-static</VcpkgTriplet>
<VcpkgTriplet Condition="'$(Platform)'=='x64'">x64-windows-static</VcpkgTriplet>
</PropertyGroup>
https://github.com/microsoft/vcpkg/blob/master/docs/users/integration.md#with-msbuild

Building Qt for Visual Studio 2010 - cannot open file 'qtmaind.lib'

I'm trying to start a C++ Qt application and have it run and compile in Visual Studio 2010.
Doing some googling, I found that there was a Visual Studio Qt Add-in, and so I installed this. I already had a MinGW Qt binary installed, and when this did not work, I found that you have to compile the source for Visual Studio 2010 (the VS 2008 binary will cause deployment issues).
Using this as a guide: How to build Qt for Visual Studio 2010, I compiled the open source version, and added it to the PATH, along with a QTDIR env variable. Hoping that I got it finally working, I created a "Qt Application" using the New Project Wizard in Visual Studio 2010. Once I finished, I tried to build the program, only to see the following error:
1>LINK : fatal error LNK1104: cannot open file 'qtmaind.lib'
I looked in the C:\QT\lib folder, and found that I only have a qtmaind.prl, but no clue as to why there is no qtmaind.lib.
I am able to echo the QMAKESPEC environmental variable to get a 'win32-msvc2010' output.
I've tried several different combinations of flags for the configure step, including the one in the link, and even tried manually setting the -platform flag.
If anyone can offer any help, it would be greatly appreciated! :D
Just ran across this same problem. I changed the "-release" flag from the linked guide to "-debug-and-release", and then it built the qtmaind.lib library (presumably, that "d" suffix stands for debug).
I encountered this issue when running a project whose settings were hardcoded for a particular machine setup.
I could see that the vcxproj file had something like:
C:\Qt\4.8.1\libqtmaind.lib
I could not find this lib file in my machine. I replaced the line by:
C:\Qt\4.7.3\lib\Qtmaind.lib
It looks like the names of library files are different across Qt versions.Or, maybe the library files were renamed.
Also, the 'd' denoted debug. If you cannot find any lib file post-fixed by 'd', it is likely that you did not 'make' a debug version of Qt.
You can cross check this by opening the .Sln file created by configure. Mine was named Projects.sln. You can open this in notepad and see if win32-debug configurations are present.
This might be a QMAKESPEC issue. Try setting your QMAKESPEC environment variable to
win32-msvc2010
and rebuild Qt like that. This should give you .lib files in your Qt folder
in QT 5.3 with vs 2013
in properties -> linker -> input
remove ANY path for qt libraries such qtmaind.lib
this is known BUG

Dll project doesn't build as Linker not able to find the same named .lib file

I am a newbie in the Visual Studio 2010 environment. I got a source base which was developed using Visual Studio 2008. I am trying to build it in VS 2010; but the build fails as the Linker says the error - LINK : fatal error LNK1181: cannot open input file X.lib'.
Here X is the name of the lib file created from the same project and X.dll is the output dll. In fact the X.lib is not present in the project. Without succesfully building the project, it won't come at all for the Dll to build succesfully. How can I resolve this "Deadlock" kind of situation?
Thanks in advance,
Shiju
I had this problem also. When converting a working VS2008 project to VS2010, the Link phase breaks in the VS2010 project with the same error. It is trying to link the .lib that the project is supposed to be building!
I found the source of my problem: The project has a custom build step that occurs at the end of the build, before the PostBuildEvent. This custom build step copies the .dll, .lib and .pdb from $(OutDir) to an external location.
The Outputs List for the custom build step is set to the full path to the copied .dll, .lib and .pdb, e.g.:
C:/a_new_location/myproject.dll;
C:/a_new_location/myproject.lib;
C:/a_new_location/myproject.pdb.
I found that whenever this Outputs List includes the .lib, that .lib gets added to list of files to link in the link phase. So with the above Outputs List, the link phase will have a file list of:
myprojectfile1.obj
myprojectfile2.obj
C:/a_new_location/myproject.lib
And that causes the link to fail:
LINK : fatal error LNK1181: cannot open input file 'C:\a_new_location\G4SrcCfgLib.lib'
It does not matter if the copy in the custom build step actually copies the file or not. All that matters is that the Outputs List includes the .lib. So, I resolved the problem by removing the .lib from the Outputs List, of course. The downside to this is that doing a Clean build will not clean C:/a_new_location/lib. But at least it builds.

Compiling a simple Qt "Hello World!" application within Visual Studio 2010 Express?

I'm trying to build a basic Qt "Hello, world!" application inside Visual Studio.
I got the moc step to work (I think), but now I am at a loss as to how to fix this linker error:
1>moc_mainwindow.obj : error LNK2001: unresolved external symbol "public:
static struct QMetaObject const QMainWindow::staticMetaObject"
(?staticMetaObject#QMainWindow##2UQMetaObject##B)
I've done a lot of searching but I am at a loss.
Here are my include directories:
i:\Qt\4.6.3\include\QtCore;
i:\Qt\4.6.3\include\QtGui;
i:\Qt\4.6.3\include;
i:\Qt\4.6.3\include\ActiveQt;
reease;
.;
i:\Qt\4.6.3\mkspecs\win32-msvc2008
Here are the libraries I am linking against:
i:\Qt\4.6.3\lib\QtGui4.lib;
i:\Qt\4.6.3\lib\QtCore4.lib;
gdi32.lib;
comdlg32.lib;
oleaut32.lib;
imm32.lib;
winmm.lib;
winspool.lib;
ws2_32.lib;
ole32.lib;
user32.lib;
advapi32.lib;
libpng.lib;
msimg32.lib;
shell32.lib;
kernel32.lib;
uuid.lib;
Does anyone have any ideas?
qmake will generate the moc voodoo from the header file in .pro file. As you aren't using qmake, by the sound of it, but a native visual studio project, this is probably the cause of the problem.
If you use qmake to generate your visual studio project all your problems will go away and life will be sweet. Probably!
I am using the open 2010.05; obviously you want to substitute the correct path for your version.
set up the environment
start 2010 command environment from the start menu
-set include=%include%;C:\Qt\2010.05\qt\include
-set lib=%lib%;C:\Qt\2010.05\qt\lib
-set path=%path%;C:\Qt\2010.05\qt\bin
-set QMAKESPEC=win32-msvc2010
write code, create files etc
generate the initial pro and makefile and fire up VS
-qmake -tp vc
-qmake
you should now have a makefile - check that it works by running:
-nmake
now launch visual studio
-VCExpress.exe /useenv
-XXX.vcxproj can now be opened
If this doesn't work you may need to build qt at against visual studio. This is very straightforward - go to the qt directory (from within the visual studio express command window) and type:
configure.exe -platform win32-msvc2010 -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-multimedia -no-qt3support -fast
You cannot install the Qt VS plugin on the Express edition of VC++. Assuming you got the moc to compile, you also need to make sure you're including the appropriate libraries (*.lib files) at link time. This goes under Project properties > Linker > Input > Additional Dependencies.
You will need qtcore4.lib at a minimum.
Also make sure the Qt library path is in your library search path. On my computer it's c:\qt\4.6.2\lib.
I was able to get QT to work with Visual C++ Express 2010 using http://rajorshi.net/blog/2009/01/using-qt-with-msvc-express-2008/ and http://portfolio.delinkx.com/files/Qt.pdf as guides. Just in case anyone still is having problems.
Have you create the visual studio project using qmake first? The problem seems to be the moc compilation. Do you have qt plug-in installed and the qt path in enviromental variables? Can you add you hello world code so I can have a look at it?
You need to add commands to generate QT metaclasses, then also include the generated files in your project as c++ code.
Generating the QT metaclasses:
First, add your QT bin path into the Executable Directory. (This is in Configuration Properties > VC++ Directories)
Add your Header files that contain Q_OBJECT macros to the project.
Multi-select your header files, then right click on a header file, click Properties.
Change "Item Type" from "C/C++ Header" to "Custom Build Tool".
Set Command line to this: moc.exe "%(FullPath)" > "$(ProjectDir)MetaObjects\moc_%(Filename).cpp"
Set Description to this: QT: Generate $(ProjectDir)MetaObjects\moc_%(Filename).cpp (optional)
Set Outputs to this: $(ProjectDir)MetaObjects\moc_%(Filename).cpp
Run Build just to make it generate the metaobject code
Add the generated C++ files from the Project Directory Metaobjects folder into your project

Resources