Constructing import librariesPosted: October 3, 2013
A user just posted an excellent question/bug to our user forums. Basically, the user was attempting to link a DLL to another DLL created by Simply Fortran, and there were some issues related to the linker finding the first library. The user was attempting to have the linker automatically find the DLL for linking, and Simply Fortran wasn’t cooperating. He used the prescribed linker flag -lone to specify that the library libone should be linked to in his second project, but the only product of the libone project was libone.dll.
The resolution was to create a true import library in the libone project. An import library will just contain a listing of the exported symbols available in the shared library, libone.dll. Currently (as of version 2.3) Simply Fortran does not automatically create import libraries when building DLLs, but it probably should do so in the future to make everything easier.
In the meantime, creating an import library is still pretty straightforward. In any DLL project, you can instruct the linker to output a GNU-compiler-compatible import library by adding a flag under Project Options. In the image below, I’ve already done so:
In the options, I’ve set my Target Name to libone.dll as I described earlier. Under Project Flags, I’ve also added an additional flag to the default flags set up by Simply Fortran:
The flag above is simply asking the linker directly (hence the -Wl, prefix) to generate an import library named libone.dll.a. When I clean and build my project, I now should see libone.dll and libone.dll.a in my project’s root directory.
To use this import library in other projects, I now need to use a slightly different flag in the linker options. If I were linking to a static libone.a library, the flag -lone would be sufficient. In this case, however, I want to use libone.dll rather than statically linking to an equivalent library. Therefore, if I’ve created my import library as described, I would instead use the flag -lone.dll. Now the linker will load and link to libone.dll.a, but the resultant project will require and use libone.dll.
The important bit of information that came out of this bug report is that Simply Fortran probably should handle creating import libraries automatically. Most every other compiler package on Windows does so in order to make life easier for users, and we of course want to do the same.