构建dll并链接到它?

构建dll并链接到它?,dll,mingw,codeblocks,dynamic-linking,Dll,Mingw,Codeblocks,Dynamic Linking,我在XP SP3上的代码块10.05 mingw上,我基本上有下面构建的dll和lib文件。我遇到的问题是我试图链接到库的第二部分。我创建了一个控制台应用程序,并使用了以下源代码: #包括 #包括“示例_dll.h” 内部主(空) { 你好(“世界”); printf(“%d\n”,双(333)); CppFunc(); 我的a级; a、 func(); 返回0; } 然后,我通过添加-L.-lexample\u dll来设置链接器设置,并且我还链接到库文件libexample\u dll.a

我在XP SP3上的代码块10.05 mingw上,我基本上有下面构建的dll和lib文件。我遇到的问题是我试图链接到库的第二部分。我创建了一个控制台应用程序,并使用了以下源代码:

#包括
#包括“示例_dll.h”
内部主(空)
{
你好(“世界”);
printf(“%d\n”,双(333));
CppFunc();
我的a级;
a、 func();
返回0;
}
然后,我通过添加
-L.-lexample\u dll
来设置链接器设置,并且我还链接到库文件libexample\u dll.a,就像我与其他许多成功使用相同设置的库一样。然后当我尝试链接可执行文件时,我得到一个错误

 C:\example_dll_client\example_dll_client.cpp|2|error: example_dll.h: No such file or directory|
C:\example_dll_client\example_dll_client.cpp||In function 'int main()':|
C:\example_dll_client\example_dll_client.cpp|6|error: 'hello' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|7|error: 'Double' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|8|error: 'CppFunc' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|10|error: 'MyClass' was not declared in this scope|
C:\example_dll_client\example_dll_client.cpp|10|error: expected ';' before 'a'|
C:\example_dll_client\example_dll_client.cpp|11|error: 'a' was not declared in this scope|
||=== Build finished: 7 errors, 0 warnings ===|
我还包括了用于构建dll和lib文件的两个文件(不是到项目,而是到这个站点作为参考。我只有错误源作为整个项目,否则dll的意义是什么?)

//示例_dll.cpp
#包括
#包括“示例_dll.h”
__stdcall void hello(const char*s)
{
printf(“你好%s\n”,s);
}
整数倍(整数x)
{
返回2*x;
}
作废CppFunc(作废)
{
看跌期权(“CppFunc”);
}
void MyClass::func(void)
{
puts(“MyClass.func()”);
}
//示例\u dll.h
#ifndef示例\u DLL\u H
#定义示例_DLL _H
#ifdef_uucplusplus
外部“C”{
#恩迪夫
#ifdef构建示例DLL
#定义示例\u DLL\u declspec(dllexport)
#否则
#定义示例DLL declspec(dllimport)
#恩迪夫
void\u stdcall示例\u DLL hello(const char*s);
int示例_DLL Double(int x);
#ifdef_uucplusplus
}
#恩迪夫
//注意:此函数未声明为外部“C”
无效示例_DLL CppFunc(无效);
//注意:此类不能声明为外部“C”
类示例\u DLL MyClass
{
公众:
MyClass(){};
虚拟~MyClass(){};
无效函数(void);
};
#endif//EXAMPLE\u DLL\u H
编辑:

我对DLL和lib文件的编译做了以下更改

我首先确保我的构建目标是一个动态库,通过右键单击项目管理器树中活动项目的属性,可以在“构建目标”选项卡下找到它。我还检查了创建导入库和check.def导出文件。然后我进入了代码块构建选项,在我添加的编译器设置->其他选项选项卡下

-c -共享

在我添加的#定义选项卡下

构建示例DLL 构建动态链接库

我的链接器设置在链接库和链接器设置下都有user32 -姆温多斯 --外显

构建现在应该编译并链接,没有错误或警告

为了使用int main()编译源代码,我包括了头文件和以下设置:

在链接库下有libexample_dll.dll.a,在链接器设置下有libexample_dll.dll.a

-L。
-lexample_dll对我来说,似乎exe和dll有不同的目录

所以
L.
不好。应该是
lpoint到libexample\u dll.a文件夹

可以复制exe目录中的example_dll.h,也可以使用
-IpointsToTheexample_dll.hFolder

可以肯定的是,由于命令不正确,执行不起作用。
我做了一个makefile

测试makefile

  • 创建一个新目录
  • 复制其中的example_dll.h、example_dll.cpp、example_dll.cpp文件
  • 在文件makefile(在同一目录中)中安全地执行以下代码
生成文件

# Environment 
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin


# build
builddll:
    g++ -c -DBUILDING_EXAMPLE_DLL example_dll.cpp
    g++ -shared -o example_dll.dll example_dll.o -Wl,--out-implib,libexample_dll.a

buildexe:
    g++ -c example_exe.cpp
    g++ -o example_exe.exe example_exe.o -L. -lexample_dll

clean:
    rm -f libexample_dll.a example_exe.exe example_dll.dll
由于复制和粘贴错误。
在使用此makefile之前,请将命令(g++和rm)前面的所有空格替换为两个选项卡。
否则你会犯这样的错误<代码>生成文件:9:**缺少分隔符。停止。

执行以下命令

  • 生成builddll
  • 生成buildexe
现在您可以测试示例_exe.exe

只是因为完整性,也有一个使干净

  • 澄清
更新

如果要使用不带头文件的DLL,必须了解所有DLL函数

这里有一个loadDll.cpp,它加载DLL并调用
Double

/* 
 * File:   loadDll.cpp
 * Author: Administrator
 *
 * Created on 19. Februar 2013, 22:42
 */

#include <cstdlib>
#include <windows.h>
#include <stdio.h>

using namespace std;

typedef int (WINAPI *PFNDOUBLE)(int x);
PFNDOUBLE idlldouble;
HINSTANCE dllctrl_inst;

int main(int argc, char** argv) {
    puts("---start---");
    dllctrl_inst = LoadLibrary( "example_dll.dll" );
    if( dllctrl_inst != NULL ) {
      idlldouble = (PFNDOUBLE)GetProcAddress( dllctrl_inst, "Double" );
    }
    if( idlldouble != NULL ) {printf("%d\n", idlldouble(333));}
    return 0;
}
/*
*文件:loadDll.cpp
*作者:管理员
*
*创建于19日。2013年2月22:42
*/
#包括
#包括
#包括
使用名称空间std;
typedef int(WINAPI*PFNDOUBLE)(intx);
p双倍空转双倍空转;
HINSTANCE dllctrl_inst;
int main(int argc,字符**argv){
看跌期权(“---开始---”;
dllctrl_inst=LoadLibrary(“example_dll.dll”);
如果(dllctrl_inst!=NULL){
idlldouble=(PFNDOUBLE)GetProcAddress(dllctrl_inst,“Double”);
}
如果(idlldouble!=NULL){printf(“%d\n”,idlldouble(333));}
返回0;
}

错误的第一行是一个很好的提示。您是否指定了
示例_dll.h
所在的include目录?(使用
-I
编译器开关)。是的,但是除了lib文件之外,难道不可能编译我的int main()代码吗?或者,我如何只使用.DLL文件编译源代码?不管怎样,头文件只包含函数调用等,没有其他内容。cpp文件包含实际操作。@lost_with_coding请看一下我的更新答案。但是,没有什么方法可以用int main()编译源代码,只使用dll文件。页面提到了它,或者我还需要示例_dll.h文件吗?MicrosoftsAPI通常是封闭源代码的,除了windows.h之外没有任何头文件,但这是链接的,对吗?完整的FacePalm!查看我的帖子下面的评论。昨晚发布这篇文章时我很累,没有意识到int main()源代码中没有包含cpp文件,而.h文件只包含函数调用等。不过感谢您的帮助。