Question

I'm very very new to Fortran and the whole Intel compiler thing (I'm using the Windows-based Intel compiler with IMSL library w/o Visual Studio integration, therefore command line only).

The problem should be extremely simple: I have a Fortran program that I needed to compile that's stored as four different source code parts. The main program (and some subroutines) are stored in the code file named central.for, and there are also three files, let's call them s1.for, s2.for and s3.for, each of which contains only one subroutine (lets call them sub1-sub3) and no main program. The main program calls the subroutines stored in s1-s3 as well as in central.for.

The question is how should I compile it:

When I try to compile the central.for, it gives error LNK2019: unresolved external symbol _SUB1 referenced in function _MAIN__. Similarly it gives the same message for SUB2 and SUB3.

When I try to compile s1.for-s3.for, it gives error LNK2019: unresolved external symbol _MAIN__ referenced in function _main

It seems obvious that I need to link them somehow. However, I have no idea how

Was it helpful?

Solution

You need to compile the pieces of the program without linking first, then link them all together. So your command line will look like this:

ifort /c s1.for
ifort /c s2.for
ifort /c s3.for
ifort /c central.for

where the /c is short for /compile-only; /nolink would also work. This will generate files s1.o, s2.o, s3.o, and central.o, respectively. The above can be done in any order. Now the individual pieces get linked together, and you'll do something like

ifort /exe:central.exe central.o s1.o s2.o s3.o

to build the executable.

Note that this gets one step more complicated if you're compiling modern fortran code that uses modules, and (for instance) the main program in central.for has a line like use s2. In that case, you have to compile (even with /c) s2 before central; when you compile s2 in that case you'd get an extra output file, s2.mod. That s2.mod file would be needed to compile central.for. But given the filename extensions, this likely is an older code and modules probably aren't an issue.

OTHER TIPS

This is how I have done it. Each of the supporting functions compile in a Static Library. Then in the main program project add an existing item and selected the static library .lib file (Output from compilation). The just call the functions as if their source was part of the main code (with CALL MYLIBFUN())

Good Luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top