Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a dll from a c++ program. (borland c++ builder and in general)

I'm trying to use a dll, namely libcurl, with my program, but, it's not linking. Libcurl comes with .h files that I can include (takes care of dllimport), but then I guess I must specify which dll to actually use when linking somehow... How do I do that? I'm compiling with Borland C++ builder, but I really want to know how these things work in general...

EDIT: This is the code (straight c/p from curl homepage)

bool FTPGetFile::ConnectToFTP(string ftpServer){
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
    res = curl_easy_perform(curl);
    return true;
}else{
    return false;
}
} 

And here are the errors:

[Linker Error] Error: Unresolved external '_curl_easy_init' referenced from C:\PROJECTS\PC\TOOLBOX\DEBUG_BUILD\FTPGETFILE.OBJ
[Linker Error] Error: Unresolved external '_curl_easy_setopt' referenced from C:\PROJECTS\PC\TOOLBOX\DEBUG_BUILD\FTPGETFILE.OBJ
[Linker Error] Error: Unresolved external '_curl_easy_perform' referenced from C:\PROJECTS\PC\TOOLBOX\DEBUG_BUILD\FTPGETFILE.OBJ

EDIT 2: As per suggestion from joe_muc I have made the lib files with the implib tool and included them in the linker path. I still get the same errors.

like image 681
c0m4 Avatar asked Nov 28 '25 22:11

c0m4


2 Answers

As mentioned, you will need the static .lib file that goes with the .dll which you run through implib and add the result lib file to your project.

If you have done that then:

  • You may need to use the stdcall calling convention. You didn't mention which version of Builder you are using, but it is usually under Project options - Advanced compiler - Calling convention.
  • You may need to enable MFC compatibility option.
  • It could be a name mangling issue (Project options - C++ compatiblity options).
  • Or generation underscores
  • Or advanced linker options "Case_insensitive-link" on or off

libcurl appears to be C code; but if it is C++, note that Microsoft and Borland classes are generally not compatible. There are supposed to be tricks to get them to work, but it is a real mess to deal with and I have never had success. C coded DLLs should work.

like image 164
Roger Nelson Avatar answered Nov 30 '25 12:11

Roger Nelson


Use "Depends.Exe" to browse the .DLL, and find the exact names of the functions that you want to use, then add the .lib file for the dll (or create it using borland's implib), and include the .lib file in your bpr project file, by right-clicking and adding it to the project. then at the top of your code, use __declspec(dllimport) to please the linker gods.

like image 21
Joshua Avatar answered Nov 30 '25 11:11

Joshua



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!