C++ DLL 无法链接?
好吧,问题就到这里了。为了提高我对 C++ API 的了解,我尝试了解有关库文件类型的更多信息。我已经很好地解决了静态库问题。您只需将库链接到项目中,编译和链接时其内容就会放置在二进制文件中。但是,当我开始尝试使用动态库时遇到了一个问题。我能够使用 __declspec 函数成功编译 DLL 文件,并创建 extern 块(因此可以成功导出到 C++)。但是,当我尝试链接该文件时,问题就出现了。根据我在网上看到的多个教程,我创建了一个带有前向定义的标头,并将其包含到可执行项目中。然后,我确保将搜索目录以及我要导入的库的路径添加到项目构建设置中。然后,当我去构建项目时,它会产生链接器错误(我可以告诉,因为目标文件已编译)“找不到-l [文件路径]”。是否还需要进行更多配置?我认为它与我的编译器(MinGW)有关,因为 Code::Blocks(我当前正在使用 Eclipse CDT 插件)产生了类似的链接错误。
如果您需要查看代码,我将在下面提供。
// EXE: main.cpp
#include <iostream>
#include "DLLTutorial.h"
int main()
{
Function();
std::cout << "1 + 3:\t" << Add(1, 3);
}
// DLL: DLLTutorial.cpp
#define DLL_EXPORT
#include <iostream>
#include "DLLTutorial.h"
extern "C"
{
DLLCOMP int Add(int a, int b)
{
return a + b;
};
DLLCOMP void Function(void)
{
std::cout << "DLL Called!\n";
};
};
// DLL: DLLTutorial.h
#ifndef DLLTUTORIAL_H_
#define DLLTUTORIAL_H_
#include <iostream>
#ifdef DLL_EXPORT
#define DLLCOMP __declspec(dllexport)
#else
#define DLLCOMP __declspec(dllimport)
#endif
extern "C"
{
DLLCOMP int Add(int a, int b);
DLLCOMP void Function(void);
};
#endif /* DLLTUTORIAL_H_ */
这只是我从网站上使用的非常简单的示例代码。我无法正确链接可执行文件,因此我将不胜感激任何可以给我的建议。
另外,我有一个相关的问题。阅读一些在线教程,其中一些提到 __declspec 函数是 Windows/Microsoft 特定的。这是真的吗?在这种情况下,在其他操作系统上使用什么是一个很好的等效方法?
Alright, here's the issue. In an attempt to advance my knowledge of C++ APIs, I attempted to learn more about library file types. I have static libraries figured out quite well. You simply link the library into the project, and its contents are placed inside the binaries when it's compiled and linked. But, I hit an issue when I started trying to use dynamic libraries. I was able to successfully compile a DLL file using the __declspec function, and creating extern blocks(so it can successfully be exported to C++). But, the issue arises when I try to link the file. Based on multiple tutorials I have seen across the web, I create a header with forward definitions, and include it into the executable project. Then, I make sure to add the search directory to the project build settings, along with the path to the library I am importing. Then, when I go to build the project, it produces a linker error(I can tell because the object file is compiled) "cannot find -l[path to file]". Are there more configurations to be made? I assume it has something to do with my compiler(MinGW), because Code::Blocks(I'm currently using the Eclipse CDT plugin) produced a similar link error.
If you need to see the code, I will provide it below.
// EXE: main.cpp
#include <iostream>
#include "DLLTutorial.h"
int main()
{
Function();
std::cout << "1 + 3:\t" << Add(1, 3);
}
// DLL: DLLTutorial.cpp
#define DLL_EXPORT
#include <iostream>
#include "DLLTutorial.h"
extern "C"
{
DLLCOMP int Add(int a, int b)
{
return a + b;
};
DLLCOMP void Function(void)
{
std::cout << "DLL Called!\n";
};
};
// DLL: DLLTutorial.h
#ifndef DLLTUTORIAL_H_
#define DLLTUTORIAL_H_
#include <iostream>
#ifdef DLL_EXPORT
#define DLLCOMP __declspec(dllexport)
#else
#define DLLCOMP __declspec(dllimport)
#endif
extern "C"
{
DLLCOMP int Add(int a, int b);
DLLCOMP void Function(void);
};
#endif /* DLLTUTORIAL_H_ */
It's just very simple example code I used from a website. I cannot get the executable file to link properly, so I would appreciate any advice that could be given to me.
Also, I have a related question. Reading a few of the tutorials online, some made mention about the __declspec function being Windows/Microsoft specific. Is this true? In which case, what would be a nice equivalent to use on other operating systems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您构建 dll 时,您还创建了一个伴随的 .lib 文件,即“implib”。您必须在 exe 中静态链接此文件。
编辑: __cdeclspec(...) 确实是微软。不知道其他平台怎么样。
When you build a dll you also create a companying .lib file, the 'implib'. You must statically link this file in your exe.
EDIT: __cdeclspec(...) is indeed Microsoft. Dont't know about other platforms.
您需要告诉编译器在哪里搜索您的库和包含内容。在 GCC 和 MinGW 中,这是通过使用
-I
包含、使用-L
库来实现的。我认为您没有必要生成 .lib (或 .a,因为它通常在 *nix 和 GCC 中),GCC 应该能够在 dll 中找到您的函数。例如,构建并链接共享库:
然后,假设所有文件都在同一目录中,构建并链接 exe 并告诉 GCC 使用您的库:
注意
-l
和 < code>-L,一个告诉 GCC 使用哪些库,另一个告诉在哪里寻找它们。在 Eclipse CDT 中,您可以在以下位置找到这些设置:项目属性 -> C/C++ 构建 ->设置-> MinGW C++ 链接器 ->图书馆。另外,
__declspec
被 MinGW 识别,但我认为它是 Microsoft 特有的。You need to tell the compiler where to search for your libraries and includes. In both GCC and MinGW this is achieved by using
-I
for includes, and-L
for libraries. I don't think its necessary for you to generate the .lib (or .a as it usually is in *nix and GCC), GCC should be able to find your function in the dll.For example, build and link the shared lib:
Then, assuming all the files are in the same dir, build and link the exe and tell GCC to use your lib:
Note the difference between
-l
and-L
, one tells GCC what libs to use, while the other tells where to look for them. In Eclipse CDT you find these settings in: Project properties -> C/C++ Build -> Settings -> MinGW C++ Linker -> Libraries.Also,
__declspec
is recognized by MinGW, but I think its Microsoft specific anyway.