2

I have a .lib static library. I've linked it under the Linker settings 'Additional Library Directories', and 'Additional Dependencies', as well as using pragma comment (lib, "mylib").. And all of that compiles fine.

What I'm asking, and I can only seem to find linking solutions when I look, is how to actually use the functions in it. If I had a function 'MyFunc' referenced in my static library, how could I call it? Visual Studio does not currently recognize any namespaces or functions defined in the library.

Thanks!

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
knoxaramav2
  • 23
  • 1
  • 1
  • 4

2 Answers2

5

You need to get header file for that library, which is usually shipped with the library. After that, you need to include it in your file where you want to use functions from it, and to call functions using declared prototypes.

Your compiler needs to know about prototypes of the functions - because it can't read/parse lib file - that is linker's job.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
  • 1
    Doh, that's what I get for not "loading new answers" when I see the popup. :) – JerKimball Feb 07 '13 at 23:29
  • Hmm, ok. I got the .h file from the project and am using the .lib from the release folder when I compiled it. I have the linker set to the release folder with the .lib, and the 'include additional directories' under c/c++->general at the parent directory with 'MyLib.h'. Now it says [error LNK1104: cannot open file 'MyLib.lib' – knoxaramav2 Feb 07 '13 at 23:39
  • @JerKimball but you would lose approx. 3 seconds to look it up, which is a very long time in SO world - if I clicked that box every time, I would still have 500 of reputation :). – Nemanja Boric Feb 07 '13 at 23:40
  • @knoxaramav2 Apparently you didn't setup linker additional lib path: Linker -> General -> Additional Library Directories. Recheck that to ensure that mylib.lib is in that directory. – Nemanja Boric Feb 07 '13 at 23:41
  • 1
    @Nemanja Boric I looked to check, and I did. I changed it to look at the 'debug' folder instead of the 'release' folder and now it works. Thanks everybody! – knoxaramav2 Feb 07 '13 at 23:45
2

If I understand what you are asking, you need to declare a prototype for your function-that-lives-in-a-lib:

Say your lib has:

int Foo(int bar) { ... }

In your "consumer" where you pragma your lib in, you'd need something that states:

extern int Foo(int bar);

or even just:

int Foo(int bar);

Usually, you do this via Header files (.h files), and for libraries, they're usually referred to as "include files"

JerKimball
  • 16,584
  • 3
  • 43
  • 55