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

Compile C file in fortran project with VS2017 and IVF2017

An_N_1
New Contributor I
685 Views

I have some .c files in a fortran project. By adding "command line" in "custom build" of the file properties to compile the file, the command line is like "cl.exe /c /EHsc ...", and set the "output" property to proper folder.

The project complie and link well under VS2010/2012 with ivf2012/2015.

But after upgrade to VS2017/2015 and ivf2017, build reported error (fail to open include file: "stdio.h", no such file or directory...).

I have verified and tried the followings:

(1) OS is window 10

(2) C++ has been installed with VS2017 or VS2015, and a helloworld C project with stdio.h included goes well.

(3) Reinstalled VS2017 or VS2015 and Intel parallel studio for times, of couse it meant nothing.

(4) Add corresponding "ucrt" directory path where the header files are in to include path of fortran project and windows system environment. It doesn't work. And I change the c file's command line of custom build property to "set", so the environment variables can be seen in complie output. It seems the variables are not affected by system environment nor fortran include path setting.

My questions are:

How to make it work?

What is the recommanded way to compile and link .c/.cpp code in a fortran project?

How to modify the path environment of command line of c files in a fortran project?

Thanks!!

0 Kudos
6 Replies
jimdempseyatthecove
Honored Contributor III
685 Views

What you do is to add a C/C++ project to your Solution, with he project type being a static library, then make the Fortran project dependent upon it.

 

Right-Click on Solution in Solution Explorer, choose Add, Add Project, ...

Jim Dempsey

0 Kudos
gib
New Contributor II
685 Views

Hi Jim, do you mean that any C/C++ code must be first compiled and packaged as a library?  I routinely link in .obj files that were compiled from C code.  I must be misunderstanding something.  My case is a bit different, though, because I compile the C code outside the Fortran project.

Gib
 

0 Kudos
Steve_Lionel
Honored Contributor III
685 Views

If you want C code compiled as part of the VS solution, Jim is correct that you must put the C code in a separate C/C++ project and link it in with the Fortran project. It has been this way since VS2002. If the Fortran project has the main program, make the C/C++ project a static library and a dependent project. If the C/C++ project has the main program, make the Fortran project a static library and name the .lib as input to the C/C++ project (MSVC projects won't link in non-MSVC dependent projects.)

0 Kudos
An_N_1
New Contributor I
685 Views

My way is similar to Gib's. Build the solution by linking .obj files compiled from .c file and .for file together, rather than by a static library.

So, according to Jim and Steve's advice, static library is the only way to do things correct. Thanks, I got it.

Since I have only 2 C files, make a project is a little kind of "use a sledge hammer to crack a nut"~

0 Kudos
jimdempseyatthecove
Honored Contributor III
685 Views

>>Since I have only 2 C files, make a project is a little kind of "use a sledge hammer to crack a nut"~

This is a rather light weight sledge hammer (fly swatter size IMHO) because the extra time required to link the .lib might be in the 10s of milliseconds.

I do not think MS VS has a C/C++ target type as "none".

While you can insert a makefile as a pre-compile build step, it is much easier to use a separate project.

Jim Dempsey

0 Kudos
An_N_1
New Contributor I
685 Views

Thanks all!

All advices can solve the problem~

And I have tried adding "$(UniversalCRTSdkDir)Include\$(UCRTVersion)\ucrt" to include path of 'intel compiler and tools->compilers' . It works too.

I wonder since "$(UniversalCRTSdkDir)Include\$(UCRTVersion)\um" is in the path after installation, why "$(UniversalCRTSdkDir)Include\$(UCRTVersion)\ucrt" is not here?

0 Kudos
Reply