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,588 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,483 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
Steven_L_Intel1
Employee
6,124 Views

You need to include in the ifort command for linking the .a for the externally-supplied library.

0 Kudos
Heli_N_
Beginner
6,124 Views

Steve Lionel (Intel) wrote:

You need to include in the ifort command for linking the .a for the externally-supplied library.

 

Dear Steve,

Thanks for your reply. I tried to add my libraries with the following command.

ifort  File1.o File2.o File3.o main.o -IMylib&modulesDirectoryAddress -LMylib&modulesDirectoryAddress

and it still gives me the previous error.

Then I tried the following option as wel :

ifort  File1.o File2.o File3.o main.o -IMylib&modulesDirectoryAddress -LMylib&modulesDirectoryAddress -lLIB1.a -lLIB2.a

but still it is not working. It is giving me the following error.

ld: cannot find -lLIB1.a

ld: cannot find -lLIB2.a

What am I doing wrong here?

Thank you very much in Advance for your help.

Best Regards,

 

 

0 Kudos
mecej4
Honored Contributor III
6,124 Views

Include files and module files are not used during the link step, so adding -I... to a linker command does nothing other than add to the confusion.

The Unix/Linux convention for specifying additional libraries is as follows: to use a library with name libmyname.a, the command line option is -lmyname. It is not appropriate to use capital letters when naming library files, especially the "lib" part. If you rename LIB1.a to lib1.a and LIB2.a to lib2.a, the linker command line should contain "-l1 -l2". For each name after "-l", the library file name is constructed by adding "lib" before and ".a" after.

You can also list the full name without a preceding '-', but you probably do not need to do so in your case.

0 Kudos
Steven_L_Intel1
Employee
6,124 Views

The .a sufix is not used for the -l switch - it looks for .a automatically.

0 Kudos
Heli_N_
Beginner
6,124 Views

Dear all,

Thanks for your reply. I corrected my library names. I do not get the can not find library error, but I ran in to another error. Now my procedure is as follows.

ifort -c File1.f90

ifort -c File2.f90

ifort -c File3.f90 -I  mylib&modulesDirectoryAddress     

ifort -c main.f90  -I  mylib&modulesDirectoryAddress

and then I tried to link in two ways:

Try1:  ifort File1.o File2.o File3.o main.o -LmyLibraryDirectory -lmyExternalLib

Try2: ifort main.o File3.o File2.o File1.o  -LmyLibraryDirectory -lmyExternalLib

using both try1 and try2 I get the same error.

../Fortran_dbg/Try1/r4/lib/MyExternalLib.a(bci.o): In function `m_bci_mp_bci_start_':
../../Sources/Kernel/bci.f:(.text+0x3a): undefined reference to `mpi_init_'
../../Sources/Kernel/bci.f:(.text+0x55): undefined reference to `mpi_comm_rank_'
../../Sources/Kernel/bci.f:(.text+0x79): undefined reference to `mpi_comm_size_'
../../Sources/Kernel/bci.f:(.text+0x94): undefined reference to `mpi_finalize_'

and a lot more of these undefined reference  errors refering to functions apparently inside this library and related to mpi.

Could you please help me on what I am doing wrong here.

Thank you very much in Advance for your help.

Best Regards,

 

0 Kudos
Lorri_M_Intel
Employee
6,124 Views

 

I assume your program has a "use mpi" statement.

On Windows, this would automatically tell the linker to add the MPI libraries.

On Linux, this does not happen.  YOU need to explicitly add the MPI libraries to your link line.    And, what your MPI libraries are named will depend on which MPI you're using.

            --Lorri

 

0 Kudos
Madhubalan_R_
Beginner
6,124 Views
This comment has been moved to its own thread
0 Kudos
Heli_N_
Beginner
6,124 Views

Dear Lorri, 

Thanks for your reply. The libraries that I am using to link with are some libraries from an external software. 

I am not using this software, but I am using only these libraries and my code does not run any mpi jobs. 

I have the windows version of my code as well and on windows I have the Intel MPI runtime installed on my machine and not the Intel MPI library. 

Also the visual estudio for the windows version contains these additional configurations under Properties/Fortran/commandline

/libs:static /threads /c

and in addition it is asked to ignore MSVCRT.

Is my linking error related to any of the above ? 

Thank you very much in Advance for your help, 

 

 

0 Kudos
Steven_L_Intel1
Employee
6,124 Views

Your source file bci.f is calling MPI routines.

MSVCRT is strictly a Windows issue and does not apply here.

0 Kudos
Heli_N_
Beginner
6,124 Views

 

Dear all,

 

Thanks for your reply. Assuming that my external libraries are calling MPI routine and that the mpi is INTEL MPI. Could you please guide how I should link to Intel mpi libraries ?

I have already installed Intel MPI  in

/opt/intel/impi/5.1.0.079/

and there are a number of *.so  libraries in the following folder.  (including libmpi.so.5)

/opt/intel/impi/5.1.0.079/lib64/

How should I link with Intel mpi libraries?

Also is there any of my operating system kernel libraries that I might have to link with as well?

I would appreciate any help.

Thank you very much in Advance for your help,

 

 

0 Kudos
TimP
Honored Contributor III
6,124 Views

mpiifort would automatically link Intel MPI and compiler libraries.  Sourcing the mpivars.sh script  will set up the Intel MPI shared object paths for build and run.  You would also source compilervars.  Intel compilers get the gnu and linux library paths automatically from the g++ which is active when compiling.

0 Kudos
Heli_N_
Beginner
6,124 Views

Hi Tim,

Thanks for your answer. I do the following steps but I the command mpiifort still is not recongnized and I get the following:

1.source compilervars.sh intel64

now if I do which ifort, i get the folloiwng:

/opt/intel/composer_xe_2013_sp1.2.144/bin/intel64/ifort

2. If I check the LD_LIBRARY_PATH, i get the following:

/opt/intel/composer_xe_2013_sp1.2.144/compiler/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/mpirt/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/compiler/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/mkl/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/compiler/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/mpirt/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/compiler/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/mkl/lib/intel64:/usr/lib64/mpi/gcc/openmpi/lib64

3. Then I go to the impi forlder and I source mpivars.sh by running (source mpivars.sh)

cd /opt/intel/impi/5.1.0.079/intel64/bin

source mpivars.sh

but still when I run mpiifort I get the following :

If 'mpiifort' is not a typo you can use command-not-found to lookup the package that contains it, like this:
    cnf mpiifort

This is while mpif90 works fine and gives this :

gfortran: fatal error: no input files                                                                                
compilation terminated.

What am I missing here ? I appreciate your comments.

Thank you very much in Advance for your help

Heli

 

 

 

 

 

0 Kudos
Kevin_D_Intel
Employee
6,124 Views

You did not show your PATH after sourcing compilervars or mpivars but in your LD_LIBRARY_PATH there is a hint that you likely have openmpi in your PATH based on the appearance of “/usr/lib64/mpi/gcc/openmpi/lib64”. That is most likely where mpif90 is resolving from.

Why mpiifort was not found after sourcing mpivars.sh is not clear.

After sourcing mpivars.sh, what is the I_MPI_ROOT environment variable set to?

After sourcing mpivars.sh, double check PATH to ensure /opt/intel/impi/5.1.0.079/intel64/bin is included. The error you received suggests that it is not in your PATH. You might also double-check also that mpiifort is present in that directory, it should be.

0 Kudos
TimP
Honored Contributor III
6,124 Views

If you have openmpi in your PATH and LD_LIBRARY_PATH after the Intel coarray MPI library path, openmpi won't work even if it is able to compile.

As Kevin hinted, a build of openmpi provided with your linux would have mpif90 built against gfortran.  The mpif90 which comes with Intel MPI would do the same (there is mpif90 -fc=ifort if you prefer that).  If your linux provides an openmpi on the default PATH and LD_LIBRARY_PATH, you should remove it.  Not only would it conflict with other MPI implementations (including use of openmpi with ifort), it may be out of date.  The instructions for building openmpi against either ifort or gfortran are on the openmpi site, in case you still want openmpi; if you build it yourself, you would use --prefix to give it its own installation path (analogous to /opt/intel/impi/version/).  Openmpi and Intel mpi don't mix; for example, they have conflicting coding of data types.

The Intel coarray MPI setup seems to conflict with the advice given by Intel prior to ifort adding it, that each MPI installation should have its own path requiring a specific command like 'source mpivars.sh' to set it up.  Large data centers typically use the module command for this purpose.  They still have to observe in general the precaution of setting up MPI after ifort, although recent Intel compilervars.sh does check for the case where Intel MPI is already set up.

0 Kudos
Heli_N_
Beginner
6,124 Views

Dear all,

I  uninstalled openmpi from my machine.  Now the command mpif90 is not recognized as well. before setting the compiler and mpi environment variables  LD_LIBRARY_PATH  will give me this:

/usr/lib64/mpi/gcc/openmpi/lib64  although I have uninstalled openmpi. (I can not restart my machine at the moment). I think the openmpi runtime is still on the machine as some applications depend on it.

I manually unset LD_LIBRARY_PATH  in a new terminal and then I set the compilervars.sh and mpivars.sh and I get the following:

echo $LD_LIBRARY_PATH

/opt/intel/impi/4.1.3.049/intel64/lib:/opt/intel/composer_xe_2013_sp1.2.144/compiler/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/mpirt/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/compiler/lib/intel64:/opt/intel/composer_xe_2013_sp1.2.144/mkl/lib/intel64

echo $I_MPI_ROOT

/opt/intel/impi/4.1.3.049

I am using another impi version now.

Anyway I do not have an mpiifort inside the folder

/opt/intel/impi/4.1.3.049/intel64/bin/

I have mpirun though.  Is there anything wrong with my installation?

Thanks in Advance,

 

 

 

 

 

 

 

 

0 Kudos
Kevin_D_Intel
Employee
6,124 Views

The contents under /opt/intel/impi/4.1.3.049 sounds like the MPI run-time installed for support of the Fortran Coarray feature which is not the complete MPI installation, hence, the missing mpiifort as you noted (among others).

Earlier you mentioned the /opt/intel/impi/5.1.0.079/. From what you wrote it sounded like you installed that separately yourself and I was expecting that might be the full Intel MPI installation. You could check whether mpiifort is present under that area. If so, then after sourcing compilervars, source the mpivars from this 5.1.0.079  installation and check if the mpiifort command then works.

If mpiifort is not present in the 5.1.0.079 installation, then to use MPI you would need to get the full Intel MPI installation package and install that.

Sorry about all the problems you are experiencing.

0 Kudos
Heli_N_
Beginner
6,124 Views

Dear Kevin,

Thanks for your reply. I am now sure that I do not have the full MPI installation (any versions) and that I only have the MPI runtime.

My fortran compiler though is part of the Intel® Parallel Studio XE Composer Edition for Fortran Linux*. (2013 update 5)

According the product information the composer edition does not come with the MPI library adn only the cluster edition has MPI library.

I am trying to compile a code that uses some external libraries. These libraries are calling some MPI routine which causes errors at the linking step.

./../Sources/Kernel/bci.f:(.text+0x3a): undefined reference to `mpi_init_'
../../Sources/Kernel/bci.f:(.text+0x55): undefined reference to `mpi_comm_rank_'
../../Sources/Kernel/bci.f:(.text+0x79): undefined reference to `mpi_comm_size_'
../../Sources/Kernel/bci.f:(.text+0x94): undefined reference to `mpi_finalize_'

When google , I saw that most poeple solved their problem by using mpi fortran compilers instead of fortran compilers. Is using mpiifort in my case the first thing I must be looking at ?

When I do strings over one of these external libraries and search for GCC I see the following:

-defaultlib:libxnhost -defaultlib:xn_host64d
GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)
GCC: (GNU) 3.3.3 (SuSE Linux)

I would appreciate your experiences.

Thanks in Advance,

 

0 Kudos
Kevin_D_Intel
Employee
6,484 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.

0 Kudos
Steven_L_Intel1
Employee
6,124 Views

There is no such thing as an "MPI Fortran compiler". Rather, MPI installations include scripts (such as mpifort) that invoke an existing Fortran compiler with options to add MPI libraries and include files.

If you are doing MPI development, you need a full MPI installation. If you are only using Fortran coarrays with Intel Fortran, we provide everything you need with the compiler.

0 Kudos
Heli_N_
Beginner
3,817 Views

Dear all,

Thanks for your replies. I did as Kevin suggested.  I am linking to Intel mpi runtime. Some of my previous errors disappreared. 

I am not getting the following error anymore.

./../Sources/Kernel/bci.f:(.text+0x3a): undefined reference to `mpi_init_'
../../Sources/Kernel/bci.f:(.text+0x55): undefined reference to `mpi_comm_rank_'
../../Sources/Kernel/bci.f:(.text+0x79): undefined reference to `mpi_comm_size_'
../../Sources/Kernel/bci.f:(.text+0x94): undefined reference to `mpi_finalize_'

but I still run in to errors when linking. I am not sure if my linking order is correct, though.

Try1: ifort File1.o File2.o File3.o main.o -LmyLibraryDirectory -lmyExternalLib -L /opt/intel/impi/5.1.0.079/intel64/lib/ -lmpi -lmpigf 

Try2 : ifort File1.o File2.o File3.o main.o  -L /opt/intel/impi/5.1.0.079/intel64/lib/ -lmpi -lmpigf  -LmyLibraryDirectory -lmyExternalLib

both trys give the following error all starting with mgl :

 undefined reference to `__intel_new_feature_proc_init'
 In function `mgl_halo_mp_mgl_halo_mark_':
(.text+0x34e): undefined reference to `__kmpc_global_thread_num'
 In function `mgl_halo_mp_mgl_halo_mark_':
(.text+0x444): undefined reference to `__kmpc_ok_to_fork'
In function `mgl_halo_mp_mgl_halo_mark_':
(.text+0x471): undefined reference to `__kmpc_fork_call'
 In function `mgl_halo_mp_mgl_halo_mark_':
(.text+0x487): undefined reference to `__kmpc_serialized_parallel'
In function `mgl_halo_mp_mgl_halo_mark_':
(.text+0x4b9): undefined reference to `__kmpc_end_serialized_parallel'
 In function `mgl_halo_mp_mgl_halo_mark_':
(.text+0x54f): undefined reference to `__kmpc_for_static_init_4'
 In function `mgl_halo_mp_mgl_halo_mark_':

Please note that I only linked with two libraries from the runtime lib directory ( -lmpi  -lmpigf). I do not have -lmpigi in my runtime directory.

I also tried both with runtime 4.0.3 and 5.1.0.079 and my fortran compiler verstion is:

ifort (IFORT) 13.1.3 20130607

Should there I be using any specific version of the runtime environment for my compiler ?

As always I would really appreciate your help.

Thanks in Advance,

 

 

 

 

0 Kudos
Reply