Quantcast
Channel: Intel® Software - Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2746

Modules and separate compilation, duplicate symbol error

$
0
0

 

Consider the following, I have an interface file, say interface.fi as below,

module foo_type
    type bar
    end type bar

end module foo_type

module foo
    use foo_type

    interface
        subroutine sub(var)
            use foo_type
            type(bar) var
        end subroutine sub
    end interface
end module foo

And there are multiple source files use the models in this file, say pr.f90

include "interface.fi"

program prg
    use foo
    type(bar) b
    call sub(b)
end program prg

and another sub.f90

include "interface.fi"

subroutine sub(var)
    use foo
    type(bar) var
end subroutine sub

Now consider I compile them as below,

	ifort -c sub.f90
	ifort -c prg.f90
	ifort -o prg sub.o prg.o

And I get the error 

duplicate symbol _foo_type._ in:
    sub.o
    prg.o
duplicate symbol _foo._ in:
    sub.o
    prg.o

It appears that both translation unit generates symbols for the module foo_type and foo. If I only include the interface file in only one source file. And in the Makefile make sure that it is compiled first, or just compile it first to generate models and do not include it in only source files. Then everything works fine. This is ok for small project where write a Makefile manually is not too much a trouble. But in other build systems, say, CMake for a larger program, I often has to write a custom command just to make sure that certain module files are generated first.

In addition, it appears that using gfortran there is no such problem. Is there a solution that I can just include the interface file wherever its members are used and still compile successfully, without going into the trouble to make sure there is only one instance of the modules being included?


Viewing all articles
Browse latest Browse all 2746

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>