Trouble importing dll library into CodeBlocks Link

2019-06-07 19:51发布

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:

enter image description here

====UPDATE====

  1. 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.

  2. Below is my source code:

include

#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;
}
  1. Below is a screen shot of my CodeBlocks IDE working directory:

enter image description here

  1. Below are screen shots of my build options for Linker/Compiler, and Linker Libraries:

enter image description here

enter image description here

enter image description here

I am not sure what is wrong with the setup. It is having trouble building. It is returning the following error messages:

enter image description here

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)

1条回答
混吃等死
2楼-- · 2019-06-07 20:15

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.

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.

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

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.

Update for OP's further issues

Your program is the supplied example simple.c with the addition of a C++ header, <iostream>.

  • Delete your libcurl project and start again with a clean C project (not C++).
  • 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 the include\curl directory).
  • Do not put anything in Search directories -> Linker.
  • Build the project. It compiles and links for me.

Update #2

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.

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.

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.

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

查看更多
登录 后发表回答