静态链接C ++库 [英] Statically linking a C++ library

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

问题描述

我不明白为什么我无法将新编写的C ++静态库静态链接到遗留C ++应用程序中。库项目的配置类型是静态库(.lib)。输出文件指定为$(OutDir)\ $(ProjectName).lib;它实际上是创建的。



主项目在公共属性下的框架和参考部分中引用了库项目。在\ Linker \Input \Additional Dependencies下有一个对.lib文件的引用。



主项目中的源代码包含一个语句< br $>


:: SetupTables();



SetupTables是用__declspec(dllexport)定义的符号在库项目的.cpp文件中。



更奇怪的是,当在主.cpp应用程序文件中键入:: SetupTables()时,IntelliSense会识别该符号并显示名称弹出工具提示中定义它的文件。生成的编译器错误是:



错误C2039:'SetupTables':不是''全局命名空间'的成员'



我缺少什么?

I don't understand why I am unable to statically link in a newly-written C++ static library into a legacy C++ application. The configuration type for the library project is "Static library (.lib)". The output file is specified as "$(OutDir)\$(ProjectName).lib"; it is in fact getting created.

The main project has a reference to the library project in the "Framework and References" section under "Common Properties". Under "\Linker\Input\Additional Dependencies" there is a reference to the .lib file.

The source code in the main project contains a statement

::SetupTables();

"SetupTables" is the symbol defined with a __declspec(dllexport) in the .cpp file of the library project.

What's more peculiar is that, when typing in "::SetupTables()" in the the main .cpp application file, IntelliSense recognizes that symbol and displays the name of the file that defines it, in a pop-up tool-tip. The compiler error generated is:

error C2039: 'SetupTables' : is not a member of '`global namespace''

What am I missing?

推荐答案

(OutDir)\


(ProjectName).lib ;它实际上是创建的。



主项目在共同属性下的框架和参考部分中引用了库项目。 \ linker \Input \Additional Dependencies有一个对.lib文件的引用。



主项目中的源代码包含一个语句



:: SetupTables();



SetupTables是用__declspec(dllexport)定义的符号库项目的.cpp文件。



在主.cpp应用程序文件中键入:: SetupTables()时,更奇怪的是,IntelliSense认识到这个符号和位移是弹出工具提示中定义它的文件的名称。生成的编译器错误是:



错误C2039:'SetupTables':不是''全局命名空间'的成员'



我缺少什么?
(ProjectName).lib"; it is in fact getting created.

The main project has a reference to the library project in the "Framework and References" section under "Common Properties". Under "\Linker\Input\Additional Dependencies" there is a reference to the .lib file.

The source code in the main project contains a statement

::SetupTables();

"SetupTables" is the symbol defined with a __declspec(dllexport) in the .cpp file of the library project.

What's more peculiar is that, when typing in "::SetupTables()" in the the main .cpp application file, IntelliSense recognizes that symbol and displays the name of the file that defines it, in a pop-up tool-tip. The compiler error generated is:

error C2039: 'SetupTables' : is not a member of '`global namespace''

What am I missing?


库的源和生成的目标文件的处理方式与应用程序项目的源文件和目标文件的处理方式完全相同必须告诉链接器包含所有编译目标文件的.lib文件的位置。因此,您不应将__declspec(dllexport)与静态库的符号一起使用。支持静态和动态链接的库在库的公共头中以这种方式完成它们的工作:



Sources and the generated object files of a library are handled exactly the same way as the sources and object files of your application project, you just have to tell the linker the location of the .lib file that contains all compiled object files. For this reason you shouldn't use __declspec(dllexport) with the symbols of static libs. Libraries that support both static and dynamic linking do their job this way in the public headers of the library:

#if MYLIB_STATIC
# define MYLIB_API
#elif DLL_EXPORT
# define MYLIB_API __declspec(dllexport)
#else
# define MYLIB_API __declspec(dllimport)
#endif





如果库用作静态,则定义MYLIB_STATIC。如果您使用lib作为DLL,则仅在编译lib时定义DLL_EXPORT,并且在使用链接到dll的应用程序中的lib的头时不定义DLL_EXPORT。而不是__declspec东西在公共头文件的函数声明中使用MYLIB_API。



MYLIB_STATIC is defined if the library is used as static. If you are using the lib as a DLL then define DLL_EXPORT only when you compile the lib and don't define DLL_EXPORT when you are using the headers of the lib in the application that links against the dll. Instead of __declspec stuff use MYLIB_API in your function declarations in your public header files.


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

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