I'm tying to learn how to make and use a static library and I've faced some problems. This is what I've done.
First I've written some code and placed in into String.h
and String.cpp
files.
Then I've compiled it into an object file:
mingw32-g++ -c -O2 -s -DNDEBUG String.cpp -o .\obj\String.o
Then I've archived(?) it:
ar cr .\lib\String.lib .\obj\String.o
And indexed(?) it:
ranlib .\lib\String.lib
After that I've successfully compiled and linked the tests with mingw:
mingw32-g++ -std=c++03 -Wall -O2 -s -DNDEBUG .\test\src\test.cpp .\lib\String.lib -o .\test\bin\test.exe
The test compiled, linked and ran perfectly.
After that I wanted to include this library into my MSVS12 project. I've:
Added a path to the String.h
to the Project - C/C++ -General - Additional Include Directories
Included String.h
to some project header
Added a path to the String.lib
to the Project - Linker - General - Additional library directories
Added String.lib
to the Project - Linker - Input - Additional dependencies
After all these steps when I try to build the project the linker gives me many LNK2011
and LNK2019
errors. It seems to me that it can not find the implementation of my functions...
Please, tell me what I'm doing wrong and how can I fix it. Thanks!
C++ doesn't have much of a standard regarding binary formats -- not even how names are recorded in a library. (C++ compilers like to "mangle" names, inserting things like argument- and return-type codes into them. But they don't agree on exactly how to do that.) Result being, libraries from one compiler are rarely portable to another compiler unless the functions in them are declared as extern "C"
.
You'll have to either declare your library functions as such, or compile the library with Visual Studio if that's where you want to use it. (You could also put the library's code in a header file if you wanted, but it sounds like you're trying to have an already-compiled, static library.)