VS2010 link project output lib with dependence lib (without project) - c++

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.

Related

How to link a *lib [duplicate]

There are things that I don't understand when it comes to linking... I'm writing a program using a 3rd party library (the GEOS library). This program has a dependency to geos.lib but still needs geos.dll to run.
I read this question, I think I understand the difference between static and dynamic libraries. What I don't understand is why I still need a dll when I statically link a library.
There are 3 kinds of libraries on Windows:
object library (*.lib)
import library (*.lib)
dynamic library (*.dll)
object libraries are statically linked. They contain the full object definitions of the code abstracted by the library.
import libraries is a special form of an object library. Instead of containing code they contain information for the linker that ultimately maps the executable file to the dynamic-link library.
dynamic link libraries, like object libraries, supply code for your program. However, this code is loaded at runtime and not compiled into your exe.
You don't always need to link an import library. Instead you can call LoadLibrary() and lookup the API entry points by name or ordinal. (You always have to tell the code which DLL and where in that DLL's API you want to enter.)
The other comments here are correct in that you cannot make a DLL into a static lib without recompiling the code for the libary -- it is a different kind of output.
It's not statically linked. The .lib is just a stub library that binds in the .dll on windows. That is, you link with the .lib at compile time, and then at runtime it will go looking for the .dll.
If .lib was created by Visual Studio then check value of Project properties -> Linker -> Input -> Module Definition File. If it's not empty then link.exe create stub library instead of static library even if Project properties -> General -> Configuration Type is "Static library (.lib)".
You are definetely linking to a dynamic library.
Just because the linker requires .lib file doesn't mean you're linking to a static library.
You can statically link the lib file if and only if this is a static lib file. So first you need to convert your dll's project to the static lib, build it and after that use the product of your build which will be a static .lib file.

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.

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

Linking a .lib library to a project in Visual Studio

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.

How link .obj files in Visual Studio?

My problem is:
I have compiled LuaJit with VisualStudio and its genereated a .Obj file, a dll and .lib file.
i just want to link the .obj file in my win32 application to generate a single EXE file.
If its possible,i still need to add the Lua headers?
Thanks anyway.
Use the header file to define LuaJit symbols for your code that deals with Lua.
To link you only need the lib and dll. Add the lib file to your link dependencies under Project Properties > Linker > Input. Put the DLL next to your EXE when you run.
You need to make the project produce a .lib file containing the code (instead of in addition to the dll) and link that. It could be that you can by switching the project setting (look for one labelled lib, or static). Otherwise, you'll have to modify the project settings. (set output type to lib rather than dll).
I never tried linking a .obj file directly, it might be possible. Lib files are more standard.