Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编译使用dll的代码时未解析的外部_C++_Visual Studio_Dll_Dynamic Linking - Fatal编程技术网

C++ 编译使用dll的代码时未解析的外部

C++ 编译使用dll的代码时未解析的外部,c++,visual-studio,dll,dynamic-linking,C++,Visual Studio,Dll,Dynamic Linking,我正在尝试开始在VS2005中为我的代码使用dll。我的代码非常简单,只是尝试一个测试用例 testdll.h: #ifdef TEST_EXPORTS #define TESTDLLPORT __declspec( dllexport ) #else #define TESTDLLPORT __declspec( dllimport ) #endif namespace TestDLLNS { static int s = 0; class MyTestDll {

我正在尝试开始在VS2005中为我的代码使用dll。我的代码非常简单,只是尝试一个测试用例

testdll.h:

#ifdef TEST_EXPORTS
#define TESTDLLPORT   __declspec( dllexport )
#else
#define TESTDLLPORT   __declspec( dllimport )
#endif

namespace TestDLLNS
{
    static int s = 0;
    class MyTestDll {
    public:
        static TESTDLLPORT int printDLLFuncs();
    };
}
testdll.cpp:

// testdll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "testdll.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

namespace TestDLLNS {
    int MyTestDll::printDLLFuncs() {
        cout << "DLL function called" << endl;
        return s;
    }
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
因此符号显然存在于.dll中。Visual Studio是否也应该创建一个testdllD.lib文件,我应该链接到test.cpp?如果是这样,我如何让VisualStudio同时生成.dll和.lib


编辑:导入/导出是否正确?据我所知,在编译dll时,您可能希望使用dllexport,而在编译使用dll的可执行文件时,将使用dllimport。

有几件事需要注意,希望您没有错过

您遇到的是链接错误,这意味着您的编译是正常的,但存在链接错误。错误表明链接未找到对象文件中引用的printDLLFuncs函数的定义。 您必须提供在项目目录->库路径中定义函数的.lib文件的路径,或者将其放在任何项目文件夹中,以便Visual Studio可以找到它。
您是否将TestDll项目和测试项目放在一个解决方案中?是的,它们在同一个解决方案中,并且共享同一个输出调试目录。当两个项目都在一个解决方案中时,您不需要修改库路径。您可以设置将为您完成此工作的项目依赖项。在我构建testdll时,Visual Studio似乎没有创建testdll.lib。有没有办法创建这个.lib,而不让我的测试应用程序静态链接到.lib?哇!这是一些评论。好了,你告诉Visual Studio创建它了吗?即使在您告诉它您想要一个函数来调用您的函数并且您不喜欢链接错误之后,它也没有创建?我想你在VisualStudio工作了一周。你觉得我的朋友怎么样?好吧,别开玩笑了,你在建什么样的项目?Win32 DLL项目?输出目录中必须有一个与dll同名的.lib。请检查那里。配置类型:Dynamic Library.dll输出目录只创建一个.dll,当我通过将配置更改为Static Library并将测试的链接器指向该目录来告诉它编译.lib时,我仍然有相同的未解决的符号错误。@Christopher:创建静态库和动态库以及使用它们的方法是不同的。你能告诉我你是如何创建这个DLL项目的吗?我创建了一个动态DLL。关于图书馆的道路,你是对的。它将.lib文件输出到另一个位置,一旦我链接到该位置,测试项目就会正确链接并工作。
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "testdll.h"

int main(int argc, char* argv[])
{
    cout << "int: " << TestDLLNS::MyTestDll::printDLLFuncs() << endl;
    cout << "Called dll" << endl;
    return 0;
}
      1    0 0001105F ?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ