Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
27159 Discussions

Fortran call C lib: MSVCRTD.lib(exe_main.obj) : error LNK2019 unresolved external symbol

cean
New Contributor I
616 Views

Hi,

I made a c lib(csim.lib), then Fortran main (fmain.f) calls it through another c function (c_fortran_csim.c). I got this error:

MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

It seems the Fortran and the C are not set in the same way. What setting should I change?

Cheers,

Cean

 

0 Kudos
4 Replies
mecej4
Black Belt
607 Views

You need to examine and fix your Visual Studio project settings. I do not use Visual Studio for building, and it is rather trivial to build your small example at the command line.

 

T:\lang\FcallC>icl /Od /c csim.c c_fortran_csim.c
T:\lang\FcallC>ifort /Od fmain.f csim.obj c_fortran_csim.obj
T:\lang\FcallC>fmain
         100   100.0000

 

cean
New Contributor I
554 Views

I want csim.lib instead of csim.obj.

I was using icx+ifort, now I changed to use icl+ifort, it seems working.

1. Create a c lib:

 

D:\FcallClib\clib>icl /c csim.c
Intel(R) C++ Intel(R) 64 Compiler Classic for applications running on IA-32, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

csim.c

D:\FcallClib\clib>LIB.EXE /OUT:csim.lib csim.obj
Microsoft (R) Library Manager Version 14.16.27040.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 

2. Create fortran application to call that c lib:

 

D:\FcallClib\FcallC>icl /c c_fortran_csim.c
Intel(R) C++ Intel(R) 64 Compiler Classic for applications running on IA-32, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

c_fortran_csim.c

D:\FcallClib\FcallC>ifort /DYNAMICBASE "D:/FcallClib/FcallC/csim.lib" fmain.f c_fortran_csim.obj /link /out:a.exe
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on IA-32, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.16.27040.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:fmain.exe
-subsystem:console
/out:a.exe
fmain.obj
c_fortran_csim.obj
D:/FcallClib/FcallC/csim.lib

D:\FcallClib\FcallC>a
         100   100.0000

 

Hope it will work with VS.  

I want to use a third party lib and I can build it in VS, not in command line.

 

 

FortranFan
Honored Contributor II
556 Views

@cean ,

 

Instead of the CMake approach, will you be willing to try instead what the vendor of the IDE you are trying (Microsoft with Visual Studio) documents as the approach for creating a static library project and how to reference it in a console app:

https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-static-library-cpp?view=...

 

and then try to substitute that console app with a Fortran main program for which your other vendor (Intel) provides documentation along with its compiler reference:

https://software.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide...

 

 

cean
New Contributor I
545 Views

@FortranFan 

I think I need this Selecting a Version of the Intel® Fortran Compiler

But to select which c compiler to use.

My two projects, c project to create c lib and main project (fortran + c) to call that lib. MS-VS by default chose two no compatible ones. I need to change the c project to use icl compiler. There are no message shown in the Output Window which c compiler is used. How do I change?

 

option2.jpg

Reply