0

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:

  1. Added a path to the String.h to the Project - C/C++ -General - Additional Include Directories

  2. Included String.h to some project header

  3. Added a path to the String.lib to the Project - Linker - General - Additional library directories

  4. 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!

4

1 回答 1

1

C++ 没有太多关于二进制格式的标准——甚至没有名称在库中的记录方式。(C++ 编译器喜欢“修改”名称,在其中插入参数类型和返回类型代码等内容。但他们并没有就具体如何做到这一点达成一致。)结果是,来自一个编译器的库很少可移植到另一个编译器除非其中的函数声明为extern "C".

您必须声明您的库函数,或者使用 Visual Studio 编译库(如果您想在其中使用它)。(如果需要,您也可以将库的代码放在头文件中,但听起来您正在尝试拥有一个已经编译的静态库。)

于 2013-05-18T00:15:04.327 回答