Compaq Fortran Visual Studio

Compaq Fortran Visual Studio

Does anyone know the details of how to create a Fortran Static Library using the Compaq Fortran Visual Studio?

There is a so-called example in the SAMPLES file that comes with the CD-ROM, but there is no explanation at all. My specific questions are:

1. When is it necessary to use the MODULE -- END MODULE statements, and exactly how are they used? When I insert these into the block of code that is to become my library, the compiler balks and says 'illegal statement'. No discussion of this point in the HELP documentation so far as I could find.

2. How come the "NEW --- PROJECT" option does not seem to support the creation of Fortran Static Libraries? I choose the Project Type to be Fortran Static Library, give the library name. Next, I choose 'build' and compile the module from the source code. This works fine, and I do get a " .LIB" file out of this, but when I attempt to use it (i.e. - compile a separate program that calls a subroutine in the LIB file), I get the message 'Library not found'. Is it possible to do what I want to do? No discussion of this in the HELP documentation.

3. The HELP documentation talks about '.MOD' files. If you look for one of the standard Fortran libraries, for example DFLIB, you will find that it is stored as 'DFLIB.MOD'. How is this different from a '.LIB' file, and when do I need to use it? Again, no help from the HELP documents.

4. Exactly how does the calling program know where to look for a new library? Can I store my .LIB file anywhere? Where and how do I specify a path name for this -- When I compile the calling program? -- When I generate the library? -- When I generate the .MOD file? No discussion of this in the HELP documentation.

18 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I have some of the same questions but may be able to help with some others.

The questions I have are the same as 2nd half of question 2 and all of question 4. Basically, how does one reference a static library within a main program and how do you configure CVF (environment, workspace, project, etc. ?) to know where to find the library. I seem to be able to build static libraries but I am then unable to use them successfully. It's possible that I haven't built the libraries correctly, but the process seems to work (no errors).

The questions I can answer (I think) are:

1. MODULE -- END MODULE is needed in any source file that is not a main program, i. e., that begins and ends with PROGRAM -- END PROGRAM. In CVF a 'project' is synonymous with a main program (except for Static Library and DLL projects). All projects are contained in a workspace and a workspace may have more than one project. I've created a Fortran Static Library project inside the same workspace as the various Fortran Console Projects that I want to have use it. As indicated, I have had no luck getting the compiler/linker to find, recognize, and successfully use the library.

2. What version of CVF are you using? I started with 6.1 (Pro) and have upgraded to 6.6.A (Pro). I have Fortran Static Library available as a project type. Once you have created a Fortran Static Library project you can populate it with source files containing modules. You can import existing files using the "Add files to project..." feature. You may be able to create new files within the Static Library project as well, but I usually create them elsewhere and test them before adding them to the Library.

Step 1. Start a library project. Create a file within it containing the source code for your user-defined function. Example:

module logarithm
function ln(x)
real ln
real x
ln = log(x)
end function
end module

Step 2. Compile and build your library project. This will generate a subdirectory called debug under which you will find a .mod, and .obj and a .lib file. Put these files in a directory of your choice. E.g. suppose you put it in c:mylibs

Step 3. Now you want to enable the compiler to find mylibs no matter what the current workspace is. Go to Tools-Options-Directories. Add c:mylibs to 'Include files' and 'Library files'.

Step 4. Open a separate workspace where you have a main program from which you want to call the user-defined functions in mylibs. The main program should start with the command

use logarithm

which ensures that you have access to the subroutines in the module logarithm.

Step 5. To make sure that your main program finds the library, go to Project-Settings-Link. Beside kernel32.lib, put a space and then the name of your .lib file. This apparently has to be done in every project from which you want to call the library in question.

Step 6. Compile, build and run your main program.

I suspect Step 5 can be avoided, but I don't know how.

I turns out that you can ignore the .obj file.

I have a question. Suppose my source files don't have modules. Each file just contains a single subroutine. This is the case with LAPACK, for example. Then it is easy enough to create the .LIB file. But then what?

I am somewhat puzzled that you say that there is "No discussion of this in the HELP documentation" because there is really quite a bit of discussion of these matters in the documentation. You should probably start with the Programmer's Guide chapter on Building Programs and Libraries, especially the section Types of Projects. Here is where the Fortran Static Library project type is described, with details on how you create and use libraries.

To take your questions in order:

1. MODULE..END MODULE. Modules don't have any direct connection with libraries, though if you are developing a set of modules for use by other projects, a Fortran Static Library project type is ideal. In Fortran 90, modules are how you create a set of precompiled declarations of variables, routines, types and constants that can then be made available, through a USE statement, when compiling other Fortran code. Modules do not in themselves have actual code or data - they are declarations. You can think of them sort of as INCLUDE files, though they are much more than that. When you compile a module, you also get a compiled object file (.OBJ) which must be linked in with your application. However, it seems to me that you're not really interested in modules - if you want to learn more, see any good Fortran 90/95 tutorial book or the Compaq Fortran Language Reference Manual section 8.3.

2. The Programmer's Guide section on the Static Library project type tells you how you refer to the .LIB. The simplest way is to add the .LIB file to your executable project as if it were a source file. Another way is to list it in Project..Settings..Link..Object/Library Files. A third way, if you have a workspace with both your executable and library project in it, is to use Project..Dependencies to make the library project a dependent project (subproject) of the executable project.

3. .MOD files are discussed in the Programmer's Guide - look in the index under .MOD Files. Specifically, they are mentioned in the section on Files In A Project. .MOD files are created by compiling modules (see #1 above). They must be available to the project that uses them - if it's in the same project, that's automatic, otherwise you need to specify the path to where they can be found under Project..Settings..Fortran..Preprocessor..INCLUDE/USE paths (.MOD files are what the compiler looks for when you have a USE stetement.)

4. I covered this in #2 above. It is spelled out in the Programmer's Guide - you have many options here. The .LIB file can be anywhere. If you don't give a path name when using the Project..Settings..Link box, the linker looks in the list of directories shown at Tools..Options..Directories..Library Files.


Retired 12/31/2016

I basically followed the steps in reply 2 (kleinp March 31,2002 2:28pm) and IT WORKED! Thanks. I had previously tried adding the path to the library files in Tools-Options-Directories-Libraries, but I had not added the path to the include files. That must have made the difference. BTW: I left the library files (*.lib, *.mod, *.obj) in the Debug directory where they were created. Also, the project I created to call the library was in the same workspace.

I assume Steve L ( COMPAQ) is the same Steve L who answered my e-mail concerning printing colorized fortran source files. If so, thanks for both these replys (e-mail and message board).

I appreciate the clarification in your points 1. and 3. regarding modules and the Language Reference Manual section 8.3. Your point 4 just confirms what I knew had to be the case, that libraries should be able to go anywhere.

As regards your introductory comments and point 2., I also have an e-mail exchange going with Lorri M (COMPAQ) regarding static libraries and I will post a reply here if I find out something additional. What I want to add to this discussion thread at this point is that I have tried ALL of the things you suggest, separately and in combination, and none of them worked. Specifically:

A) I have my static library project and my main project in the same workspace. Selecting the static library as a dependency in the main project does NOT make the library link successfully, even when I put the library name in the Project-Settings-Link "Object Modules / Libraries" box and/or add the *.lib file to my main project.

B) The first thing I tried to do was 'add' the library to my main project by adding the *.lib file to the project as described in the Digital Visual Fortran Programmer's Guide by Etzel and Dickinson (1999, Digital Press), Section 2.2.5 (Fortran Static Library Projects) pages 17-19. This did not work. Of this, only one paragraph is on how to use a static library. This is the same information that is available in the online help files, and it does not include most of what has been covered in this discussion. The only other reference to fortran static libraries in this guide is Section 3.5 (Specifying Project Types with DF Command Options). There is no reference to fortran static libraries in the Language Manual at all.

I have to agree, therefore, with the original message author (gnelson_ESIL February 8,2002 9:08am) that the documentation on fortran static libraries is very thin indeed, and the single method described does not work (for me).

Also, I was able to compile and link the SCIGRAPH library and SGDEMO program and run them. I did this from their original installation location under ... Microsoft_Visual_StudioDF98SAMPLESSCIGRAPH and also from copies of the files that I placed in the same path as my workspaces and projects (which I keep somewhere else). I printed off the source code to use as a reference and checked all of the project-settings- and tools-options- to use as a guide to setting up my library and project, but all to know avail.

So, the samples worked, my stuff didn't, and I still don't know why (although I am going to check the sample project's workspace tools-options-directories-include to see if that path is present). Again, I have to agree with the original message author (gnelson_ESIL February 8,2002 9:08am) that these sample programs are not well-documented nor self
-explanatory, at least in-so-far-as their ability to clearly demonstrate how to create and use a Fortran static library.

Thanks to Steve for the reply. I have tried all methods described in 2. None of them work.
Let me describe in more detail what I do and maybe someone can spot the error.
Step 1. Create a static library project.
Step 2. Add several .f source files to this project.
Step 3. Build the library. An .obj file is created for each .f file. A single .lib file is created.
Step 4. Create an executable project. Add an .f90 file which calls subroutines in the library.
Step 5a. Add the above .lib file to the executable project. Build. Error: Unresolved external symbol.
Step 5b. Go to Tools-Options-Directories-Library Files and add the path of the .lib file. Go to Project-Settings-Link-Object/Library Files and add the name of the .lib file. Build. Error: Unresolved external symbol.
Step 5c. Insert the static library project into the workspace containing the executable project. Go to Project-Set Active Project and select the executable project. Build. Error: Unresolved external symbol.

Ah.. The unresolved external symbol error does not mean the library is not being searched - given what you've said, it undoubtedly is. Rather, there is an error in your code such that the caller and callee don't agree as to what the external name should be. The most common cause for this is mismatched argument list, assuming that the routine in question is one of yours and not a language library routine.

As an experiment, put the source for the library routine directly in the executable project and build - what happens?


Retired 12/31/2016

You are clearly on to something.

I added all the .f files to the executable project and tried to build. Result: 2701 errors. All the calls to subroutines (both in the main program and in the .f files) lead to Error LNK2001: unresolved external symbol.

I'm not sure whether this is relevant, but what I'm trying to do is to install LAPACK 3.5+ which contains upgrades of routines that are in IMSL but which have now been deprecated. I suppose I could live without these upgrades for the time being, but it would be nice to know how to do this in principle.


Give us a couple of the linker messages in their entirety. A lot more info is needed to help you. Actually, I'd suggest sending a zip file of your workspace with the LAPACK routines to Lorri at vf-support.


Retired 12/31/2016

Steve was right! There was an error in my code. I had the wrong number of arguments in the call statement - this is what led to the unresolved external error message. Many thanks.


I was also getting LNK2001 errors (followed by a LNK1120 error) until I added the static library directory to Tools-Options-Directories "Include files" (I had already added it to Tools-Options-Directories "Library files". However, there was no problem with my code as I had been using the exact same modules by placing the source code "*.f90" files in my main project. I was also getting LNK1181 errors with some of the combinations of things I tried ("cannot open input file 'xxxxx.lib').

I noticed in the SAMPLES project that the main project (sgdemo) had a dependencies folder with scigraph.mod in it (scigraph is the static library). My project did not have this, and I suspected that it should, but didn't know how to get it.

I'm up and running now, so thanks again to Steve L and kleinp. I'm still curious, however, why these other suggested methods don't seem to work. In particular, including the *.lib file in the main project (as indicated in the User's Guide) and/or checking the box for the static library project in the main projects Project-Dependencies list don't seem to work. Is this everyone's experience (i.e., it just doesn't work) or is this unique to my installation/configuration? If it's the later, I would still like to know how to "fix" it. Thanks again, Bruce

Adding the library folder to the INCLUDE list should do nothing. There's probably something odd in your configuration or project, but it's difficult to diagnose remotely. Usually, if you send a zip archive of your project to vf-support, we can figure things out quickly enough.


Retired 12/31/2016

Adding the library folder to your INCLUDE list means the *.mod files the compiler makes when compiling the library will be visible.

Ah, but that would be a compile-time issue, not link-time. Yes, if you're generating .MOD files, those are located as if they were INCLUDE files.


Retired 12/31/2016

You can make it a link time issue by creating the library and then putting your *.mod files in a separate directory where the compiler can find them and subsequently modifying the library so that now some of the interfaces are invalid or missing in the old *.mod files...

I purchased Chapman's book on Fortran 90/95 for Engineer's. It's not specifically geared to CVF so it doesn't describe how to create and use static libraries in that particular environment. I did get a hint from it, however, that might be relevant.

When creating subroutines to include in a CVF Static Library Project is a separate module required as part of the library (and with the same name as the static library project?) that contains nothing but interface definitions for the various subroutines? Is that what the *.lib file is supposed to contain so that the main program knows how to access the routines in the object file library during linking? If so, I do not have such a module (and have not had time to try this since thinking of it, but I will). Although I always 'contain' my subroutines within modules, I do not, as a rule, use explicit interface definitions. Bruce

Leave a Comment

Please sign in to add a comment. Not a member? Join today