无法将DLL库导入CodeBlocks链接器 [英] Trouble importing dll library into CodeBlocks Linker
问题描述
我正在尝试使用使用CodeBlocks IDE的简单应用程序使用libcurl.在Codeblocks IDE中,单击构建选项==>链接器设置==>链接库==>添加"后,文件浏览器仅允许我在* .a,*.so,*.lib和*之间进行选择. dyl文件.为什么不允许我选择* .dll文件?我为libcurl下载了Windows二进制软件包,它们都提供.dll文件.看起来像这样:
I am trying to use libcurl for a simple application using CodeBlocks IDE. In Codeblocks IDE, after clicking on Build Options ==> Linker Settings ==> Link Libraries ==> "Add" , the file browser only allows me to choose between *.a, *.so, *.lib, and *.dyl files. Why is it not allowing me to choose *.dll files? I downloaded the binary packages for Windows for libcurl and they all provide .dll files. This is what it looks like:
==== UPDATE ====
-
我现在已经为lib curl下载了以下zip文件 其中包括CURL源,DLL文件和一个.lib文件.有可能 在此处找到: http://www.confusedbycode.com/curl/curl -7.34.0-win64.zip
Hi I have now downloaded the following zip file for lib curl which includes CURL Source, DLL Files, and a .lib file. It can be found here: http://www.confusedbycode.com/curl/curl-7.34.0-win64.zip
但是,我仍然无法编译我的源代码 代码.
However, I am still having issues being able to compile my source code.
以下是我的源代码:
包括
#include <iostream>
#include <stdio.h>
#include "curl/curl.h"
using namespace std;
int main()
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
- 下面是我的CodeBlocks IDE工作目录的屏幕截图:
- 以下是链接器/编译器和链接器库的构建选项的屏幕截图:
我不确定设置有什么问题.建设有麻烦.它返回以下错误消息:
I am not sure what is wrong with the setup. It is having trouble building. It is returning the following error messages:
以下是最新的构建日志:
Below is the latest Build Log:
-------------- Build: Debug in libcurl_c (compiler: GNU GCC Compiler)---------------
mingw32-gcc.exe -Wall -g -IC:\Users\bbb\Desktop\libcurl_packages\confused_by_code\curl-7.34.0-win64\curl-7.34.0-win64\include -c C:\Users\bbb\Desktop\workspace\libcurl_c\main.c -o obj\Debug\main.o
mingw32-g++.exe -o bin\Debug\libcurl_c.exe obj\Debug\main.o C:\Users\bbb\Desktop\libcurl_packages\confused_by_code\curl-7.34.0-win64\curl-7.34.0-win64\lib\libcurl.lib
obj\Debug\main.o: In function `main':
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:9: undefined reference to `_imp__curl_easy_init'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:11: undefined reference to `_imp__curl_easy_setopt'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:13: undefined reference to `_imp__curl_easy_setopt'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:16: undefined reference to `_imp__curl_easy_perform'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:19: undefined reference to `_imp__curl_easy_strerror'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:23: undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
6 errors, 0 warnings (0 minutes, 1 seconds)
推荐答案
程序在运行时会搜索并加载DLL(动态链接库).到
如果要自动执行此操作,则不要链接.dll
本身(不能链接).您将匹配的导入库链接为扩展名.lib
.
A DLL (dynamic link library) is searched for and loaded by your program at runtime. To
make this happen automatically, you do not link the .dll
itself (you can't). You link the matching import library, with extension .lib
.
libcurl.dll
的导入库为libcurl.lib
.如果您已经将cURL
下载并解压到C:\develop\curl-7.34.0-win32
中,则可以在以下位置找到导入库
C:\develop\curl-7.34.0-win32\lib\libcurl.lib
.您应该将此文件添加到库中
为您的Code :: Blocks项目.
The import library for libcurl.dll
is libcurl.lib
. If you have downloaded and extracted cURL
, say, into C:\develop\curl-7.34.0-win32
, then you will find the import library at
C:\develop\curl-7.34.0-win32\lib\libcurl.lib
. You should add this file to the libraries
for your Code::Blocks project.
然后将链接该项目(除非存在其他问题),但是要使其成功运行,必须在运行时搜索DLL的位置之一中找到libcurl.dll
.确保这一点的最简单方法是将libcurl.dll
的副本放置在程序运行所在的目录中.否则,您可以通过研究来确定其位置
动态链接库搜索顺序
The project will then link (unless it has other problems), but in order for it to run successfully it will have to find libcurl.dll
in one of the locations that are searched for DLLs at runtime. The simplest way to ensure this is to place a copy of libcurl.dll
in the directory from which your program runs. Otherwise you can decide on a location for it by studying
Dynamic-Link Library Search Order
您可能很难找到合适的二进制软件包以从中下载
此处可用.一些
其中是cURL
命令行工具的软件包(您不需要)
其中一些是开发二进制文件的包(您确实需要)
适用于各种平台.访问 http://www.confusedbycode.com/curl/并下载
curl-7.34.0-win32.zip
或curl-7.34.0-win64.zip
,具体取决于
定位的是win32
还是win64
.提取档案并
在子目录lib
和dlls
中找到导入库和DLL,
分别.
You may have difficulty in finding the right binary package to download from
the plethora available here. Some
of them are packages of the cURL
commandline tools (which you don't want)
and some of them are packages of the development binaries (which you do want)
for various platforms. Visit http://www.confusedbycode.com/curl/ and download
either curl-7.34.0-win32.zip
or curl-7.34.0-win64.zip
, depending on
whether you are targetting win32
or win64
. Extract the archive and
find the import library and DLL in the subdirectories lib
and dlls
,
respectively.
针对OP的其他问题进行更新
您的程序是提供的示例simple.c
,其中添加了C ++头文件<iostream>
.
Your program is the supplied example simple.c
with the addition of a C++ header, <iostream>
.
- 删除您的
libcurl
项目,然后从一个干净的C
项目(不是C++
)重新开始. - 仅将1个源文件添加到C项目中,例如
simple.c
或它的副本.请勿将其设置为.cpp
文件或进行其他更改.不要将任何其他文件添加到项目中. - 在构建选项-> 链接器设置-> 链接库中,像以前一样将相对路径添加到
libcurl.lib
. li>
- 在构建选项-> 搜索目录-> 编译器(不是链接器)中,将相对路径添加到cURL
include
目录,没有别的(不是include\curl
目录). - 请勿在搜索目录-> 链接器中放置任何内容.
- 构建项目.它会为我编译和链接.
- Delete your
libcurl
project and start again with a cleanC
project (notC++
). - Add only 1 source file to the C project, the example
simple.c
or a copy of it. Don't make it a.cpp
file or otherwise change it. Don't add any other files to the project. - In Build options -> Linker settings -> Link libraries add the relative path to
libcurl.lib
as you did before. - In Build options -> Search Directories -> Compiler (not Linker) add the relative path to the cURL
include
directory and nothing else (not theinclude\curl
directory). - Do not put anything in Search directories -> Linker.
- Build the project. It compiles and links for me.
更新#2
现在的问题是,您试图将64位curl-7.34.0-win64\lib\libcurl.lib
与由32位工具链mingw32
生成的32位目标代码链接.你不能那样做.
The problem now is that you are attempting to link the 64-bit curl-7.34.0-win64\lib\libcurl.lib
with the 32-bit object code generated by your 32-bit toolchain, mingw32
. You can't do that.
在同一站点上用curl-7.34.0-win32
替换curl-7.34.0-win64
的安装.在您的项目中,将your\path\to\curl-7.34.0-win64\lib\libcurl.lib
替换为your\path\to\curl-7.34.0-win32\lib\libcurl.lib
,然后重试.该示例将进行编译和链接.
Replace your install of curl-7.34.0-win64
with curl-7.34.0-win32
from the same site. In your project, replace your\path\to\curl-7.34.0-win64\lib\libcurl.lib
with your\path\to\curl-7.34.0-win32\lib\libcurl.lib
and try again. The example will compile and link.
如果在运行时找到32位libcurl.dll
,并且同样依次由libcurl.dll
动态加载的32位DLL,它也将正确运行.就本示例而言,只需将your\path\to\curl-7.34.0-win32\dlls
中的所有DLL复制到与.exe
相同的目录中.对于cURL
应用程序的常规开发,您需要在系统上安装cURL
库.
It will also run correctly, provided that it finds 32-bit libcurl.dll
at runtime, and likewise the 32-bit DLLs that are dynamically loaded by libcurl.dll
in turn. For the purpose of the example just copy all the DLLs from your\path\to\curl-7.34.0-win32\dlls
into the same directory as the .exe
. For regular development of cURL
apps you would want the cURL
libraries installed on the the system.
由于您首先选择下载64位cURL
,因此您可能希望构建64位
可执行文件(尽管32位可执行文件将在64位主机上运行).你不能那样做
使用您的32位工具链mingw32
.您可以安装64位工具链,例如 TDM-GCC MinGW编译器,
并将其配置为C :: B中的其他工具链.或者,您可以更换您的
使用C :: B 13.12进行C :: B安装,该C :: B已预先配置了TDM-GCC,来自 Sourceforge
Since you chose to download 64-bit cURL
in the first place, you may wish to build 64-bit
executables (although 32-bit executables will run on 64-bit hosts). You can't do that that
with your 32-bit toolchain, mingw32
. You can install a 64-bit toolchain, e.g. TDM-GCC MinGW Compiler,
and configure it as an additional toolchain in C::B. Alternatively you could replace your
C::B installation with a C::B 13.12 one that has TDM-GCC pre-configured, from Sourceforge
这篇关于无法将DLL库导入CodeBlocks链接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!