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

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

cean
New Contributor II
2,089 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
Honored Contributor III
2,080 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

 

0 Kudos
cean
New Contributor II
2,027 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.

 

 

0 Kudos
FortranFan
Honored Contributor II
2,029 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=msvc-160

 

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-and-reference/top/compiler-setup/using-microsoft-visual-studio.html

 

 

0 Kudos
cean
New Contributor II
2,018 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

0 Kudos
Reply