我如何使用Visual Studio 2010编写一个静态的ANSI-C库 [英] How do i write a static ansi-c library using visual studio 2010

查看:490
本文介绍了我如何使用Visual Studio 2010编写一个静态的ANSI-C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我编写和编译VS2010中我失去了一些东西一个lib文件。我有点不正确地将其标记用于出口。

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

控制台命令 DUMPBIN.EXE -headers MYLIB.LIB> stackoverflow.txt 生成以下输出。 (我删除空行)

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

我创建一个新的空项目。新项目没有pre-编译头一个空的静态库项目。我将其命名为MYLIB。后来我创建两个文件。 C文件上.C结束 - 这标志着它作为一个ANSI-C文件和编译器将其编译为ANSI-C

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

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?

编辑1:

跌落 __ declspec(dllexport)的似乎reasonalbe。

Dropping the __declspec(dllexport) seems reasonalbe.

mylib.h

#ifndef __STACKOVERFLOW_EXAMPLE_
#define __STACKOVERFLOW_EXAMPLE_

int test(int magic);

#endif // __STACKOVERFLOW_EXAMPLE_

现在我试过,但可惜的是它并没有改变结果。在lib文件仍然没有符号。

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

编辑2:

第二部分拼图是我的控制台命令来显示的符号。正确的命令是 DUMPBIN.EXE -all MYLIB.LIB> stackoverflow.txt 。事后的公共符号显露。

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.

推荐答案

尽量省略 __ declspec(dllexport)的,本应使用 DLL文件而已。如果您编译文件,就像

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_

一切都应该罚款。见<一href=\"http://www.$c$cproject.com/Articles/85391/Microsoft-Visual-C-Static-and-Dynamic-Libraries#TheStaticLibrary\"相对=nofollow>这里的例子。

这篇关于我如何使用Visual Studio 2010编写一个静态的ANSI-C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆