Linking with DLL in C/C# - c

I've been trying to link to a third-party DLL. I've tried in 3 different ways: by compiling C in Cygwin, by compiling C++ in Visual Studio and by compiling C# in Visual Studio. Everytime I compile the programs, I get the following errors:
Cygwin: undefined reference to '__imp__IEC61850_Create'
Visual S: LNK2019: unresolved external symbol '__imp__IEC61850_Create'
As you might have gathered, I am trying to call the function 'IEC61850_Create' which is found in the DLL, but it always shows up in errors with the '__imp__' prefix. As the DLL is third-party, I can't view the source. In other places I have searched, people usually talk about an accompanying .o or .lib file. In this case, the only resource I have is the .dll.
Has anyone else experienced something like this, or know how I can link to the library? I can provide sample code if needed.
Thanks.

You really need to link to the .lib file, because only it has the __imp__ stubs needed for static linking.
However, what you can do is to create a .def file and use lib to convert it into a .lib file.
Alternatively you can create a dummy project with empty functions to create a .dll and a .lib file, link to the .lib file, but then use the real .dll with the actual program.
For more information you could read Microsoft KB Article 131313.

Related

Linking a lib library in C using Visual Studio 2013

I added to Visual studio Project->Properties->Linker->Input->Additional Dependencies the x.lib file. My build was successful but program didnt start because after start it wrotes: The program cant start because of x.dll is missing from your computer. Why is it looking for x.dll and not x.lib?
For dynamic linking:
x.lib is used for compiling, which contains the linking information of library functions. When compiling, compiler just check whether these functions exist. To understand it simply, compiling will give the way to locating these functions in dll files.
While x.dll is dynamic link library which contains the implementations (maybe not that accurate) of these functions. If you didn't set dll right, the program cannot execute the corresponding functions. dll is the actually executable file, not lib.

How to Include external C library on windows

I am fairly new to C and I am trying to include a external library without using any IDE, only text-editor and the minGW compiler on windows cmd. The library is libPNG in this case, I would really like to understand how the process work not only for this library.
If there is a better way (and easier) to do this, I would also like to know.
You have two parts to take care of:
compilation,
linking.
Compilation
In compilation, when you transform source files in object files, your compiler must know what are the functions provided by the external library.
You could declare each function you use or you can include the library header file(s) in your code:
#incude <library_file.h>
It's not enough, you will have to tell your compiler where it can find this file:
-I<path_to_lib_folder> with gcc
/I<path_to_lib_folder> with cl (the visual studio compiler)
Linking
In linking process, you put the object and library files together to construct an executable file.
You need to tell the linker
what files it must use and
where it can find the library file
You tell the linker what files to use with the -l options, for instance, -lfoo will tell it to search for the libfoo.so lib
Note: with cl you can tell specify which library to use directly in your source code with #pragma comment (lib, "libfoo.lib")
Add you specify where with:
-L<path_to_lib_folder> with gcc
/LIBPATH:<path_to_lib_folder> with link (the visual studio linker)
You can also use dynamic linking, but let's start with the first step.

What is the correct way to add .dll file in a MSVC 2010 project

I need to use the library libMPSSE.dll in my win32 console application project in MSVC 2010. After writing the code I just copied the dll in the folder where my .cpp file is present. I am able to compile successfully but the issue is I am having linking error:
libMPSSE.dll : fatal error LNK1107: invalid or corrupt file: cannot
read at 0x308
Is it really a problem with the dll itself or is there any problem with the dll path. How do we add dll to projects?
They have not provided any .lib file. The complete code is here
These are the usual steps to link to a DLL:
Include the DLL's header file in any of your source files that need to use functions from the DLL. You'll typically need to make sure that your build environment's include path contains the location of the header file. In the IDE you can do this using the Additional Include Directories configuration setting.
Pass the DLL's .lib file to the linker. In the IDE you do this by adding the .lib file to the Additional Dependencies setting. And you'll typically need to add the path to the .lib file to the Additional Library Directories setting.
Having done that, your program should compile and link. To make it run, you'll need to ensure that the DLL itself can be found at runtime. The simplest way to make that happen is to put it in the same directory as the executable file.
My guess, looking at your error message, is that in step 2 you passed the .dll to the linker rather than passing the .lib file.
As said here earlier you can't link to .dll files with C linker directly.
There're win32 APIs that can load the .dll file and return to you pointers to function.
Usually, .dll file is accompanied with .lib file contains code that does this burden for you and provides the API .dll file exposes. All you need is to link to this .lib file and put .dll file near the .exe file created.
Specifically regarding libMPSSE, it's said in its release notes that you can rename the provided .a file to .lib file to link to it in Visual Studio (Project properties->configuration properties->Linker->Input). I tried it and it works as supposed.

Using pcre in a c program on windows

I am trying to build an appliction based upon the pcredemo application. When I try and compile the application in Windows I get the following compiler errors.
undefined reference to `_imp__pcre_compile'|
undefined reference to `_imp__pcre_exec'|
undefined reference to `_imp__pcre_free'|
Am I missing some .dll files or something?
Those are linker errors. You need to make sure that you are passing the PCRE .lib files to the linker. Another possible problem could be that your compiler/linker is using a different .lib file format from that used to build the PCRE .lib files.
If you are linking PCRE statically, you need to #define PCRE_STATIC before compiling.

Why winpcap requires both .lib and .dll to run?

Specifications can be seen here:
http://www.winpcap.org/docs/docs_40_2/html/group__wpcapsamps.html
It's very strange,either .lib or .dll is enough IMO,why does it require both?
In general, you need the .lib for the linker, and .dll at runtime. The .lib file is called an "import library", which contains the glue that tells the linker the functions you're calling can be found in the associated .dll file.
You will probably find that only the .dll file is required at runtime.
This is a widely used layout for Win32 DLL projects and is not limited to Winpcap.
Its not only with winpcap, all external libraries are like that.
When you compiles your source codes which using particular library, you need header files *.h from that library, and you will get *.o files
When you link those *.o files to executables, you will need *.lib or *.dll.a files.
When you run those executable files, you will need *.dll files
If you are calling a Dll you will need an Lib with that. you can see the below link for more info
This is from wikipedia
Linking to dynamic libraries is usually handled by linking to an import library (your .LIB) when building or linking to create an executable file. The created executable then contains an import address table (IAT) by which all DLL function calls are referenced (each referenced DLL function contains its own entry in the IAT). At run-time, the IAT is filled with appropriate addresses that point directly to a function in the separately-loaded DLL.

Resources