Reputation: 3835
I've figured out how to set VC++ to compile code into a .lib file instead of a .exe, but I'm having trouble getting a lib to link together with my other .obj files.
Here is how I have the library and application folders set up. (I'm not sure if this is right)
AppFolder
App.sln
App.ncb
*.h
*.cpp
Debug
*.obj
App.exe
and somewhere else on the hard drive...
LibraryFolder
lib
Library.lib
include
LibrarySolutionFolder
Library.sln
Library.ncb
*.h
*.cpp
Debug
*.obj
Library.lib
I've been #including the library *.h files from my app's cpp files, and everything compiles fine. It's just when it links I get a list of all the .lib files that are being searched, and Library.lib isn't on there even though I have it listed in VC++ directories. How can I get this to link? (And am I structuring the library folders correctly?)
Upvotes: 3
Views: 7236
Reputation: 158254
From the command line:
cl /EHsc {objfiles}+ /link /LIBPATH:LibraryFolder Library.lib
Where {objfiles}+ means one or more object or cpp files.
Upvotes: 1
Reputation: 13235
To link against a library, you can either:
If you are sure you will only use VC++,
#pragma comment(lib,"C:\\path\\to\\library.lib")`
(Thanks @Nils)
NB: It seems very odd to have your library solution folder inside an 'include' directory: which are really intended for *.h (or other #include
d files).
Upvotes: 1
Reputation: 10560
The VC++ directories is the list of directory locations to searched during linking. It is not a list of libraries to be linked in.
You need to add the lib file to the Additional Dependencies field of the Project Linker settings.
Upvotes: 3
Reputation: 86313
VC does not simply link the library if you include the header-file.
You have to tell the linker to use the library. For good reasons: You alredy have thousands of libs in your library folder. If MSVC had to search all of them each time you link your program it would have to wade through hundrets of megabytes of data.
That would take quite a while, therefore it's not done by default.
For VC you can also give a hint to the linker inside your source. To do so you add the following line somewhere in your source-code (the header of the lib may be a good place).
#pragma comment(lib,"c:\\path_to_library\\libname.lib")
That's not platform independet but the most convenient way to get a lib automatically linked to a project using MSVC.
Another way is to simply add the linker to the project settings. The relevant info can be found lin the linker settings of your project. Don't forget to add the lib to the release and debug configurations.
Upvotes: 0
Reputation: 135245
On the project properties:
Configuration Properties -> Linker -> Input -> Additional Dependancies
Add it in there.
Or, in your .h file for the library, add:
#pragma comment(lib, "Library")
This will do it automatically for you.
Upvotes: 10