Visual Studio 2005链接器问题 [英] Visual Studio 2005 Linker problem

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

问题描述

我主要是Visual Studio的新手,所以如果这是一个基本问题,我深表歉意.我有一个包含许多项目的解决方案.在项目A中,我有一个预先存在的文件集,向其中添加了一个新类.项目B使用项目A中该新类中编码的功能.首先构建项目A,并生成一个.lib文件,并将该.lib文件链接到项目B中.但是,当我去创建.lib文件时for Project BI获取链接错误,引用我添加的Project A中的新功能.在项目A生成的.lib文件中使用"dumpbin"命令时,我注意到添加的功能的符号不存在.但是,在Project A中编译新类后创建的.obj文件确实包含这些符号.知道为什么项目A的.lib文件中没有这些符号吗?

I'm mostly new to Visual Studio, so I apologize if this is a basic problem. I have a solution which contains a number of projects. In project A, I have a pre-existing set of files that I added a new class to. Project B uses the functionality coded in that new class in Project A. Project A is built first, and a .lib file is generated, and that .lib file is linked into Project B. However, when I go to create the .lib file for Project B I get a link error, referencing the new functionality in Project A that I added. Using 'dumpbin' command with the .lib file generated from Project A, I notice that the symbols for the functions that I added aren't there. However, the .obj file created after compiling the new class in Project A does contains those symbols. Any idea why those symbols aren't then present in Project A's .lib file?

推荐答案

我假定它们都是DLL项目.在Windows上,您需要通过用__declspec(dllexport)装饰来导出符号,或者需要使用.DEF文件来指示要导出的符号.

I assume that these are both DLL projects. On Windows you need to either export the symbols by decorating them with __declspec(dllexport), or you need to use a .DEF file to indicate which symbols to export.

对于__declspec,通常可以通过创建类似以下内容的标头来实现:

With __declspec, this is usually accomplished by creating a header something like this:

#ifdef PROJECT_A_EXPORTS
#define PROJECT_A_API __declspec(dllexport)
#else
#define PROJECT_A_API __declspec(dllimport)
#endif

然后,当您要导出类的所有成员时,请写:

Then when you want to export all members of a class, you write:

class PROJECT_A_API MyClass
{
   // ...
};

并在编译项目A而不是项目B时定义PROJECT_A_EXPORTS.这样,您可以在两个项目之间共享相同的MyClass.h头文件.在编译项目A时,将导出符号,在编译项目B时,将导入符号.

And define PROJECT_A_EXPORTS when compiling Project A but not Project B. That way, you can share the same MyClass.h header file between both projects. When compiling Project A, you'll export the symbols, and when compiling Project B, you'll import the symbols.

以下是有关DEF文件路由的一些信息.但是,这可能很难维护,在C ++中,您需要列出每个符号的修饰名称,这可能会很麻烦.

Here's some information on the DEF file route. This can be hard to maintain, though, and in C++ you need to list each symbol's decorated name, which can be a pain.

这篇关于Visual Studio 2005链接器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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