How do i write a static ansi-c library using visual studio 2010

做~自己de王妃 提交于 2020-01-02 08:32:45

问题


When I write and compile a lib file in VS2010 I am missing something. I somehow do not mark them correctly for export.

The console command dumpbin.exe -headers mylib.lib > stackoverflow.txt generates the following output. (I removed empty lines)

 Microsoft (R) COFF/PE Dumper Version 10.00.30319.01 Copyright (C)
 Microsoft Corporation.  All rights reserved.
 Dump of file mylib.lib
 File Type: LIBRARY
 ANON OBJECT HEADER VALUES
                1 version
              14C machine (x86)
         515AC7B8 time date stamp Tue Apr 02 13:57:44 2013
                  ClassID: {0CB3FE38-D9A5-4DAB-AC9B-D6B6222653C2}
             171A size
                0 flags

I create a new empty project. The new Project is an empty static library project without pre-compiled headers. I name it mylib. Afterwards I create two files. The c file ends on .c - this marks it as an ansi-c file and the compiler will compile it as ansi-c.

mylib.h

#ifndef __STACKOVERFLOW_EXAMPLE_
#define __STACKOVERFLOW_EXAMPLE_

__declspec(dllexport) int test(int magic);

#endif // __STACKOVERFLOW_EXAMPLE_

mylib.c

#include "mylib.h"

int test(int magic){
    return magic * 7;
}

What could possibly go wrong here? Why does my lib file contain no symbols?


EDIT 1:

Dropping the __declspec(dllexport) seems reasonalbe.

mylib.h

#ifndef __STACKOVERFLOW_EXAMPLE_
#define __STACKOVERFLOW_EXAMPLE_

int test(int magic);

#endif // __STACKOVERFLOW_EXAMPLE_

I tried this now but unfortunatelly it did not change the outcome. Still no symbols in the lib file.


EDIT 2:

the second part to the puzzle was my console command to reveal the symbols. The correct command is dumpbin.exe -all mylib.lib > stackoverflow.txt. And afterwards the public symbols are revealed.


回答1:


Try to omit the __declspec(dllexport), this shall be used for DLLs only. If you compile your files just like

#ifndef __STACKOVERFLOW_EXAMPLE_
#define __STACKOVERFLOW_EXAMPLE_

int test(int magic);

#endif // __STACKOVERFLOW_EXAMPLE_

everything should be fine. See here for an example.




回答2:


Without even knowing what kind of options you used to compile this it's hard to even try to give an answer.

But, no matter what you are trying, VS offers really poor support for the C language, some features or versions of the C language are not available at all inside VS, VS is well know to not be a really good place for C.

I suggest to switch to MinGW and stick to it.




回答3:


In 2013, with Visual Studio 2010, I'm getting non-empty static libraries created, but neither dumpbin nor lib produce any output whatsoever, whether I say /all, -all or /disasm, one of which should say something.

File says "current ar archive" on Windows, and on linux, $ ar tvf MathFuncsLib.lib rw-rw-rw- 100666/100666 16252 Jan 13 15:29 2014 Debug/MathFuncsLib.obj

And yes, this is the trivial math library Microsoft uses for their walk-through example (:-))



来源:https://stackoverflow.com/questions/15765321/how-do-i-write-a-static-ansi-c-library-using-visual-studio-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!