Linking a .lib library to a project in Visual Studio - c++

Somehow, even after going through a lot of materials, I could not understand one thing. I have a c++ library project in Visual Studio 2013. When I build it, it creates *.lib file. Now, there is another project where I need to link the .lib file. I created a folder called lib, dumped the .lib file in there and set the path of the lib folder to Properties->Linker->General->Additional Library Directories. Next I added the library file name *.lib in Properties->Linker->Input->Additional Dependencies. Now, building the project throws huge list of linker errors with message unresolved external symbol .... What is that I need to do more for linking the library?

I'm not sure what's the language your "another project" used, anyway, you should specifically set your *.lib file in Linker->Input->Additional Dependencies if you didn't explicitly load it in code.

If the both projects reside in the same solution the simplest option is to right-click of the project, go to to Properties, there go to Common Properties and in Framework and References add a reference to that library project. This will set the linker to link the .lib and adds a build dependency so the lib is always compiled before the executable.

Related

Visual Studio 2019 - How to add a .lib to a c++ library project?

I created a c++ library with VS2019
my other projects see and use that library fine, they compile fine
but my library itself cannot be compiled as it complains about missing entry points for a depending lib
I want to add that .lib file, like I did for my other projects
but in the library project settings, there is no linker option, so I cannot tell it to use the missing .lib dependency
how do I resolve this ?
thanks for helping me on this
[edit] strangly enough SDL libraries dont cause any problem (becos of dll's ?) while nfd.lib has no dll
maybe the reason I cannot link my library with static libraries ?
image
but my library itself cannot be compiled as it complains about missing entry points for a depending lib
The library project can only be built but not compiled. After you finish building the .lib project, you will get a .lib file like this in the debug folder. And then you could add it to your C++ project.
According to your description, If you want to add a .lib to a .lib project? If so, I suggest you could try to add the name of the .lib file with its extension to Additional Dependencies. Librarian -> General -> Additional Dependencies. Here is the equivalent of linker input. And then add the path to the .lib file to the Additional Library Directories. Librarian -> General -> Additional Library Directories.Here is the equivalent of linker General.

VS2010 link project output lib with dependence lib (without project)

I have solution with single project called Math.
The output of Math project is dll and import lib.
Math project uses another import lib (cxcore.lib) that is part of Math project files (was added as resource and its type was set to library).
My goal is that the linker will link cxcore.lib to math.lib so other projects that uses math.lib will not need to provide cxcore.lib to linker files.
I've set "link library dependencies" to "yes" but still cxcore.lib isn't linked to Math.lib.
As a work around I've used lib command (lib /out:x.lib mpsmath.lib cxcore.lib) as a post build event.
Is there any "clean" way to achieve this goal in VS2010 ?
If you intend to "link" a .lib file into another .lib file you can't do that with MSVC: static libraries aren't linked. They are intended to be linked against something, but that doesn't mean linking happens when creating a .lib file.
You can either link your .lib file into the dll you're creating or move the source code of the dependency library (if you have it) into your project.

Visual Studio 2010 - Create Convenience Static Library OpenCv

For learning purposes, i wanted to create a static library, a "package" of the lib files used in opencv to then link it against my app "opencvuser". Doing so, i get tremendous amounts of erros. (LNK2005 and LNK2019)
My Setup:
Project: staticLib
I've created a static library application without precompiled headers.
Under librarian i've put D:\OpenCV248\build\x64\vc10\staticlib as an additional library
directory. And I've specified all available .lib files as additional
dependencies. (opencv_core248d.lib, opencv_imgproc248d.lib, opencv_highgui248d.lib, ...) Source
Project: opencvuser
I've added C:\OpenCV240\build\include as an additional include directory
Then i've listed "staticLib" under "Properties -> References"
What i expect: Now i should get the same functionallity, as i would add the opencv lib files instead of my built staticLib.lib is my expectation correct?
What i've checked so far:
All Projects are x64
Runtime-Library is set in both Projects to "Multi-threaded Debug"
Anyone knows if the Runtime-Library setting on the static libraries are set to "Multi-threaded Debug"?
You are getting those linker errors because the OpenCV libs you are trying to use were statically linked against the CRT. In your project, you are dynamically linking to CRT and these things won't mix. I would recommend that you don't try to create a "package" of all the OpenCV libs and instead just link to the specific libs you need where you need them.
But I am also going to show you how to solve your problem:
You need to recompile OpenCV without statically linking to the CRT.
You can check out the OpenCV documentation for instructions on how to compile OpenCV using CMake and Visual Studio 2010.
When you run CMake, after you pressed the "Configure" button, look for an option called "BUILD_WITH_STATIC_CRT" and disable it. Then you can press "Generate", open the solution with VS2010 and compile OpenCV.
In your VS2010 project, use the following settings:
In the "opencvuser" project configuration, under Librarian, additional library directories you need to add the path to where the .lib files that you built are located. For me, it's in "c:\opencv248\mybuild\lib\Debug\".
Under Additional Dependencies, you need to include all the OpenCV lib files (opencv_core248d.lib, etc). I also needed to include Comctl32.lib and zlibd.lib because if I didn't I would get some linker errors.
Here are the dependencies I put in:
opencv_calib3d248d.lib
opencv_contrib248d.lib
opencv_core248d.lib
opencv_features2d248d.lib
opencv_flann248d.lib
opencv_gpu248d.lib
opencv_haartraining_engined.lib
opencv_highgui248d.lib
opencv_imgproc248d.lib
opencv_legacy248d.lib
opencv_ml248d.lib
opencv_nonfree248d.lib
opencv_objdetect248d.lib
opencv_ocl248d.lib
opencv_photo248d.lib
opencv_stitching248d.lib
opencv_superres248d.lib
opencv_ts248d.lib
opencv_video248d.lib
opencv_videostab248d.lib
Comctl32.lib
zlibd.lib
Also, in the "opencvuser" project you need to add an empty .cpp file. If you don't add this file, the solution will be empty and Visual Studio won't compile it. I just added a file called "dummy.cpp" to the project. That file is completely empty. Don't put a "main()" function in it because it will collide with the main function in the other project and you will get an error.
In the "staticlib" project, under Linker->General, Additional library directories, you need to include the path to the opencvuser.lib file. For me, it's "..\debug". Also, under Linker->Input, Additional Dependencies, you need to add the "opencvuser.lib" file.
Set project dependencies
You also need to make sure that the projects are built in the right order (first opencvuser, then staticlib). To do this, right-click on the solution and choose Properties. In that window, under Common Properties->Project dependencies, make sure that "opencvuser" does not have a dependency on "staticlib", but "staticlib" must have a dependency on "opencvuser".
That's it, now your project should work. Here are the contents of the two files, and the project running.

compiled *.lib file visual studio

I have several library project. And I have a solution which including these library project and an application project.
My question is, in linker, should I link the *.lib file from the debug folder of those individual projects or from the debug folder of this solution? If I click rebuild, those library project will be compiled to *.lib and new *.lib file will be generated both in their own solution/Debug folder and the current Debug folder.
My problem is that If I set Liker->General->Additional Library Directories to their individual debug folder and Input->Additional Dependencies to the *.lib files, visual studio will give some random linker error 1104 cant open *.lib file, But it is now the same lib file every time, sometimes is Library1 sometimes is Library2.
If I keep the directories and set the dependencies to all those *.obj files. works just fine.
If I delete the directores and set the dependencies to *.lib files in the current solution debug file by "U:\Source\Applications\CURRENTSOLUTION\Debug\Library1.lib" it works just fine.
So, which is the correct way to way my library?
sometimes is Library1 sometimes is Library2
You probably have a build order problem. It is starting to build your EXE project before the libraries are built. The fix for the existing way you have it is to right-click your EXE project and select "Project Dependencies". Tick the library projects.
But the superior solution is to right-click the EXE project, Properties, Common Properties, Framework and References. Click the Add New Reference button and tick the library projects. That not only takes care of the build order, it also automatically tells the linker to link the .libs. Which now also works in the Release build, your existing solution probably didn't do that yet.

Cannot link to lib file after upgrading to VS10

So I imported this VS9 solution into VS10 and VS10 would convert the whole thing into its new format for solution and project files. However, now it is giving me linker errors.
One project, a native C++ project, is using a set of header files and a .lib file to link to some external library. In the project, this library is specified by being put into a "Library" folder in the project. In VS9, this works fine, in VS10, it fails. The same lib is linked, however, when put into the "Additional Dependencies" setting in the Linker Input panel of the project's options.
I know the technique to drop lib files into the project for more than a decade. Has this stopped working with VS10?
No ,that still works in VS2010 (just tested it to make sure)!