views:

25

answers:

2

In Visual Studio (C++) the other day, I was trying to build some example code and it would not work, even though I was pointing at the right include and lib directories. (I got linker errors)

I asked a friend who fixed the problem by specifying the necessary .lib files in the General Properties->Linker->Input field of the project settings.

My questions:

Simply pointing to the directory with the .lib files is not enough? You need to specifically tell the linker which lib files to link?

By listing the .lib files in the "additional dependencies" field, am I specifying exactly which static libs get built into the exe? If the answer to this is yes, then will these be the ONLY lib files that get built into the exe? Why is it called "additional" dependencies? Is there another place to specify lib files to include? Before I thought this was done by including the necessary header file?

Thanks everyone!

Russel

A: 

That is correct, simply pointing at a directory will not cause the .libs in that directory to get linked in (consider the case of the Visual Studio 'lib' folder: there's 60 files in my version of it: imagine if they all got linked in!)

It's called "additional" because there's typically the standard C++ runtime that gets linked automatically. Various #pragmas can cause libraries to get linked automatically as well (this is libraries such as boost do it).

Dean Harding
A: 

Including the header in your source code makes declarations from that header available to the compiler. If the compiler can't find a declaration for a library function (constant, macro, whatever) you used, it can't generate object code that refers to that function.

Once compilation succeeds, you've got object code that has a bunch of symbols in it. In order for the object code to be useful, those symbols actually need to refer to something. That's where the linker comes in -- it resolves the symbols in the code you just compiled to their corresponding object code in the binary libraries you're using.

So, yes, you do need to tell the linker which .lib files to link -- analogous to how you told the compiler (by way of your #include directives) which header files to refer to. If the linker can't resolve a symbol, linking will fail (which I'm guessing is the linker error you got).

I don't actually use Visual Studio, but according to this MSDN article, VS links by default to LIBC.LIB and a whole bunch of other libraries. If you're using some other static library that isn't linked by default, that's an additional dependency.

Meredith L. Patterson