I have some C code (having various header files and a make file) compiled as a .so file in Linux(Ubuntu) and a python program which calls the functions of this shared object using Ctypes. Now, I want to use this program on windows. In windows, Ctypes needs a .dll file instead of .so in linux.
So, is there a way in which I can convert the .so files of linux into .dll files to be used with Ctypes in Windows ?
No. You will need to recompile the library for Windows.
Related
I've seen from resources that .a files are static library files for WINDOWS Systems, and that .lib files are the equivalent but for UNIX systems. I'm beginning to try to import libraries in projects and saw that for SDL2 specifically, there are .a files that I include with my linking commands with minGW is there something under the hood that is going on for these files to be read and used on a Windows machine?
I've linkedthese successfully (and not-so-successfully a few hours ago),but this bugged me so I wanted to ask you all :)
On Windows machines using MinGW, .a files (also known as static library files) are handled by the linker (ld.exe) during the linking stage of the compilation process. The linker takes the object files (.o) generated by the compiler (gcc.exe) and combines them with the code and data stored in the .a files to create a single executable file. The linker also resolves any symbol references between the object files and the library code and data.
I am using codegen in MATLAB R2016b to generate a .dll file as follows:
codegen -config:dll ex_fun.m -args {0,0,0,0,0,0}
I have tried to include the dll file as a reference using Visual Studio 2015 but nothing works fine and I couldn't use the .lib file as a linker eitherll.
The strange thing is that I could easily use gcc on Linux by compiling the .c script that calls the C function along with the output .so file (which is equivalent to .dll in windows) as follows:
gcc main.c ex_fun.so -Wl,-rpath=$(pwd)
However, I couldn't find any direct method in Windows. How can I use and call a .dll file output by MATLAB in a C main script program?
It should be noted that a .def file is also generated but I don't how to use it along with the output dll file.
In Visual Studio IDE:
Switch platform to "x64"
In Project properties:
C++>General>Additional include directories = ^add compilation folder.
Linker>General>Additional libraries directories = ^add compilation
folder.
Linker>Input>Additional Dependencies = "ex_fun.lib"
In code: add #include "ex_fun.h"
After compilation, copy the "ex_fun.dll" to output folder and run.
If I make headerfile.h and then headerfile.c with the source code for the functions. Once I compile the main C file sampleprogram.c
cc -o sampleprogram headerfile.c sampleprogram.c
Is that source file for the header still needed?
Is headerfile.c and sampleprogram.c compiled and linked together?
Do you need to include the headerfile.h along with sampleprogram if you were to put the program on a usb drive and put it on a different computer?
Headers are not required to execute a program or to use a shared/static library. However, headers are required to write code that uses the interface of a library. Usually, under RedHat distros, you'd have rpms that install the library (libuv) and rpms that install the headers (libuv-devel).
Therefore, without headers, you can execute a program, or you can link against a library. However, if you want to write code that uses the API of a library, you need the headers on your system.
If you compile a program and put it on a USB drive, all you need is the executable. However, you might need to recompile your program as the compiler targeted the specific architecture of the system it ran on. If you move the executable to another computer, it might not work.
Short answer:
No, it is not needed.
Longer answer:
No it is not needed, because when you use #include, the content of the header file will basically be pasted in to the C file. Because of that, the executable will not need the header file nor the c file to run (they are compiled into the executable already by the compiler)
I have seen in driver libraries these three files. How are the three files related, what is the order in which the files are compiled and what is the content of each file? In addition to this I have also seen .a files are they same as .lib?
.lib and .dll files are both containers of executables of a Windows library (.o or .obj files), with the former (.lib) containing stuff (functions, definitions, etc) that you have to link statically to the executable file of your project. The latter (.dll) is either already present in your system or you put it into your system, and it is dynamically linked to the executable file of your project.
For Unix/Linux systems, the file-extensions are .a and .so respectively (that is, .a instead of .lib, and .so instead of .dll).
In all cases, when compiling your project you must #include one or more of the .h files provided to you by the library you are using (they are called header files), because that's where the stuff inside the executables of the library get defined.
EDIT
The main advantage of a statically linked library is that it is self-contained (no external dependencies) but it increases the size of your own executable file. The main disadvantage is that future versions must be re-compiled and re-distributed.
For dynamically linked libraries, we re-distribute just the updated library executables. The main disadvantage is that our program relies on the library being already installed on the customer's system.
I'm currently trying to port a C application to AIX and am getting confused. What are .a and .so files and how are they used when building/running an application?
Archive libraries (.a) are statically linked i.e when you compile your program with -c option in gcc. So, if there's any change in library, you need to compile and build your code again.
The advantage of .so (shared object) over .a library is that they are linked during the runtime i.e. after creation of your .o file -o option in gcc. So, if there's any change in .so file, you don't need to recompile your main program.
But make sure that your main program is linked to the new .so file with ln command.
This will help you to build the .so files.
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
.a are static libraries. If you use code stored inside them, it's taken from them and embedded into your own binary. In Visual Studio, these would be .lib files.
.so are dynamic libraries. If you use code stored inside them, it's not taken and embedded into your own binary. Instead it's just referenced, so the binary will depend on them and the code from the so file is added/loaded at runtime. In Visual Studio/Windows these would be .dll files (with small .lib files containing linking information).
.a files are usually libraries which get statically linked (or more accurately archives), and
.so are dynamically linked libraries.
To do a port you will need the source code that was compiled to make them, or equivalent files on your AIX machine.
They are used in the linking stage. .a files are statically linked, and .so files are sort-of linked, so that the library is needed whenever you run the exe.
You can find where they are stored by looking at any of the lib directories... /usr/lib and /lib have most of them, and there is also the LIBRARY_PATH environment variable.
Wikipedia is a decent source for this info.
To learn about static library files like .a read Static libarary
To learn about shared library files like .so read Library_(computing)#Shared_libraries On this page, there is also useful info in the File naming section.