Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Compiling and Linking Fortran Code with external modules and libraries

Heli_N_
Beginner
6,320 Views

Dear all,

I am trying to create a fortran program. I have already done this in Windows and using Visual Studio.

In linux I am using ifort (ifort (IFORT) 14.0.2 20140120).

I am very new to this. I appreciate your help. I will explain the procedure I am taking below.

I have my source code ( File1.f90, File2.f90, File3.f90 and main.f90) and then I have a folder full of external libraries (*.a and *.mod )

I have not written these libraries nor I have any idea which compiler was used to compile and create those libraries.

I try to first compile my files using the following:

File1.90  and File2.f90 are not using these external libraries while File3.90 is using these external libraries. At last main.f90 is using File3.90. In order to compile I have used the following commands:

ifort -c File1.f90

ifort -c File2.f90

ifort -c File3.f90 -I  mylib&modulesDirectoryAddress     

ifort -c main.f90  -I  mylib&modulesDirectoryAddress

All files are compiled successfully but when I try to link them using the following command I ran in to error :

ifort  File1.o File2.o File3.o main.o -I  mylib&modulesDirectoryAddress

The error is this:

File3.f90 undefined reference to `m_get_mp_myFunction_'

myFunction is a function that I call inside File3.f90 from one of the external modules/libraries.

 subroutine File3_internalFunction
    use externalModule, only : myFunction
      implicit none

      !external
      character(len=*), intent(in) :: fname
      integer,          intent(in) :: timeStep
      character(len=32), intent(in)::  vname
      real, pointer   :: varValue(:)
      logical:: stopIfNotPresent
      character(len=256) ::  firstPartOfString
      character(len=80) ::  outpStr

      varValue => myFunction(fname, vname, timeStep)
   
    end subroutine

Could you please let me know what is the possible reason that I am getting the above error?  Also do I need to have additional compile or link options that I am missing right now ? I am coming from Visual Studio and I am  quite confused how to get the same exectuable here?

Also are there any system-dependent options that I will need to define?I am trying to get an executable for 64-bit Linux systems.

I would really appreciate your help.

Thank you very much in Advance for your help.

Kind Regards,

Heli

 

 

0 Kudos
1 Solution
Kevin_D_Intel
Employee
6,215 Views

Ok, looking back over this thread, I think this might work for you since you are trying to link against libraries built with MPI but where your program does not use MPI. I built a small mock-up to test this; a main program linked to a static library built with a routine containing MPI code.

First, to resolve the MPI routines, on your ifort command-line for the link step at least, try adding: -L /opt/intel/impi/4.1.3.049/intel64/lib -lmpi -lmpigf -lmpigi

Second, if the link succeeds, before you run the executable, ensure that LD_LIBRARY_PATH contains: /opt/intel/impi/4.1.3.049/intel64/lib

I believe this may enable you to link and run the resulting executable without using mpirun. For my trivial test case, I could run the executable (a.out in my case) itself directly, and with mpirun using two ranks.

View solution in original post

0 Kudos
27 Replies
Kevin_D_Intel
Employee
432 Views

It appears there is OpenMP code in the source file from which the mgl_halo style symbols came from. Did you compile the associated source from which those symbols came or was it pre-compiled elsewhere and you are just linking it in?

Adding -openmp to at least the link will address most of those. I'm not sure about the __intel_new_feature_proc_init. That might be coming from trying to link objects created by an earlier version of the compiler than you are using.

Given all the steps you are having to take here to build this program I wonder whether any executable you might produce will execute if that's the goal. It feels like you might be trying to mix/match too much pre-compiled material and without perhaps the needed development environment and appropriately compatible compiler options to support how some components being used have been built.

0 Kudos
Heli_N_
Beginner
432 Views

Dear Kevin, 

Thank you very much for your help. Adding the -openmp flag solved the issue.  I link in the following way now:

•ifort -openmp  main.o File1.o File2.o File3.o     -LmyLibraryDirectory -lmyExternalLib -L /opt/intel/impi/5.1.0.079/intel64/lib/ -lmpi -lmpigf 
 
My executable (a.out) runs ok on my machine. When I try to run it on another clean (not having Intel redistributable or runtime installed) linux machine. I get the following error:
 
cannot open shared object file
 
 which refers to some library either in the redistributable package or in the Intel MPI library runtime. 
When I manually install the redistributable package  and the runtime and add their lib directory to LD_LIBRARY_PATH , the executable runs fine. 
 
Is there anyway that I could have an executable that would already come with these libraries included ( redistributable and runtime libs ) and the user would just take the exectuable to their machine and run it?
 
Again, thanks everyone for all your help. 
Heli
0 Kudos
Steven_L_Intel1
Employee
432 Views

You can do this on Linux - add -static-intel to the ifort command line that does the linking. Otherwise the end user would need to install our redistributables.

0 Kudos
Heli_N_
Beginner
432 Views

Dear Steve,

Thank you very much for your help, This flag did the trick. now the user will only need to install the Intel MPI runtime environment. 

Thanks again, 

 

0 Kudos
Heli_N_
Beginner
432 Views

Dear all, 

I have a question regarding the same topic. I have buildt my executable on an openSUSE Linux and the exectuable runs perfectly on other openSUSE machines as well (offcourse after installing Intel MPI Runtime Library and setting the path to the lib folder ).

I have also tested my executable on an Ubuntu Desktop and it works correctly. 

My question is, does the exectuable working ok on OpenSuse and Ubuntu mean that it will work on any linux distribution? 

I have built my executable using -static-intel, does this guarantee that the executable will work on any linux distribution ?

If not, is there any way to make sure that the exectuable will run on all linux distributions?

I would really appreciate your comments, 

Thank you very much in Advance for your help, 

Heli

0 Kudos
Steven_L_Intel1
Employee
432 Views

There are no guarantees, especially on Linux, given variations in glibc and kernel. At best, with -static-intel you will be ensuring that the user doesn't need additional .so files in order to run the program (unless you use coarrays).

0 Kudos
Heli_N_
Beginner
432 Views

Dear Steve, 

Thanks alot for your help, 

Best Regards, 

Heli

0 Kudos
Reply