views:

2213

answers:

5

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?)

+10  A: 

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.

1800 INFORMATION
Thanks, it's working now
da_code_monkey
A: 

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.

Nils Pipenbrinck
Using #pragma comment(lib,...) with absolute paths is more than just compiler-specific. It's "your-machine-specific" (unless it refers to a 'subst' drive or network drive mapping). The linker search path option -L is better (ditto for #include and -I). But hey, it's "Works On My Machine" certified.
bk1e
I thought it is obvious that this was ment as an example, bk1e. Do you really think I have a directory named "path_to_library" on my drive? *head hits table*
Nils Pipenbrinck
+3  A: 

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.

jussij
+1  A: 

To link against a library, you can either:

  • List it in Project-> Properties...->Linker, Input->Additional Dependancies (VC++ directories only lets you use just the .lib name rather the full path),
  • Add the library project to your app. solution (On solution, right click -> Add -> Existing Project...), then use Project -> Project Dependancies..., then check your library project (make sure the application project is selected in the drop-down). This is probably the best way to go if you are editting both projects, as VC++ will rebuild the library if it has changed before building your app.
  • 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 #included files).

Simon Buchan
Cool, thanks for the heads up about the include directory.
da_code_monkey
A: 

From the command line:

cl /EHsc {objfiles}+ /link /LIBPATH:LibraryFolder Library.lib

Where {objfiles}+ means one or more object or cpp files.

ceretullis