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

Painted into a corner: a DLL combining C and Fortran routines

Arjen_Markus
Honored Contributor II
1,087 Views
Hello,

Igot stuck with the following problem: I have a library with C and Fortran routines and that needs
to be turned into a DLL. Visual Studio does not allow me to combine sources in different languages
in one project, whereas Developer Studio did. So, I have created two projects, one for the C part
and one for the Fortran part.

However, the C part contains some routines that are also used in the Fortran part, so I have
a sort of cyclic dependency that is relevant at link time.

I thought I could avoid splitting off these common routines into a third part, by compiling the
Fortran routines into a static library and linking that library together with the C routines into
a DLL.

That does not work, however: libifcoremt.lib is missing a MAIN__ routine. So, unless I can
persuade the linker that that is not necessary (under Linux I need to specify -nofor_main
to do that, but Windows has no such option)I have to goto the other solution.

My question: Is that indeed the _only_ way?

(Hopefully I have described the problem clear enough ;))

Regards,

Arjen

0 Kudos
10 Replies
Andrew_Smith
Valued Contributor I
1,087 Views
How is is possible to have common routines (source files?) in both C and Fortran projects ?
0 Kudos
Arjen_Markus
Honored Contributor II
1,087 Views
Oh, sorry, sloppy choice of words: I have C routines, callable from Fortran, that are used in both C and
Fortran routines. So they are common to both sets.

Regards,

Arjen
0 Kudos
IanH
Honored Contributor III
1,087 Views
You've got something screwy with your project settings. Are you sure the C project is building a DLL? What have you changed from the out-of-the-box project default settings for both projects?

(To be clear - you've got a fortran project that builds a static library and then a C project that builds a DLL that references the fortran project? If so - make sure the run time library settings are consistent between the two and let the C project know where the Fortran runtime libraries directory is and all should be merry. The cyclic dependency thing shouldn't matter at link time.)
0 Kudos
Arjen_Markus
Honored Contributor II
1,087 Views
The C project does try to build a DLL
I have changed the following (apart from macros that are used by the C code):
- Additional library path to find the Fortran runtime libraries
- Additional library ifconsol.lib, because otherwise I got a message that it could not find that library
(this is due to the Fortran source being compiled as a static library, I guess)
- The option /force, as otherwise the linker trips over multiply defined symbols

The Fortran project uses defaults only (except for a path to some include files).

The error message is:

LIBCMT.lib(crt0init.obj) : warning LNK4006: ___xc_a already defined in MSVCRT.lib(cinitexe.obj); second definition ignored

LIBCMT.lib(crt0init.obj) : warning LNK4006: ___xc_z already defined in MSVCRT.lib(cinitexe.obj); second definition ignored

LIBCMT.lib(tolower.obj) : warning LNK4006: _tolower already defined in MSVCRT.lib(MSVCR90.dll); second definition ignored

LIBCMT.lib(_file.obj) : warning LNK4006: ___iob_func already defined in MSVCRT.lib(MSVCR90.dll); second definition ignored

LIBCMT.lib(osfinfo.obj) : warning LNK4006: __get_osfhandle already defined in MSVCRT.lib(MSVCR90.dll); second definition ignored

LIBCMT.lib(osfinfo.obj) : warning LNK4006: __open_osfhandle already defined in MSVCRT.lib(MSVCR90.dll); second definition ignored

libifcoremt.lib(for_main.obj) : error LNK2019: unresolved external symbol _MAIN__ referenced in function _main

Regards,

Arjen

0 Kudos
Steven_L_Intel1
Employee
1,087 Views
Change the "Runtime Library" property of the Fortran project to be the same as your DLL project (probably Multithread DLL).
0 Kudos
Arjen_Markus
Honored Contributor II
1,087 Views
I did that, but the error message remains the same.

(It is probably easier to yield and split up the C sources into two projects, but it feels like
defeat - one simple library becoming three interdependent DLLs)

Regards,

Arjen
0 Kudos
Les_Neilson
Valued Contributor II
1,087 Views

I have a mixed C/Fortran solution which builds a dll

The Fortran project creates a lib in $(Outdir)($(ProjectName).lib
where Outdir is $(Configuration) which would be debug or release
Fortran -> Libraries -> Runtime Library Multithread DLL

The C Project has :
C/C++ -> Code Generation -> Runtime library Multi-threaded DLL

The linker step has output file \winapi.dll
Link Library Dependencies = Yes
Additional Library Directories = above (plus other paths)
Input-> Additional dependencies -- here I added the fortran lib from above plus other used libs

So it should work ok for you
Les
0 Kudos
Arjen_Markus
Honored Contributor II
1,087 Views
Those settings are not different than mine. Odd.

Maybe I should try this first with a smaller example.

Regards,

Arjen
0 Kudos
Steven_L_Intel1
Employee
1,087 Views
You have some object that is referencing LIBCMT.LIB, which is the C static multithread library. Most likely it is your Fortran library. Rebuild it with the proper Libraries > Runtime Library > Multithread DLL setting and make sure your C project is using the rebuilt version.
0 Kudos
Arjen_Markus
Honored Contributor II
1,087 Views
Hm, the Fortran project has the options "/libs:dll /threads" as a consequence of the option to use the
multithreaded DLL.

I can not find any object file that references "libcmt" ... that is, the reference was in another C library
that I needed to include (that was already available as aDLL, but I had chosen the wrong dependency
so ended up with the static library instead).

Solved! Thanks for all the replies. This was a real puzzle, but of course, if you look in the wrong place
it is unlikely that you find the cause of the problem.

Regards,

Arjen
0 Kudos
Reply