问题
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?
回答1:
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.
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
{
// ...
};
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.
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.
来源:https://stackoverflow.com/questions/1179103/visual-studio-2005-linker-problem