如何链接在Visual C ++ 2008中的静态库? [英] How to link a static library in Visual C++ 2008?

查看:204
本文介绍了如何链接在Visual C ++ 2008中的静态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的VC ++解决方案包括两个项目,一个应用程序(exe)和一个静态库。



编译正常,但无法链接。我从我使用的静态库的每个函数得到一个未解决的外部符号错误。它们看起来像这样:



MyApplication.obj:error LNK2019:未解析的外部符号__declspec(dllimport)int __cdecl MyStaticLibrary :: accept(int,struct sockaddr * ,int *)



应用程序找到的.lib很好,所以这不是问题。我想dllimport是问题 - 为什么会在那里,当我试图构建一个静态库?应用程序和库都使用多线程(/ MT)运行时库,而不是多线程DLL(/ MD)。





我认为一些答案是正确的。被称为UDT的库在主头文件中有这个:

  #ifdef UDT_EXPORTS 
#define UDT_API __declspec(dllexport)
#else
#define UDT_API __declspec(dllimport)
#endif


b $ b

这是否意味着它不是用作静态库?

解决方案

设置它链接?你的MyApplication和MyStaticLibrary :: accept的头文件是什么样子的?



如果你有两个项目在同一个解决方案文件,最好的方法设置它链接是右键单击解决方案文件 - >属性,然后将库设置为应用程序的依赖关系。 Visual Studio将自动处理链接,并确保库构建是最新的,当你建立你的应用程序。



这种错误听起来像你有它的定义作为DLL导入/导出在你的头文件虽然。



编辑:
是的,这是问题。您可能首先将其创建为动态库? (



1)您可以删除所有这些东西以及代码中的任何UDT_API修饰符。



2)您可以删除该项,并添加以下行:

  #define UDT_API 

强大的解决方案是将其更改为:

  #ifdef UDT_STATIC 
#define UDT_API
#else
#ifdef UDT_EXPORTS
#define UDT_API __declspec(dllexport)
#else
#define UDT_API __declspec(dllimport)
#endif
#endif

然后,当要将其用作静态库时,将预处理器指令UDT_STATIC添加到项目中,你想使用它作为一个动态库。 (将需要添加到这两个项目。)


My VC++ solution includes two projects, an application (exe) and a static library.

Both compile fine, but fail to link. I'm getting an "unresolved external symbol" error for each function from the static lib I use. They look like this:

MyApplication.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl MyStaticLibrary::accept(int,struct sockaddr *,int *)"

The app find's the .lib just fine, so that is not the issue. I'm thinking the "dllimport" is the problem -- why would it be there when I'm trying to build a static library? Both the app and library use the "Multi-threaded (/MT)" runtime library, not "Multi-threaded DLL (/MD)".

EDIT:

I think some of the answers are right. The library, which is called UDT, has this in the main header file:

#ifdef UDT_EXPORTS
   #define UDT_API __declspec(dllexport)
#else
   #define UDT_API __declspec(dllimport)
#endif

Does this mean it wasn't meant to be used as a static library?

解决方案

How are you setting it up to link? And what does your header file for MyApplication and MyStaticLibrary::accept look like?

If you have both projects in the same solution file, the best way to set it up to link is to right-click the Solution file->Properties and then set the library as a dependency of the application. Visual Studio will handle the linking automatically, and also make sure that the library build is up to date when you build your application.

That error kinda sounds like you have it defined as a DLL import/export in your header file though.

Edit: Yes, that's the problem. You probably created it as a dynamic library first? (or whoever wrote it did.)

There are a few options.

1) You can just delete all of that stuff, and any UDT_API modifiers in the code.

2) You can delete that stuff and add this line:

#define UDT_API

3) A more robust solution is to change it to this:

#ifdef  UDT_STATIC
    #define UDT_API
#else
    #ifdef UDT_EXPORTS
       #define UDT_API __declspec(dllexport)
    #else
       #define UDT_API __declspec(dllimport)
    #endif
#endif

And then add the preprocessor directive UDT_STATIC to your projects when you want to use it as a static library, and remove it if you want to use it as a dynamic library. (Will need to be added to both projects.)

这篇关于如何链接在Visual C ++ 2008中的静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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